学生信息管理系统.cpp(大二上)

2020-04-23 16:00:48来源:博客园 阅读 ()

新老客户大回馈,云服务器低至5折

学生信息管理系统.cpp(大二上)

  #include<iostream> #include<fstream> #include<string> #include<iomanip> #include<conio.h> #include<windows.h> using namespace std; #define M 200 //可以存信息总量 int N=0; //学生总量 class student{ public: student(); void set(); //初始化信息,第一次对信息的录入 void add(); //添加学生信息 void del(); //删除某学生信息 void change(); //修改某学生信息 void search(); //查询某学生信息 void display(); //显示全部学生信息 private: static int num; string name,sex,age,birthday,address,tel,Email; };   class denglu{//定义登录密码类 public: denglu();//构造函数 设置账号、初始密码 int input(); int newpw(); private: string id; char password[10]; };   int student::num=1000; ///1、构造函数 设置账号、初始密码 denglu::denglu() { id = "100001"; for(int i=0;i<6;i++){ password[i] = '1'; } }   ///2、密码输入、校验   int denglu::input() { int i=0,j=0,t,flag=1; char ch,pw[10],a[10]; string id0,dl1; cout<<"退出登录界面请输入“-1 ”,否则输入任意数字"<<endl; cin>>flag; if(flag == -1) exit(0); //cout<<"请输入管理员账号,密码:\n"; cout<<"输入管理员账号:"; cin>>id0; cout<<"输入六位数密码:"; while((ch=getch())!='\r') {//不回显函数。getch()函数输入字符时不显示出来,其他功能同getchar() if(ch!=8) { //不是退格(退格键的ASCII码为:8)就录入 pw[i]=ch; cout<<'*';//并且输出*号 i++; } else { cout<<'\b';//让光标回退一位,下一次输入字符时,会把光标所在位置的字符替换掉 cout<<' ';//再显示空格符把刚才的*给盖住, cout<<'\b';//然后再回撤一格等待录入。 i--; } } pw[i]='\0'; system("cls"); //判断账号密码是否正确 if(id0 != id){ cout<<"\n账号错误!"<<endl; return 0; } for(i=0;i<6;i++){ if(pw[i] != password[i]){ cout<<"\n密码错误!"<<endl; return 0; } } return 1; }   ///3、密码修改 int denglu::newpw() { int j=0; char c; cout<<"\n请输入六位新密码:"; while((c=getch())!='\r') {//getch()函数输入字符时不显示出来,其他功能同getchar() if(c!=8) { //不是回(回退键的ASCII码为:8)就录入 password[j]=c; cout<<'*';//并且输出*号 j++; } else { cout<<'\b';//这里是删除一个,通过输出回撤符 /b,回撤一格, cout<<' ';//再显示空格符把刚才的*给盖住, cout<<'\b';//然后再回撤一格等待录入。 j--; } } system("cls"); cout<<"\n密码修改成功!"<<endl; return 0; }   student::student() {} ///4、初始化文件   void student::set() { int i; string setname,setsex,setage,setbirthday,setaddress,settel,setEmail; ofstream outfile; outfile.open("f1.txt",ios::out); //----打开文件---- if(!outfile) { cerr<<" open error"<<endl; exit(1); } //----依次录入相应个数的学生信息---- for(i=0;i<M;i++) { cout<<"当姓名输入为'0'时,停止输入!!!"<<endl; cout<<"请输入姓名:"<<endl; cin>>setname; name=setname; if(setname=="0") break; if(setname!="0") { N++; num++; outfile<<num<<" "; outfile<<setname<<" "; cout<<"请输入性别:"<<endl; cin>>setsex; sex=setsex; outfile<<setsex<<" "; cout<<"请输入年龄:"<<endl; cin>>setage; age=setage; outfile<<setage<<" "; cout<<"请输入出生日期:"<<endl; cin>>setbirthday; birthday=setbirthday; outfile<<setbirthday<<" "; cout<<"请输入家庭住址:"<<endl; cin>>setaddress; address=setaddress; outfile<<setaddress<<" "; cout<<"请输入电话:"<<endl; cin>>settel; tel=settel; outfile<<settel<<" "; cout<<"请输入电子邮箱:"<<endl; cin>>setEmail; Email=setEmail; outfile<<setEmail<<" "<<endl; } } outfile.close(); //----关闭文件---- }   ///5、添加文件 void student::add() { string addname,addsex,addage,addbirthday,addaddress,addtel,addEmail;   //----以追加的方式录入信息,直接将信息追加到以前文件的末尾---- ofstream outfile("f1.txt",ios::app); if(!outfile) { cerr<<" open error"<<endl; exit(1); } N=N+1; //----依次输入相应的数据,并且追加到之前的文件中去---- cout<<"请输入您要添加的数据"<<endl; num++; outfile<<num<<" "; cout<<"请输入姓名:"<<endl; cin>>addname; outfile<<addname<<" "; cout<<"请输入性别:"<<endl; cin>>addsex; outfile<<addsex<<" "; cout<<"请输入年龄:"<<endl; cin>>addage; outfile<<addage<<" "; cout<<"请输入出生日期:"<<endl; cin>>addbirthday; outfile<<addbirthday<<" "; cout<<"请输入家庭住址:"<<endl; cin>>addaddress; outfile<<addaddress<<" "; cout<<"请输入电话:"<<endl; cin>>addtel; outfile<<addtel<<" "; cout<<"请输入电子邮件:"<<endl; cin>>addEmail; outfile<<addEmail<<endl; outfile.close(); cout<<"已添加成功!"<<endl; }   ///6、删除文件 void student::del() { int k,i; int num[M]; string name[M],sex[M],age[M],birthday[M],address[M],tel[M],Email[M]; ifstream infile("f1.txt",ios::in); if(!infile) { cerr<<" open error"<<endl; exit(1); } for(i=0;i<N;i++) { cout<<" "; infile>>num[i]; cout<<setw(4)<<num[i]; infile>>name[i]; cout<<setw(7)<<name[i]; infile>>sex[i]; cout<<setw(6)<<sex[i]; infile>>age[i]; cout<<setw(6)<<age[i]; infile>>birthday[i]; cout<<setw(12)<<birthday[i]; infile>>address[i]; cout<<setw(11)<<address[i]; infile>>tel[i]; cout<<setw(14)<<tel[i]; infile>>Email[i]; cout<<setw(18)<<Email[i]<<endl; } cout<<"您想删除第几个学生信息?"<<endl; cin>>k; k=k-1; infile.close(); ofstream outfile("f1.txt",ios::out); if(!outfile) { cerr<<" open error"<<endl; exit(1); //退出程序 } //----将数组存取的信息输出到文件中去---- for(i=0;i<N;i++) { if(i!=k) { outfile<<num[i]<<" "; outfile<<name[i]<<" "; outfile<<sex[i]<<" "; outfile<<age[i]<<" "; outfile<<birthday[i]<<" "; outfile<<address[i]<<" "; outfile<<tel[i]<<" "; outfile<<Email[i]<<" "<<endl; } } outfile.close(); N=N-1; cout<<"已删除成功!"<<endl; }   ///7、修改文件   void student::change() { int i,k; int num[M]; string name[M],sex[M],age[M],birthday[M],address[M],tel[M],Email[M]; ifstream infile("f1.txt",ios::in); if(!infile) { cerr<<" open error"<<endl; exit(1); } for(i=0;i<N;i++) { cout<<" "; infile>>num[i]; cout<<setw(4)<<num[i]; infile>>name[i]; cout<<setw(7)<<name[i]; infile>>sex[i]; cout<<setw(6)<<sex[i]; infile>>age[i]; cout<<setw(6)<<age[i]; infile>>birthday[i]; cout<<setw(12)<<birthday[i]; infile>>address[i]; cout<<setw(11)<<address[i]; infile>>tel[i]; cout<<setw(14)<<tel[i]; infile>>Email[i]; cout<<setw(18)<<Email[i]<<endl; } infile.close(); ofstream outfile("f1.txt",ios::out); if(!outfile) { cerr<<" open error"<<endl; exit(1); } cout<<"您想修改第几个学生信息?"<<endl; cin>>k; k=k-1; cout<<"请输入新的数据:"<<endl; cout<<"请输入姓名:"<<endl; cin>>name[k]; cout<<"请输入性别:"<<endl; cin>>sex[k]; cout<<"请输入年龄:"<<endl; cin>>age[k]; cout<<"请输入出生日期:"<<endl; cin>>birthday[k]; cout<<"请输入家庭住址:"<<endl; cin>>address[k]; cout<<"请输入电话:"<<endl; cin>>tel[k]; cout<<"请输入电子邮箱:"<<endl; cin>>Email[k]; for(i=0;i<N;i++) { outfile<<num[i]<<" "; outfile<<name[i]<<" "; outfile<<sex[i]<<" "; outfile<<age[i]<<" "; outfile<<birthday[i]<<" "; outfile<<address[i]<<" "; outfile<<tel[i]<<" "; outfile<<Email[i]<<" "<<endl; } outfile.close(); cout<<"已修改成功!"<<endl; }   ///8、查询文件   void student::search() { int i,n,w=0,num[M],a[M]; char ch; string searchname, name[M],sex[M],age[M],birthday[M],address[M],tel[M],Email[M],b[M][7]; cout<<"a.按学号查找 b.按姓名查找"<<endl; ifstream infile("f1.txt",ios::in); if(!infile) { cerr<<" open error"<<endl; exit(1); } for(i=0;i<N;i++) { infile>>num[i];a[i]=num[i]; infile>>name[i];b[i][0]=name[i]; infile>>sex[i];b[i][1]=sex[i]; infile>>age[i];b[i][2]=age[i]; infile>>birthday[i];b[i][3]=age[i]; infile>>address[i];b[i][4]=age[i]; infile>>tel[i];b[i][5]=address[i]; infile>>Email[i];b[i][6]=age[i]; } cout<<"请输入a或者b!"<<endl; cin>>ch; if(ch=='a') { cout<<"请输入学号!"<<endl; cin>>n; for(i=0;i<N;i++) if(a[i]==n) { w=1; cout<<" 学号 姓名 性别 年龄 出生日期 家庭住址 电话 邮箱"<<endl; cout<<" "; cout<<setw(4)<<a[i]; cout<<setw(7)<<b[i][0]; cout<<setw(6)<<b[i][1]; cout<<setw(6)<<b[i][2]; cout<<setw(12)<<b[i][3]; cout<<setw(11)<<b[i][4]; cout<<setw(14)<<b[i][5]; cout<<setw(18)<<b[i][6]<<endl; } if(w==0) cout<<"无此人信息!"<<endl; } else if(ch=='b') { cout<<"请输入姓名!"<<endl; cin>>searchname; for(i=0;i<N;i++) if(name[i]==searchname) { w=1; cout<<" 学号 姓名 性别 年龄 出生日期 家庭住址 电话 邮箱"<<endl; cout<<setw(4)<<a[i]; cout<<setw(7)<<b[i][0]; cout<<setw(6)<<b[i][1]; cout<<setw(6)<<b[i][2]; cout<<setw(12)<<b[i][3]; cout<<setw(11)<<b[i][4]; cout<<setw(14)<<b[i][5]; cout<<setw(18)<<b[i][6]<<endl; } if(w==0) cout<<"无此人信息!"<<endl; } else cout<<"没有此选项!"<<endl; }   ///9、显示文件   void student::display() { int k,num; string name,sex,age,birthday,address,tel,Email; ifstream infile("f1.txt",ios::in); if(!infile) { cerr<<" 0open error"<<endl; exit(1); } cout<<" 学号 姓名 性别 年龄 出生日期 家庭住址 电话 邮箱"<<endl; for(k=0;k<N;k++) { cout<<" "; infile>>num; cout<<setw(4)<<num; infile>>name; cout<<setw(7)<<name; infile>>sex; cout<<setw(6)<<sex; infile>>age; cout<<setw(6)<<age; infile>>birthday; cout<<setw(12)<<birthday; infile>>address; cout<<setw(11)<<address; infile>>tel; cout<<setw(14)<<tel; infile>>Email; cout<<setw(18)<<Email<<endl; } infile.close(); }     student stu; denglu dl;   ///10、菜单   void menu() { cout<<"\n\t\t\t**************欢迎进入学生信息管理系统*******************"<<endl; cout<<"\t\t\t* 1.输入学生信息 *"<<endl; cout<<"\t\t\t* 2.添加学生信息 *"<<endl; cout<<"\t\t\t* 3.删除学生信息 *"<<endl; cout<<"\t\t\t* 4.修改学生信息 *"<<endl; cout<<"\t\t\t* 5.查询学生信息 *"<<endl; cout<<"\t\t\t* 6.浏览全部学生信息 *"<<endl; cout<<"\t\t\t* 7.修改密码 *"<<endl; cout<<"\t\t\t* 0.退出学生信息 *"<<endl; cout<<"\t\t\t*********************************************************"<<endl; }   ///11、主函数   int main() { char ch='1'; int f; while(1){ f = dl.input(); if(f==1) break; }   while(f || ch!='0'){ system("color 1f"); menu(); cin>>ch; while(ch!='0') { system("cls"); switch(ch) { case '1':stu.set();break; case '2':stu.add();break; case '3':stu.del();break; case '4':stu.change();break; case '5':stu.search();break; case '6':stu.display();break; case '7':dl.newpw();break; case '0':cout<<"退出系统!";break; default:cout<<"没有此选项!"<<endl;break; } menu(); cin>>ch; } system("cls"); f = dl.input(); } return 0; }

原文链接:https://www.cnblogs.com/zjwoo/p/12762989.html
如有疑问请与原作者联系

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:C++基础 学习笔记五:重载之运算符重载

下一篇:2020年04月19日个人赛