Java I/O流实现文件内容复制

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Scanner;

/***
    * 复制功能 	* @author HDC
    */
public class Copy {
	public static void main(String[] args) {
		try {
			Scanner scn = new Scanner(System.in);
			System.out.println("请输入要复制文件的路径");
			String path1 = scn.next();
			System.out.println("请输入复制文件的新名字");
			String path2 = scn.next();
			File f1 = new File(path1);
			File f2 = new File(path2);
			// 判断文件是否存在
			if (!f1.exists()) {
				f1.createNewFile();
			}
			if (!f2.exists()) {
				f2.createNewFile();
			}
			// 创建输入输出流
			InputStream in = new FileInputStream(f1);
			OutputStream out = new FileOutputStream(f2);
			byte[] b = new byte[1024];
			int len = 0;
			// 查看原文件是否有内容,若没有手动输入
			if ((len = in.read(b)) == -1) {
				System.out.println("源文件为空,请手动输入再复制");
				String s = scn.next();
				OutputStream out1 = new FileOutputStream(f1);
				out1.write(s.getBytes());
				out1.flush();
				out1.close();

			} else {
				out.write(b, 0, len);
			}
			while ((len = in.read(b)) != -1) {
				out.flush();
				out.write(b, 0, len);
				out.flush();
			}
			System.out.println("复制成功");
			in.close();
			out.close();
		} catch (Exception e) {
		// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}

java-list

某中学有若干学生
学生对象放在一个List中,每个学生有一个姓名属性、班级名称属性和考试成绩属性,某次考试结束后,每个学生都获得了一个考试成绩。请打印出每个班级的总分和平均分。

定义接口

package com.practice3;
/***
    * 自定义CompareTo接口
    * @author HDC
    */
public interface MyCompare<T> {
	int myCompareTo(T o);
}

自定义MySort排序

package com.practice3;
/***
    * 自定义MySort排序
    */
import java.util.List;
public class MySort {
	public static <T extends MyCompare<T>> void sort(List<T> list) {
		for (int i = 0; i < list.size(); i++) {
			for (int j = 0; j < list.size(); j++) {
				if ((list.get(i)).myCompareTo(list.get(j))>0) {
					T t = list.get(i);
					list.set(i, list.get(j));
					list.set(j, t);
				}
			}
		}
	}
}

学生类里面有班级,姓名,成绩属性

package com.practice3;
/***
    * 学生类里面有班级,姓名,成绩属性
    * @author HDC
    *
    */
public class Student implements MyCompare<Student>{
	private String name;
	private String cls;
	private int grade;
	public Student(String name,String cls,int grade){
		this.name=name;
		this.cls=cls;
		this.grade=grade;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getCls() {
		return cls;
	}
	public void setCls(String cls) {
		this.cls = cls;
	}
	public int getGrade() {
		return grade;
	}
	public void setGrade(int grade) {
		this.grade = grade;
	}
	@Override
	public int myCompareTo(Student o) {
		if(o.cls.compareTo(this.cls)!=0){
			return o.cls.compareTo(this.cls);
		}else if(o.name.compareTo(this.name)!=0){
			return o.name.compareTo(this.name);
		}else{
			return this.grade-o.grade;
		}
	}
}

测试学生类

package com.practice3;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
/***
    * 测试学生类
    *
    * @author HDC
    *
    */
public class Test {
	public static void main(String[] args) {
		List<Student> std = new ArrayList<Student>();
		std.add(new Student("asd", "A", 80));
		std.add(new Student("qwe", "C", 70));
		std.add(new Student("zxc", "B", 60));
		std.add(new Student("aaa", "A", 70));
		std.add(new Student("sss", "C", 90));
		std.add(new Student("ddd", "C", 85));
		std.add(new Student("dfd", "B", 70));
		std.add(new Student("dfg", "A", 90));
		MySort.sort(std);
		for (Student s : std) {
			System.out.println(s.getCls()+"\\t"+s.getName()+"\\t"+s.getGrade());
		}
		System.out.println("---------------------------");
		//求平均分和总分
		Iterator<Student> it = std.iterator();// 使用迭代器
		Student s = it.next();// 获取对象
		String cls = s.getCls();
		int sum=s.getGrade(),i=1;
		while (it.hasNext()) {// 判断迭代器当中是否存在下一个数
			s = it.next();// 获取对象
			if(s.getCls()!=cls){
				System.out.println(cls+"班的总分为:"+sum+"\\t平均分是:"+(double)sum/i);
				cls=s.getCls();
				i=1;
				sum=s.getGrade();
			}else{
				i++;
				sum+=s.getGrade();
			}
		}
		System.out.println(cls+"班的总分为:"+sum+"\\t平均分是:"+(double)sum/i);
	}
}

偶然看见前两天给学弟改的数据结构单链表的代码

#include<stdio.h>
#include<stdlib.h>
#define ok 1
#define error 0
#define overflow -2
typedef int status;
typedef int elemtype;
typedef struct{
     elemtype *elem;
     int length;
     int listsize;
}sqlist;
status initlist(sqlist &L){//创建
     L.elem=(elemtype *)malloc(100*sizeof(elemtype));
     L.length=0;
     L.listsize=100;
     return ok;
}
status delet (sqlist &L,int i,elemtype &e){
     e=L.elem[i-1];
     for(int j=i-1;j<=L.length-2;j++){
          L.elem[j]=L.elem[j+1];
     }
     L.length--;
     return ok;
}
int main(){
     int i;
     elemtype e;
     sqlist L;//这里本来是分号
     initlist(L);
     for(i=1;i<5;i++){
          L.elem[i-1]=3*i;
          L.length++;
     }
     printf("L初始输出\n");
     for(i=0;i<4;i++){//这里的i从0开始
          printf("%d\n",L.elem[i]);
     }
     delet(L,1,e);
     printf("被删除节点%d\n",e);
     printf("L删除后:\n");
     for(i=0;i<L.length;i++){
          printf("%d\n",L.elem[i]);
     }
     return 0;
}


C++的简单教务系统

今天偶然发现大一写的C++的简单教务系统功能代码所以发出来看看

#include<iostream>
#include<string>
#define max 100
using namespace std;
class student    
{
	public:    
        string name;    
        string sex;    
        string add;    
        string course;    
        int grade;    
};    
class zhuce:public student    
{    
    public:    
        zhuce base(zhuce a[])    
        {    
            for(int i=0;i<max;i++)    
            {    
                cout<<"请输入姓名:";    
                cin>>a[i].name; 
                cout<<endl;    
                cout<<"请输入性别:";    
                cin>>a[i].sex;     
                cout<<endl;    
                cout<<"输入家庭住址:";    
                cin>>a[i].add;     
                cout<<endl;    
            }    
        }    
        void subject(zhuce a[])    
        {    
            string b;    
            cout<<"请输入要选课学生的姓名:";    
            cin>>b;
            cout<<endl;    
            for(int i=0;i<=max;i++)    
            {    
                if(a[i].name==b)    
                {    
                    cout<<"请输入已选课:";    
                    cin>>a[i].course;    
                }    
            }    
        }    
        void score(zhuce a[])    
        {    
            string b;    
            cout<<"请输入需要注册成绩学生的姓名:";    
            cin>>b;
            cout<<endl;    
            for(int i=0;i<max;i++)    
            {    
                if(a[i].name==b)    
                {    
                    cout<<"请输入学生成绩:";    
                    cin>>a[i].grade;
                    cout<<endl;    
                }    
            }    
        }    
        void prin(student a[])    
        {    
            string b;    
            cout<<"请输入需要查询学生的姓名:";    
            cin>>b;
            cout<<endl;    
            for(int i=0;i<max;i++)    
            {    
                if(a[i].name==b)    
                {    
                    cout<<a[i].name<<endl<<a[i].sex<<endl<<a[i].add<<endl<<a[i].course<<endl<<a[i].grade;    
                }    
            }    
        }    
};    
int main()    
{    
    zhuce a[1000];    
    int b;
    cout<<" ╔---------------------------------------╗"<<endl;         
    cout<<" ┆ 输入学生信息----1  输入学生选课------2 	┆"<<endl;     
    cout<<" ┆ 输入学生成绩----3  输出学生信息------4    ┆"<<endl;     
    cout<<" ┆ 退出程序------5  				    	┆"<<endl;     
    cout<<" ╚---------------------------------------╝"<<endl;     
    cout<<"请输入所要进行的操作序号: ";     
    cin>>b;       
    do{    
        switch(b)    
        {
        	case 1:a[].base();break;
        	case 2:a[].subject();break;    
        	case 3:a[].score();break;    
        	case 4:a[].prin();break;    
        	case 5:break;    
        	default:cout<<"错误选择!请重选"<<endl;break;     
        }    
             
    }while(b!=5);
    return 0;
}    

归并排序

归并排序(MERGE-SORT)是建立在归并操作上的一种有效的排序算法,该算法是采用分治法(Divide and Conquer)的一个非常典型的应用。将已有序的子序列合并,得到完全有序的序列;即先使每个子序列有序,再使子序列段间有序。若将两个有序表合并成一个有序表,称为二路

#include<stdio.h>
typedef int Elemtype;
void Merge(Elemtype a[],Elemtype b[],int startindex,int midindex,int endindex){//子序列的排序
    int i=startindex,j=midindex+1,k=startindex;
    while(i<=midindex&&j<=endindex){
        if(a[i]>a[j]){
            b[k++]=a[i++];
        }else{
            b[k++]=a[j++];
        }
    }
    while(i<=midindex){
        b[k++]=a[i++];
    }
    while(j<=endindex){
        b[k++]=a[j++];
    }
    for(i=startindex;i<=endindex;i++){
        a[i]=b[i];
    }
}
void MergeSort(Elemtype a[],Elemtype b[],int startindex,int endindex){//待排序序列分组
    int midindex;
    if(startindex<endindex){
        midindex=(startindex+endindex)/2;
        MergeSort(a,b,startindex,midindex);
        MergeSort(a,b,midindex+1,endindex);
        Merge(a,b,startindex,midindex,endindex);
    }
}
int main(){
    Elemtype a[11];
    Elemtype b[11];
    int len =10;
    for(int i=0;i<len;i++){
        scanf("%d",&a[i]);
    }
    MergeSort(a,b,0,len-1);
    for(int i=0;i<len;i++){
        printf("%d=%d\n",i,a[i]);
    }
    return 0;
}

堆排序

很久没更了,来一发

堆排序主要用到了大顶堆和小顶堆,建堆后将堆定元素和未排序序列最后一个元素交换则已排序序列多一个直到号元素结束

#include<stdio.h>
typedef int Elemtype;
//建立大顶堆
void HeapAdjust(Elemtype a[],int i,int len){//i是待调整为大顶堆数组的起点,len结束
    int child;
    int temp;
    for(;i*2+1<len;i=child){
        child=i*2+1;//子节点位置
        if(child<len-1&&a[child+1]>a[child])
            child++;//获得左右孩子中大的位置
        if(a[i]<a[child]){//如果比父节点打就和父节点替换
            temp=a[i];
            a[i]=a[child];
            a[child]=temp;
        }else
            break;//否则退出;
    }
}
//用大顶堆开始堆排序
void HeapSort(Elemtype a[],int len){
    int i;
    int temp;
    //调整序列前半部分成大顶堆,第一个元素为最大值
    for(i=(len/2-1);i>=0;i--){
        HeapAdjust(a,i,len);
    }
    for(i=len-1;i>0;i--){
        temp=a[0];
        a[0]=a[i];
        a[i]=temp;
        HeapAdjust(a,0,i);//i是因为后面n-i个已经有序
    }
}
int main(){
    Elemtype a[11];
    int len =10;
    for(int i=0;i<len;i++){
        scanf("%d",&a[i]);
    }
    HeapSort(a,len);
    for(int i=0;i<len;i++){
        printf("%d=%d\n",i,a[i]);
    }
    return 0;
}


java计算器

import java.awt.*;
import java.awt.event.*;
import java.applet.Applet;
import javax.swing.*;
public class apl extends Applet implements ActionListener 
{
 double a=0,b=0;
 char c=' ';
 JTextField field1=new JTextField(18);
 JTextField field2=new JTextField(18);
 JButton button1=new JButton("+");
 JButton button2=new JButton("-");
 JButton button3=new JButton("*");
 JButton button4=new JButton("/");
 JButton button5=new JButton("平方");
 JButton button6=new JButton("开平方");
 JButton button7=new JButton("=");
 JButton button8=new JButton("重置");
 public void init() 
 {  // 初始化
  this.setSize(250, 200);
  add(field2);    add(field1);    
  add(button1);   add(button2);   
  add(button3);   add(button4);   
  add(button5);   add(button6);   
  add(button7);   add(button8);
  button1.addActionListener(this);
  button2.addActionListener(this);
  button3.addActionListener(this);
  button4.addActionListener(this);
  button5.addActionListener(this);
  button6.addActionListener(this);
  button7.addActionListener(this);
  button8.addActionListener(this);
  }
  
//加
  public void dobutton1()//加
  {  // 处理按钮事件
   if(a==0&&c==' ')
   {
    a=Double.parseDouble(field1.getText());
   }
   else
   {
    b=Double.parseDouble(field1.getText());
    if(c=='+')
    {
     a+=b;
    }
    if(c=='-')
    {
     a-=b;
    }
    if(c=='*')
    {
     a*=b;
    }
    if(c=='/')
    {
     a/=b;
    }
    if(c=='^')
    {
     a*=a;
    }
    if(c=='&')
    {
     a=Math.sqrt(a);
    }
   }
   c='+';
   field2.setText(field2.getText()+Double.toString(b==0?a:b)+c);
   field1.setText("  ");
  }
  public void dobutton2()//减
  {  // 处理按钮事件
   if(a==0&&c==' ')
   {
    a=Double.parseDouble(field1.getText());
   }
   else
   {
   b=Double.parseDouble(field1.getText());
   if(c=='+')
   {
    a+=b;
  }
   if(c=='-')
    {
     a-=b;
    }
    if(c=='*')
    {
     a*=b;
    }
    if(c=='/')
    {
     a/=b;
    }
    if(c=='^')
    {
     a*=a;
    }
    if(c=='&')
    {
     a=Math.sqrt(a);
    }
   }
   c='-';
   field2.setText(field2.getText()+Double.toString(b==0?a:b)+c);
   field1.setText("  ");
  }
  public void dobutton3()//乘
  {  // 处理按钮事件
   if(a==0&&c==' ')
   {
    a=Double.parseDouble(field1.getText());
   }
   else
   {
   b=Double.parseDouble(field1.getText());
   if(c=='+')
   {
    a+=b;
   }
   if(c=='-')
    {
     a-=b;
    }
    if(c=='*')
    {
     a*=b;
    }
    if(c=='/')
    {
     a/=b;
    }
    if(c=='^')
    {
     a*=a;
    }
    if(c=='&')
    {
     a=Math.sqrt(a);
    }
   }
   c='*';
   field2.setText(field2.getText()+Double.toString(b==0?a:b)+')'+c);
   field1.setText("  ");
  }
  public void dobutton4()//除
  {  // 处理按钮事件
   if(a==0&&c==' ')
   {
    a=Double.parseDouble(field1.getText());
   }
   else
   {
   b=Double.parseDouble(field1.getText());
   if(c=='+')
   {
    a+=b;
  }
   if(c=='-')
    {
     a-=b;
    }
    if(c=='*')
    {
     a*=b;
    }
    if(c=='/')
    {
     a/=b;
    }
    if(c=='^')
    {
     a*=a;
    }
    if(c=='&')
    {
     a=Math.sqrt(a);
    }
   }
   c='/';
   field2.setText(field2.getText()+Double.toString(b==0?a:b)+')'+c);
   field1.setText("  ");
  }
  public void dobutton5()//平方
  {  // 处理按钮事件
   if(a==0&&c==' ')
   {
    a=Double.parseDouble(field1.getText());
   }
   else
   {
   b=Double.parseDouble(field1.getText());
   if(c=='+')
   {
    a+=b;
  }
   if(c=='-')
    {
     a-=b;
    }
    if(c=='*')
    {
     a*=b;
    }
    if(c=='/')
    {
     a/=b;
    }
    if(c=='^')
    {
     a*=a;
    }
    if(c=='&')
    {
     a=Math.sqrt(a);
    }
   }
   c='^';
   field2.setText(field2.getText()+Double.toString(b==0?a:b)+c);
   field1.setText("  ");
  }
  public void dobutton6()//开方
  {  // 处理按钮事件
   if(a==0&&c==' ')
   {
    a=Double.parseDouble(field1.getText());
   }
   else
   {
   b=Double.parseDouble(field1.getText());
   if(c=='+')
   {
    a+=b;
  }
   if(c=='-')
    {
     a-=b;
    }
    if(c=='*')
    {
     a*=b;
    }
    if(c=='/')
    {
     a/=b;
    }
    if(c=='^')
    {
     a*=a;
    }
    if(c=='&')
    {
     a=Math.sqrt(a);
    }
   }
   c='&';
   field2.setText(field2.getText()+Double.toString(b==0?a:b)+c);
   field1.setText("  ");
  }
  public void dobutton7()//=
  {
   if(a==0&&c==' ')
   {
    a=Double.parseDouble(field1.getText());
   }
   else
   {
     if(c=='+')
     {
      b=Double.parseDouble(field1.getText());
      a+=b;
      }
     if(c=='-')
     {
      b=Double.parseDouble(field1.getText());
      a-=b;
      }
     if(c=='*')
     {
      b=Double.parseDouble(field1.getText());
      a*=b;
      }
     if(c=='/')
     {
      b=Double.parseDouble(field1.getText());
      a/=b;
      }
     }
    if(c=='^')
    {
     a=a*a;
    }
    if(c=='&')
    {
     a=Math.sqrt(a);
    }
   
   field1.setText(a+"");
   field2.setText(field2.getText()+Double.toString(b==0?a:b)+'=');
   b=0;
   c=' ';
  }
  public void dobutton8()//重置
  {  // 处理按钮事件
   field1.setText(" ");
   field2.setText(" ");
   c=' ';
   a=0;
   b=0;
  }
//事件源处理
public void actionPerformed(ActionEvent e)
{
 Object obj;
 obj=e.getSource();
 if(obj==button1)
 {
  dobutton1();
 }else if(obj==button2)
 {
  dobutton2();
 }else if(obj==button3)
 {
  dobutton3();
 }else if(obj==button4)
 {
  dobutton4();
 }else if(obj==button5)
 {
   dobutton5();
 }else if(obj==button6)
 {
  dobutton6();
 }else if(obj==button7)
 {
   dobutton7();
 }else if(obj==button8)
 {
   dobutton8();
 }
}
}