前言:刚刚学了一段时间的C++,上星期组长说这星期完成对C语言课设的改编,虽然后面倒真没有要求一定要做完,说是按照进度学习算了,不着急做课程设计!不过我觉得一直做个课程设计不失为一种好的学习方法,所以我就把类和基本的文件操作看了一遍就尝试着改了一下!其实改得很简单,源代码本身也由于水平有限改得比较冗杂!不过现在水平是这样。说实话现在倒也觉得还行,起码对类有了更加深刻的印象。不过以后把书看得更完全还要试一下其他的课程设计。个人觉得课程设计真的对学习蛮有用的!(在此吐槽一下,改得没有一点技术含量! ! !)
源代码:
<span style="font-size:14px;">#include<iostream> #include<fstream> #include<stdlib.h> //清屏函数头文件 #include<cstring> #include<iomanip> using namespace std; struct Student_information { double number; char name[20]; char sex[10]; double Class; double C; double english; double math; double all; double average; int ranking; }; //源代码总类 class Student { public: Student(); void Student_menu_begin(); void Student_menu_normal(); void Student_add(); void Student_show(); void Student_modify(); void Student_delete(); void Student_sort(); void Student_find(); void Student_flush(); private: double number; char name[20]; char sex[10]; double Class; double C; double english; double math; double all; double average; int ranking; }; Student::Student() { number=0; strcpy(name," "); strcpy(sex," "); Class=0; C=0; english=0; math=0; } //菜单函数1 void Student::Student_menu_begin() { cout<<"请根据目录选择你想要进行的操作:"<<endl; cout<<"\t\t"<<"********" <<"【0】退出程序"<<"********"<<endl; cout<<"\t\t"<<"********" <<"【1】添加学生"<<"********"<<endl; cout<<"\t\t"<<"********" <<"【2】显示学生"<<"********"<<endl; cout<<"\t\t"<<"********" <<"【3】修改学生"<<"********"<<endl; cout<<"\t\t"<<"********" <<"【4】删除学生"<<"********"<<endl; cout<<"\t\t"<<"********" <<"【5】成绩排序"<<"********"<<endl; cout<<"\t\t"<<"********" <<"【6】查找学生"<<"********"<<endl; cout<<"\t\t"<<"********" <<"【7】清空数据"<<"********"<<endl; cout<<"\t\t"<<"如果是第一次使用该程序,请按【1】选择添加学生"<<endl; cout<<"进行操作:"<<" "; } //菜单函数2 void Student::Student_menu_normal() { cout<<"请根据目录选择你想要进行的操作:"<<endl; cout<<"\t\t"<<"********" <<"【0】退出程序"<<"********"<<endl; cout<<"\t\t"<<"********" <<"【1】添加学生"<<"********"<<endl; cout<<"\t\t"<<"********" <<"【2】显示学生"<<"********"<<endl; cout<<"\t\t"<<"********" <<"【3】修改学生"<<"********"<<endl; cout<<"\t\t"<<"********" <<"【4】删除学生"<<"********"<<endl; cout<<"\t\t"<<"********" <<"【5】成绩排序"<<"********"<<endl; cout<<"\t\t"<<"********" <<"【6】查找学生"<<"********"<<endl; cout<<"\t\t"<<"********" <<"【7】清空数据"<<"********"<<endl; cout<<"\t"<<"进行操作:"<<" "; } //添加学生 void Student::Student_add() { system("cls"); struct Student_information news[50]; int n=1,i=0,number=0; while(n==1) { cout<<"请输入添加学生的学号:"<<endl; cin>>news[0].number; cout<<"请输入添加学生的名字:"<<endl; cin>>news[0].name; cout<<"请输入添加学生的性别:"<<endl; cin>>news[0].sex; cout<<"请输入添加学生的班级:"<<endl; cin>>news[0].Class; cout<<"请输入添加学生的C语言成绩:"<<endl; cin>>news[0].C; cout<<"请输入添加学生的英语成绩:"<<endl; cin>>news[0].english; cout<<"请输入添加学生的数学成绩:"<<endl; cin>>news[0].math; system("cls"); ofstream outfile; outfile.open("C++信息文件.txt",ios::app); outfile<<news[0].number<<" "; outfile<<news[0].name<<" "; outfile<<news[0].sex<<" "; outfile<<news[0].Class<<" "; outfile<<news[0].C<<" "; outfile<<news[0].english<<" "; outfile<<news[0].math<<" "; outfile.close(); ifstream infile("C++计数文件.txt",ios::in); infile>>number; infile.close(); number=number+1; outfile.open("C++计数文件.txt",ios::out); outfile<<number<<" "<<endl; outfile.close(); cout<<"请问你要继续输入吗?继续输入,请按1,否则请按0"<<endl; cin>>n; system("cls"); } } //显示函数 void Student::Student_show() { system("cls"); int number; struct Student_information news[50]; ifstream infile1("C++计数文件.txt",ios::in); infile1>>number; infile1.close() ; ifstream infile("C++信息文件.txt",ios::in); for(int i=0;i<number;i++) { infile>>news[i].number; infile>>news[i].name; infile>>news[i].sex; infile>>news[i].Class; infile>>news[i].C; infile>>news[i].english; infile>>news[i].math; } infile.close() ; cout<<"文件中储存的信息如下:"<<endl<<endl; cout<<setiosflags(ios::left); cout<<setw(10)<<"学号"; cout<<setw(10)<<"姓名"; cout<<setw(10)<<"性别"; cout<<setw(10)<<"班级"; cout<<setw(10)<<"C语言成绩"; cout<<setw(10)<<"英语成绩"; cout<<setw(10)<<"高数成绩"; cout<<endl; for(int i=0;i<number;i++) { cout<<setiosflags(ios::left); cout<<setw(9)<<news[i].number<<" "; cout<<setw(9)<<news[i].name<<" "; cout<<setw(9)<<news[i].sex<<" "; cout<<setw(9)<<news[i].Class<<" "; cout<<setw(9)<<news[i].C<<" "; cout<<setw(9)<<news[i].english<<" "; cout<<setw(9)<<news[i].math<<" "; cout<<endl; } cout<<endl<<endl; } //修改函数 void Student::Student_modify() { double num; int data; system("cls"); int number; struct Student_information news[50]; ifstream infile1("C++计数文件.txt",ios::in); infile1>>number; infile1.close() ; ifstream infile("C++信息文件.txt",ios::in); for(int i=0;i<number;i++) { infile>>news[i].number; infile>>news[i].name; infile>>news[i].sex; infile>>news[i].Class; infile>>news[i].C; infile>>news[i].english; infile>>news[i].math; } infile.close() ; int n=1; while(n==1) { Student q1; q1.Student_show(); cout<<endl; cout<<"请输入所要修改学生的学号:"; cin>>num; cout<<endl; int w=1; for(int i=0;i<number;i++) if(num==news[i].number) { data=i; break; } else if(i==number-1) { cout<<"输入的数据有误,文件中没有储存相关信息!"<<endl<<endl;; w=0; } if(w==1) { cout<<"请输入修改后学生的学号:"<<endl; cin>>news[data].number; cout<<"请输入修改后学生的名字:"<<endl; cin>>news[data].name; cout<<"请输入修改后学生的性别:"<<endl; cin>>news[data].sex; cout<<"请输入修改后学生的班级:"<<endl; cin>>news[data].Class; cout<<"请输入修改后学生的C语言成绩:"<<endl; cin>>news[data].C; cout<<"请输入修改后学生的英语成绩:"<<endl; cin>>news[data].english; cout<<"请输入修改后学生的数学成绩:"<<endl; cin>>news[data].math; system("cls"); ofstream outfile; outfile.open("C++信息文件.txt",ios::out); for(int i=0;i<number;i++) { outfile<<news[i].number<<" "; outfile<<news[i].name<<" "; outfile<<news[i].sex<<" "; outfile<<news[i].Class<<" "; outfile<<news[i].C<<" "; outfile<<news[i].english<<" "; outfile<<news[i].math<<" "; } outfile.close() ; cout<<"数据成功修改!"<<endl<<endl; } cout<<"请问还需要继续修改吗?如果继续请按【1】,否者请按【0】:"; cin>>n; } } //删除函数 void Student::Student_delete() { system("cls"); Student q1; q1.Student_show(); int number; struct Student_information news[50]; ifstream infile1("C++计数文件.txt",ios::in); infile1>>number; infile1.close() ; ifstream infile("C++信息文件.txt",ios::in); for(int i=0;i<number;i++) { infile>>news[i].number; infile>>news[i].name; infile>>news[i].sex; infile>>news[i].Class; infile>>news[i].C; infile>>news[i].english; infile>>news[i].math; } infile.close() ; int y=1; while(y==1) { double num1,data,num2=1; cout<<"请输入想要删除学生的学号:"; cin>>num1; for(int i;i<number;i++) if(num1==news[i].number) { data=i; break; } else if(i==number-1) { num2=0; cout<<endl; cout<<"您输入的数据有误!数据文件没有此数据!"<<endl<<endl; } if(num2==1) { for(int i=data;i<number-1;i++) { news[i].number=news[i+1].number; strcpy(news[i].name,news[i+1].name); strcpy(news[i].sex,news[i+1].sex); news[i].Class=news[i+1].Class; news[i].C=news[i+1].C; news[i].english=news[i+1].english; news[i].math=news[i+1].math; } number=number-1; ofstream outfile; outfile.open("C++计数文件.txt",ios::out); outfile<<number; outfile.close() ; outfile.open("C++信息文件.txt",ios::out); for(int i=0;i<number;i++) { outfile<<news[i].number<<" "; outfile<<news[i].name<<" "; outfile<<news[i].sex<<" "; outfile<<news[i].Class<<" "; outfile<<news[i].C<<" "; outfile<<news[i].english<<" "; outfile<<news[i].math<<" "; } outfile.close() ; cout<<"目标数据删除成功!"<<endl<<endl; } cout<<"请问还需继续删除数据吗?若果需要,请按【1】,否者请按【0】:"; cin>>y; cout<<endl; } } //成绩排序 void Student::Student_sort() { system("cls"); int number; struct Student_information news[50]; ifstream infile1("C++计数文件.txt",ios::in); infile1>>number; infile1.close() ; ifstream infile("C++信息文件.txt",ios::in); for(int i=0;i<number;i++) { infile>>news[i].number; infile>>news[i].name; infile>>news[i].sex; infile>>news[i].Class; infile>>news[i].C; infile>>news[i].english; infile>>news[i].math; } infile.close() ; for(int i=0;i<number;i++) { news[i].all=news[i].C+news[i].english+news[i].math; news[i].average=news[i].all/number; } double temp; char character[20]; int temp2; for(int i=0;i<number;i++) for(int x=0;x<number-i-1;x++) if(news[x].all<news[x+1].all) { temp=news[x].number; news[x].number=news[x+1].number; news[x+1].number=temp; strcpy(character,news[x].name); strcpy(news[x].name,news[x+1].name); strcpy(news[x+1].name,character); strcpy(character,news[x].sex); strcpy(news[x].sex,news[x+1].sex); strcpy(news[x+1].sex,character); temp=news[x].Class; news[x].Class=news[x+1].Class; news[x+1].Class=temp; temp=news[x].C; news[x].C=news[x+1].C; news[x+1].C=temp; temp=news[x].english; news[x].english=news[x+1].english; news[x+1].english=temp; temp=news[x].math; news[x].math=news[x+1].math; news[x+1].math=temp; temp=news[x].all; news[x].all=news[x+1].all; news[x+1].all=temp; temp2=news[x].average; news[x].average=news[x+1].average; news[x+1].average=temp2; } for(int i=0;i<number;i++) news[i].ranking=i+1; cout<<"文件中学生数据的排名如下:"<<endl; cout<<endl; cout<<setiosflags(ios::left); cout<<setw(10)<<"学号"; cout<<setw(10)<<"姓名"; cout<<setw(10)<<"性别"; cout<<setw(10)<<"班级"; cout<<setw(10)<<"C语言成绩"; cout<<setw(10)<<"英语成绩"; cout<<setw(10)<<"高数成绩"; cout<<setw(10)<<"总成绩"; cout<<setw(10)<<"平均分"; cout<<setw(10)<<"排名"; cout<<endl<<endl; for(int i=0;i<number;i++) { cout<<setiosflags(ios::left); cout<<setw(9)<<news[i].number<<" "; cout<<setw(9)<<news[i].name<<" "; cout<<setw(9)<<news[i].sex<<" "; cout<<setw(9)<<news[i].Class<<" "; cout<<setw(9)<<news[i].C<<" "; cout<<setw(9)<<news[i].english<<" "; cout<<setw(9)<<news[i].math<<" "; cout<<setw(9)<<setiosflags(ios::fixed)<<setprecision(2)<<news[i].all<<" "; cout<<setw(9)<<news[i].average<<" "; cout.unsetf(ios::fixed); cout<<setprecision(10); cout<<setw(10)<<news[i].ranking<<" "; cout<<endl; cout<<endl; } cout<<endl<<endl<<endl; } //查找函数 void Student::Student_find() { system("cls"); int number; struct Student_information news[50]; ifstream infile1("C++计数文件.txt",ios::in); infile1>>number; infile1.close() ; ifstream infile("C++信息文件.txt",ios::in); for(int i=0;i<number;i++) { infile>>news[i].number; infile>>news[i].name; infile>>news[i].sex; infile>>news[i].Class; infile>>news[i].C; infile>>news[i].english; infile>>news[i].math; } infile.close() ; int x=1; while(x==1) { system("cls"); double num; cout<<"请输入要查询学生的学号:"; cin>>num; cout<<endl; for(int i=0;i<number;i++) if(num==news[i].number) { cout<<"查询的基本信息为:"<<endl<<endl; cout<<setiosflags(ios::left); cout<<setw(10)<<"学号"; cout<<setw(10)<<"姓名"; cout<<setw(10)<<"性别"; cout<<setw(10)<<"班级"; cout<<setw(10)<<"C语言成绩"; cout<<setw(10)<<"英语成绩"; cout<<setw(10)<<"高数成绩"; cout<<endl<<endl; cout<<setw(9)<<news[i].number<<" "; cout<<setw(9)<<news[i].name<<" "; cout<<setw(9)<<news[i].sex<<" "; cout<<setw(9)<<news[i].Class<<" "; cout<<setw(9)<<news[i].C<<" "; cout<<setw(9)<<news[i].english<<" "; cout<<setw(9)<<news[i].math<<" "; cout<<endl<<endl; break; } else if(i==number-1) cout<<"文件中未查询到有关信息!"<<endl; cout<<"请问还需要查询相关的信息吗?如果是请按【1】,否则请按【0】:"; cin>>x; } } //清空函数 void Student::Student_flush() { system("cls"); char aids[20]; cout<<endl; cout<<"\t"<<"警告:这是格式化操作,将清空文件中所储存的数据! ! !O(≧口≦)O"<<endl<<endl; cout<<"\t"<<"请输入:“确认操作”"<<" "<<"执行此操作"<<endl; cout<<"\t"<<":"; cin>>aids; if(strcmp(aids,"确认操作")==0) { ofstream outfile; outfile.open("C++计数文件.txt",ios::out); outfile.close() ; outfile.open("C++信息文件.txt",ios::out); outfile.close() ; cout<<"\t"<<"文件中所储存的数据已被清空!"<<endl; } else cout<<"\t"<<"文件中数据未被删除!"<<endl; cout<<endl; } //主函数 int main() { system("color 9F"); int n; Student p1; p1.Student_menu_begin(); while(1) { cin>>n; switch(n) { case 0: return 0; case 1: p1.Student_add(); p1.Student_menu_normal(); break; case 2: p1.Student_show(); p1.Student_menu_normal(); break; case 3: p1.Student_modify(); p1.Student_menu_normal(); break; case 4: p1.Student_delete(); p1.Student_menu_normal(); break; case 5: p1.Student_sort(); p1.Student_menu_normal(); break; case 6: p1.Student_find(); p1.Student_menu_normal(); break; case 7: p1.Student_flush(); p1.Student_menu_normal(); break; } } }</span>备注:用的编译器为Dev-C++