C++课程设计--学生信息管理系统
尽管老套,可是非常经典,于是就把自己写的代码放上面了,以后也许还能看到当初的幼稚 :).
本代码实现了对学生信息的创建,改动,删除,排序,文件保存等基本操作,通过文件实现了多个用户的多个password登录
附代码:
<strong>#include<iostream> #include<cstring> #include<string> #include<fstream> #include<algorithm> #include<iomanip> #include<cstdio> #include<cstdlib> #include<conio.h> #include<windows.h> using namespace std; class student { private: long long num; char name[20]; char sex[6]; int age; char phone[20]; public: static int NUM; student() {} ~student() {} void input(); friend void readin(); friend void show(); friend void mood(); friend void del(); friend void soort(); friend void save(); friend void findyou(); friend void clear1(); } zhuo_yue[100]; int student::NUM=0; void readin() { ifstream in("inf.txt",ios::binary); int i=1; while(!in.eof()) { in.read((char*) &zhuo_yue[i],sizeof(zhuo_yue[i])); i++; } in.close(); student::NUM=i-2; } void show() { if(student::NUM==0) cout<<endl<<endl<<setw(10)<<"暂无学生信息"; else { cout<<"学生信息显示"<<endl<<endl; cout<<"学号"<<setw(15)<<"姓名"<<setw(15) <<"性别"<<setw(15)<<"年龄"<<setw(15)<<"电话" <<endl; for(int i=1; i<=student::NUM; i++) { cout<<zhuo_yue[i].num<<setw(15)<<zhuo_yue[i].name<<setw(15) <<zhuo_yue[i].sex<<setw(15)<<zhuo_yue[i].age<<setw(15)<<zhuo_yue[i].phone <<endl; } } cout<<endl<<setw(40)<<"按随意键退出"; getch(); } void mood() { long long L; cout<<"请输入欲改动的学号:"; cin>>L; cout<<endl<<setw(40)<<"确认改动(y/n)"; if(getch()=='y') { system("cls"); for(int i=1; i<=student::NUM; i++) if(L==zhuo_yue[i].num) { cout<<"学生信息改动"<<endl<<endl; cout<<"学号:"; cin>>zhuo_yue[i].num; cout<<endl<<endl; cout<<"姓名:"; cin>>zhuo_yue[i].name; cout<<endl<<endl; cout<<"性别:"; cin>>zhuo_yue[i].sex; cout<<endl<<endl; cout<<"年龄:"; cin>>zhuo_yue[i].age; cout<<endl<<endl; cout<<"电话:"; cin>>zhuo_yue[i].phone; cout<<endl<<endl; break; } cout<<endl<<setw(40)<<"已改动,按随意键退出"; getch(); } else return; } void del() { int sum=0; cout<<setw(50)<<"[1]按学号删除 [2]按姓名删除"<<endl; if(getch()=='1') { cout<<"请输入学号:"; long long it; cin>>it; cout<<endl<<setw(30)<<"您确定删除此学生吗?(y/n)"; if(getch()=='y') { system("cls"); for(int i=1; i<=student::NUM; i++) { if(zhuo_yue[i].num==it) { for(int j=i+1; j<=student::NUM; j++) zhuo_yue[j-1]=zhuo_yue[j]; student::NUM--; sum++; break; } } cout<<"共删除"<<sum<<"人"; } else return; } else if(getch()=='2') { char name1[20]; cout<<"请输入姓名:"; cin>>name1; cout<<endl<<setw(30)<<"您确定删除此学生吗?(y/n)"; if(getch()=='y') { system("cls"); for(int i=1; i<=student::NUM; i++) { if(strcmp(zhuo_yue[i].name,name1)==0) { for(int j=i+1; j<=student::NUM; j++) zhuo_yue[j-1]=zhuo_yue[j]; student::NUM--; sum++; } } cout<<"共删除"<<sum<<"人"; } else return; } cout<<endl<<endl<<setw(40)<<"按随意键结束"; getch(); } void soort() { cout<<setw(50)<<"[1]按学号排序 [2]按年龄排序"<<endl; if(getch()=='1') { for(int i=1; i<student::NUM; i++) for(int j=1; j<student::NUM-i+1; j++) { if(zhuo_yue[j].num>zhuo_yue[j+1].num) { student it=zhuo_yue[j]; zhuo_yue[j]=zhuo_yue[j+1]; zhuo_yue[j+1]=it; } } cout<<endl<<setw(50)<<"已排序,按随意键退出"; getch(); } else if(getch()=='2') { for(int i=1; i<student::NUM; i++) for(int j=1; j<student::NUM-i+1; j++) { if(zhuo_yue[j].age>zhuo_yue[j+1].age) { student it=zhuo_yue[j]; zhuo_yue[j]=zhuo_yue[j+1]; zhuo_yue[j+1]=it; } } cout<<endl<<setw(50)<<"已排序,按随意键退出"; getch(); } } void student::input() { LI: NUM++; cout<<"学生信息输入"<<endl<<endl; cout<<"学号:"; cin>>zhuo_yue[NUM].num; cout<<endl<<endl; cout<<"姓名:"; cin>>zhuo_yue[NUM].name; cout<<endl<<endl; cout<<"性别:"; cin>>zhuo_yue[NUM].sex; cout<<endl<<endl; cout<<"年龄:"; cin>>zhuo_yue[NUM].age; cout<<endl<<endl; cout<<"电话:"; cin>>zhuo_yue[NUM].phone; cout<<endl<<endl; cout<<"已输入,是否继续 (y/n)"; if(getch()=='y') { system("cls"); goto LI; } else return; } void save() { ofstream out("inf.txt",ios::binary); cout<<endl<<setw(40)<<"已保存。按随意键退出"; for(int i=1; i<=student::NUM; i++) { out.write((char*)&zhuo_yue[i],sizeof(zhuo_yue[i])); } out.close(); getch(); } void findyou() { int sum=0; cout<<setw(50)<<"[1]按学号查找 [2]按姓名查找"<<endl; if(getch()=='1') { cout<<"请输入学号:"; long long it; cin>>it; cout<<endl<<setw(30)<<"您确定查看此学生吗?(y/n)"; if(getch()=='y') { system("cls"); cout<<"查看学生信息"<<endl<<endl; for(int i=1; i<=student::NUM; i++) { if(zhuo_yue[i].num==it) { cout<<setw(20)<<"学号:"<<zhuo_yue[i].num<<endl <<setw(20)<<"姓名:"<<zhuo_yue[i].name<<endl <<setw(20)<<"性别:"<<zhuo_yue[i].sex<<endl <<setw(20)<<"年龄:"<<zhuo_yue[i].age<<endl <<setw(20)<<"电话:"<<zhuo_yue[i].phone<<endl; cout<<"---------------------------------------"<<endl<<endl; sum++; } } cout<<"共显示"<<sum<<"人"; } else return; } else if(getch()=='2') { char name1[20]; cout<<"请输入姓名:"; cin>>name1; cout<<endl<<setw(30)<<"您确定查看具有此姓名的学生吗?(y/n)"; if(getch()=='y') { system("cls"); cout<<"查看学生信息"<<endl<<endl; for(int i=1; i<=student::NUM; i++) { if(strcmp(zhuo_yue[i].name,name1)==0) { cout<<setw(20)<<"学号:"<<zhuo_yue[i].num<<endl <<setw(20)<<"姓名:"<<zhuo_yue[i].name<<endl <<setw(20)<<"性别:"<<zhuo_yue[i].sex<<endl <<setw(20)<<"年龄:"<<zhuo_yue[i].age<<endl <<setw(20)<<"电话:"<<zhuo_yue[i].phone<<endl; cout<<"---------------------------------------"<<endl<<endl; sum++; } } cout<<"共显示"<<sum<<"人"; } else return; } cout<<endl<<endl<<setw(40)<<"按随意键结束"; getch(); } char mainmenu() { cout<<"\n\n 欢迎进入学生信息管理系统 "<<endl<<endl; cout<<"\n\n [1]管理员 [2]学生"<<endl<<endl; char ff=getch(); return ff; } void adminmainmenu() { cout<<"\n\n 欢迎进入学生信息管理系统(管理者) "<<endl<<endl <<endl <<endl <<" 1: 录入学生信息 "<<endl<<endl <<" 2: 显示学生信息 "<<endl<<endl <<" 3: 改动学生信息 "<<endl<<endl <<" 4: 删除学生信息 "<<endl<<endl <<" 5: 查找学生信息 "<<endl<<endl <<" 6: 保存学生信息 "<<endl<<endl <<" 7: 排序学生信息"<<endl<<endl <<" 8: 返回初始页面"<<endl<<endl <<" 0: 退出系统"<<endl; } void mainmenu1() { cout<<"\n\n 欢迎进入学生信息管理系统(学生) "<<endl<<endl <<endl <<endl <<" 1: 显示学生信息 "<<endl<<endl <<" 2: 查找学生信息 "<<endl<<endl <<" 3: 排序学生信息"<<endl<<endl <<" 4: 返回初始页面"<<endl<<endl <<" 0: 退出系统"<<endl; } void clear1() { fstream cl; cl.open("inf.txt",ios::out); cl.close(); cout<<"销毁成功"; exit(1); } int main() { LIIIII: readin(); system("cls"); char get = mainmenu(); if(get=='1') { char adminname[20],mima[20]; ifstream in; ofstream out; in.open("admin.txt",ios::in); if(!in) { cout<<"无管理员账号,请先设定:"<<endl<<endl; FF: cout<<"输入账号名:"; cin>>adminname; cout<<"输入password:"; cin>>mima; cout<<"确定创建(y/n)"; char fff=getch(); if(fff=='y') { out.open("admin.txt",ios::app); out<<adminname<<' '<<mima<<' '; system("cls"); cout<<" 已创建,按随意键返回"; getch(); out.close(); goto LIIIII; } else if(fff=='n') goto LIIIII; } else { system("cls"); cout<<" 已存在用户"<<endl<<endl; cout<<"是否新建管理员账户(y/n)"; char gr=getch(); if(gr=='y') { system("cls"); goto FF; } ss1: system("cls"); cout<<"登录账号:"; cin>>adminname; cout<<"输入password:"; cin>>mima; char adminname1[20],mima1[20]; int flog=0; while(!in.eof()) { in>>adminname1>>mima1; if(!strcmp(adminname,adminname1)&&!strcmp(mima1,mima)) { flog=0; break; } } if(flog==0) { system("cls"); cout<<" 登陆成功 按随意键继续"; getch(); } else { cout<<"password错误"<<endl<<endl; cout<<"[1]返回初始界面 [2]又一次输入password"; char gg=getch(); if(gg=='1') goto LIIIII; else { system("cls"); goto ss1; } } } while(1) { adminmainmenu(); char ch; ch=getchar(); system("CLS"); switch(ch) { case '1': zhuo_yue[student::NUM].input(); break; case '2': show(); break; case '3': mood(); break; case '4': del(); break; case '5': findyou(); break; case '6': save(); break; case '7': soort(); break; case '8': goto LIIIII; case 'F': clear1(); case '0': cout<<"\n\n\n\n 谢谢使用~~"; exit(1); } } } else if(get=='2') { char stuname[20],stumima[20]; ifstream in; ofstream out; in.open("student.txt",ios::in); if(!in) { cout<<"无学生账号。请先设定:"<<endl<<endl; LL: cout<<"输入账号名:"; cin>>stuname; cout<<"输入password:"; cin>>stumima; cout<<"确定创建(y/n)"; char fff=getch(); if(fff=='y') { out.open("student.txt",ios::app); out<<stuname<<' '<<stumima<<' '; system("cls"); cout<<" 已创建。按随意键返回"; getch(); out.close(); goto LIIIII; } else if(fff=='n') goto LIIIII; } else { system("cls"); cout<<" 已存在用户"<<endl<<endl; cout<<"是否新建账户(y/n)"; char gr=getch(); if(gr=='y') { system("cls"); goto LL; } sss: system("cls"); cout<<"登录账号:"; cin>>stuname; cout<<"输入password:"; cin>>stumima; char stuname1[20],stumima1[20]; int flog=1; while(!in.eof()) { in>>stuname1>>stumima1; if(!strcmp(stuname,stuname1)&&!strcmp(stumima1,stumima)) { flog=0; break; } } if(flog==0) { system("cls"); cout<<" 登陆成功 按随意键继续"; getch(); } else if(flog==1) { system("cls"); cout<<"password错误"<<endl<<endl; cout<<"[1]返回初始界面 [2]又一次输入password"; char gg=getch(); if(gg=='1') goto LIIIII; else { system("cls"); goto sss; } } in.close(); } while(1) { mainmenu1(); char ch; ch=getchar(); system("CLS"); switch(ch) { case '1': show(); break; case '2': findyou(); break; case '3': soort(); break; case 'F': clear1(); case '4': goto LIIIII; case '0': cout<<"\n\n\n\n 谢谢使用~~"; exit(1); } } } } </strong>