今天小编给大家分享一下怎么使用C++代码实现学生信息管理系统的相关知识点,内容详细,逻辑清晰,相信大部分人都还太了解这方面的知识,所以分享这篇文章给大家参考一下,希望大家阅读完这篇文章后有所收获,下面我们一起来了解一下吧。
编译环境:
Microsoft Visual Studio 2019
Fileoperate.h
Interface.h
Student.h
Fileoperate.cpp
Interface.cpp
Main.cpp
Student.cpp
Fileoperate.h
#ifndef STUDENT_H #define STUDENT_H #include<iostream> #include<string> #include<windows.h> //调用API using namespace std; class Student { private: int No; //序号 string Num; //学号 string Name; //姓名 string Birthday; //出生日期 string Sex; //性别 string Political_appearance; //政治面貌 string Phone_number; //手机号码 string Address; //家庭地址 public: void SetNo(int i) { No = i;} //设置学号 void Setinformation(string,string,string,string,string,string,string); // 修改函数 void SetNum(); //修改学号 void SetName() {cin>>Name;} //修改姓名 void SetBirthday(); //修改生日 void SetSex() {cin>>Sex;} //修改性别 void SetPolitical_appearance() {cin>>Political_appearance;} //修改政治面貌 void SetPhone_number(); //修改电话号码 void SetAddress() {cin>>Address ;} //修改家庭地址 void Numshow(int,int); //学号高亮显示 void Addressshow(int,int); //家庭地址高亮显示 void Show(); //普通显示 string GetNum() { return Num;} //获取学号 string GetName() { return Name;} //获取姓名 string GetBirthday(){ return Birthday;} //获取生日 string GetSex() { return Sex;} //获取性别 string GetPolitical_appearance() { return Political_appearance;}//获取政治面貌 string GetPhone_number() { return Phone_number;} //获取电话号码 string GetAddress() { return Address;} //获取家庭地址 };
Interface.h
#include <string> #include <iostream> #include <Windows.h> //Sleep函数,system using namespace std; class Interface { public: void Startface(); //启动界面 void Loadface(); //主界面 void Subloadface(); //子界面 void Subloadface2(); //子界面2 char Inputcharface(); //提示导入文件界面 int Operateface(); //操作输入界面 int Operateface2(); //操作输入界面2 int Addface(); //增加信息界面 int Delface(); //删除信息界面 int Setface(); //修改信息界面 int Searchface(); //搜索信息界面 void Search_afterface(); //搜索后信息显示界面 void Mark(); //标签 };
Student.h
#ifndef STUDENT_H #define STUDENT_H #include<iostream> #include<string> #include<windows.h> //调用API using namespace std; class Student { private: int No; //序号 string Num; //学号 string Name; //姓名 string Birthday; //出生日期 string Sex; //性别 string Political_appearance; //政治面貌 string Phone_number; //手机号码 string Address; //家庭地址 public: void SetNo(int i) { No = i;} //设置学号 void Setinformation(string,string,string,string,string,string,string); // 修改函数 void SetNum(); //修改学号 void SetName() {cin>>Name;} //修改姓名 void SetBirthday(); //修改生日 void SetSex() {cin>>Sex;} //修改性别 void SetPolitical_appearance() {cin>>Political_appearance;} //修改政治面貌 void SetPhone_number(); //修改电话号码 void SetAddress() {cin>>Address ;} //修改家庭地址 void Numshow(int,int); //学号高亮显示 void Addressshow(int,int); //家庭地址高亮显示 void Show(); //普通显示 string GetNum() { return Num;} //获取学号 string GetName() { return Name;} //获取姓名 string GetBirthday(){ return Birthday;} //获取生日 string GetSex() { return Sex;} //获取性别 string GetPolitical_appearance() { return Political_appearance;}//获取政治面貌 string GetPhone_number() { return Phone_number;} //获取电话号码 string GetAddress() { return Address;} //获取家庭地址 }; #endif
```cpp
Fileoperate.cpp
#include "Fileoperate.h" int Fileoperate::Judge() { if( file.fail() ) { return 0; } else { return 1; } } void Fileoperate::open_in_file() { file.open(filename, ios::in); if( !Judge() ) { exit(1); } } void Fileoperate::open_out_file() { file.open(filename, ios::out); if( !Judge() ) { exit(1); } } int Fileoperate::Filecin() { string Num1; //学号 string Name1; //姓名 string Birthday1; //出生日期 string Sex1; //性别 string Political_appearance1; //政治面貌 string Phone_number1; //手机号码 string Address1; //家庭地址 if(file.eof()) //文件结束,返回0 { return 0; } file>>Num1>>Name1>>Birthday1>>Sex1>>Political_appearance1>>Phone_number1>>Address1; stu.Setinformation(Num1,Name1,Birthday1,Sex1,Political_appearance1,Phone_number1,Address1); return 1; } int Fileoperate::Filecout(Student s) { file << s.GetNum() << endl << s.GetName() << endl << s.GetBirthday() << endl << s.GetSex() << endl << s.GetPolitical_appearance() << endl << s.GetPhone_number() << endl << s.GetAddress() <<endl<<endl; return 1; } void Fileoperate::Closefile() { file.close(); }
Interface.cpp
#include <iostream> #include "Interface.h" using namespace std; void Interface::Startface() { cout<<endl<<endl<<endl<<endl<<endl<<endl<<endl<<endl<<endl<<endl<<endl <<endl<<endl<<endl<<endl<<endl<<endl<<endl<<endl<<endl<<endl; cout<<" 启动中..."; cout<<endl; for(int i = 0;i<80;i++) { Sleep(8); cout<<">"; } system("cls"); } void Interface::Loadface() { cout<<endl<<endl; cout<<" 学 生 信 息 管 理 系 统 "<<endl<<endl; cout<<" ##################################################################"<<endl; cout<<" # #"<<endl; cout<<" # 1.增加学生记录 # 2.删除学生记录 # 3.修改学生记录 #"<<endl; cout<<" # 4.找查学生记录 # 5.显示学生记录 # 6.退出 #"<<endl; cout<<" # #"<<endl; cout<<" ##################################################################"<<endl; cout<<endl; } void Interface::Subloadface2() { cout<<endl; cout<<" =======================+++操 - 作 - 提 - 示+++===================="<<endl <<" | 1.增加学生记录 | 6.退出 |"<<endl <<" =================================================================="<<endl; cout<<endl; } void Interface::Subloadface() { cout<<endl<<endl; cout<<" =======================+++操 - 作 - 提 - 示+++===================="<<endl <<" | 1.增加学生记录 | 2.删除学生记录 | 3.修改学生记录 |"<<endl <<" | 4.找查学生记录 | 5.显示学生记录 | 6.退出 |"<<endl <<" =================================================================="<<endl; cout<<endl; } char Interface::Inputcharface() { char op_char; cout<<"需要导入文件吗?(Y/N):"; cin>>op_char; return op_char; } int Interface::Operateface() { string op_num; int flag,nflag = 0 ; do { flag = 0; if(nflag) { cout<<"超出范围..Again:"; cin>>op_num; } else { cout<<"输入你的操作:"; cin>>op_num; } if (op_num.length() != 1) { flag ++; nflag ++;} if(op_num[0] >'6'||op_num[0]<'0') { flag ++; nflag ++;} }while(flag); return op_num[0]-'0'; } int Interface::Operateface2() { string op_num; int flag,nflag = 0 ; do { flag = 0; if(nflag) { cout<<"超出范围..Again:"; cin>>op_num; } else { cout<<"输入你的操作:"; cin>>op_num; } if (op_num.length() != 1) { flag ++; nflag ++;} if(op_num[0] != '6'&&op_num[0] != '1') { flag ++; nflag ++;} }while(flag); return op_num[0]-'0'; } int Interface::Addface() { int num; cout<<"请输入要添加学生的人数(若退出,输入0):"; cin>>num; return num; } int Interface::Delface() { int no; cout<<"请输入注销学生的序号(若退出,输入0):"; cin>>no; return no; } int Interface::Setface() { int no; cout<<"输入要修改的学生序号(若退出,输入0):"; cin>>no; if(no) { cout<<"输入你要改的学生的信息代号"<<endl <<"(1.学号,2.姓名,3.出生日期,4.性别,5.政治面貌,6手机号码,7.家庭地址)"<<endl <<"如果你要全部修改,则输入0..."<<endl; cout<<"代码:"; } return no; } int Interface::Searchface() { int no; cout<<"输入搜索的类型"<<endl<<"1.学号,2.姓名,3.性别,4.政治面貌,5.家庭地址"<<endl; cout<<"类型(若退出,输入0):"; cin>>no; return no; } void Interface::Search_afterface() //搜索结果 { int i; cout<<endl; for(i = 0;i<36;i++) cout<<"~"; cout<<"搜索结果"; for(i = 0;i<36;i++) cout<<"~"; }
Student.cpp
#include <iostream> #include <cstdlib> #include <string> #include <algorithm> //sort函数 #include "Interface.h" //界面类 #include "Student.h" //学生类 #include "Fileoperate.h" //文本类 using namespace std; const int student_num = 120 + 1; //人数上限 int No = 1; //序号 int Total = 0; //总人数 Interface face; //界面 Fileoperate Infile; //读入文件类对象 Fileoperate Outfile; //读出文件类对象 Student stu[student_num]; //学生类对象数组 int switchfun(int); //功能函数选择 void Allshow(); //显示所有学生信息 void Searchshow(int [],int,int,int,int); //显示搜索信息 void InFile(); //读入文件函数 void OutFile(); //读出文件函数 int main() { int op_num; //操作序数 char op_char; //选择数 char op_char2; //是否保存 face.Startface(); //启动界面 face.Loadface(); //登陆界面 int flag ,nflag = 0; //输入格式判断标记 do { flag = 0; if(nflag) cout<<"超出范围...Again:"<<endl; op_char = face.Inputcharface(); if(op_char !='Y'&&op_char !='y'&&op_char !='N'&&op_char !='n') { flag = 1; nflag = 1; } }while(flag); if (op_char == 'Y'||op_char =='y') //选择是,读入文件 { InFile(); //读入文件 char Sub_op_char; nflag = 0; do { flag = 0; cout<<"已成功导入, 要显示资料吗?(Y/N):"; cin>>Sub_op_char; if(Sub_op_char !='Y'&&Sub_op_char !='y'&&Sub_op_char !='N'&&Sub_op_char !='n') { flag = 1; } }while(flag); if (Sub_op_char == 'Y'||Sub_op_char =='y') //是否浏览已读入文件的信息 { Allshow(); //显示所有信息 face.Subloadface(); } else { if (Sub_op_char == 'N' || Sub_op_char == 'n') cout<<endl;//不显示信息 } } else { if(op_char == 'N'||op_char =='n') face.Subloadface2(); } while(1) //程序执行过程 { if(Total != 0) op_num = face.Operateface(); //获取操作数 else op_num = face.Operateface2(); //获取操作数 if(op_num == 6) break; //输入6,跳出 switchfun(op_num); //功能选择 if(Total != 0 ) face.Subloadface(); else { cout<<endl; cout<<"学生数为零!"; face.Subloadface2(); } } cout<<"要保存数据吗?(Y/N):"; cin>>op_char2; if(op_char2 == 'Y'||op_char2 == 'y') { cout<<endl<<"已保存!"<<endl; OutFile(); //读出文件 } system("pause"); return 0; } int switchfun(int op_num) { int i; int num; //添加界面操作数 string Num1; //学号 string Name1; //姓名 string Birthday1; //出生日期 string Sex1; //性别 string Political_appearance1; //政治面貌 string Phone_number1; //手机号码 string Address1; if (op_num == 1) { num = face.Addface(); //获取添加界面操作数 if(num == 0) { cout<<"已退出,任意键继续..."; getchar(); getchar(); return 0;} for(i = 0;i<num;i++) { cout<<"输入第"<<i+1<<"个学生信息:\n"; cout<<endl; cout<<"学号:"; cin>>Num1; cout<<"姓名:"; cin>>Name1; cout<<"出生日期:"; cin>>Birthday1; cout<<"性别:"; cin>>Sex1; cout<<"政治面貌:"; cin>>Political_appearance1; cout<<"手机号码:"; cin>>Phone_number1; cout<<"家庭地址:"; cin>>Address1; cout<<endl; Total ++; stu[Total-1].SetNo(Total); //设置序号 stu[Total-1].Setinformation(Num1,Name1,Birthday1,Sex1,Political_appearance1, Phone_number1,Address1); } if(i == num) cout<<"添加完成!"<<endl; } else if(op_num == 2) { int no; //删除界面操作数 no = face.Delface(); //获取删除界面操作数 if(no == 0) { cout<<"已退出,任意键继续..."; getchar(); getchar(); return 0;} for (i = no;i<Total;i++) { stu[i].SetNo(i);stu[i-1] = stu[i];} cout<<endl<<"删除该学生信息成功!"<<endl; cout<<"任意键继续..."; getchar();getchar(); Total --; } else if(op_num == 3) { int no; char info[128]; int a[14]; int k = 0; int flag = 0; no = face.Setface(); //获取修改界面操作数 if(no == 0) { cout<<"已退出,按任意键继续..."; getchar(); getchar(); return 0;} getchar(); gets_s(info); int l = strlen(info); i = 0; while(i<l) //字符输入标准化 { if(info[i]>='0'&&info[i]<='9') a[k++] = info[i]-'0'; if(a[k-1]==0) { flag = 1; break;} i++; } sort(a,a+k); //排序 if(flag) //如果输入零,修改全部信息 { cout<<"学号:"; cin>>Num1; cout<<"姓名:"; cin>>Name1; cout<<"出生日期:"; cin>>Birthday1; cout<<"性别:"; cin>>Sex1; cout<<"政治面貌:"; cin>>Political_appearance1; cout<<"手机号码:"; cin>>Phone_number1; cout<<"家庭地址:"; cin>>Address1; cout<<endl; stu[no-1].Setinformation(Num1,Name1,Birthday1,Sex1,Political_appearance1,Phone_number1,Address1); } else { for(i = 0;i<k;i++) { switch(a[i]) //修改部分信息 { case 1: { cout<<"输入学号:"; stu[no-1].SetNum(); break; } case 2: { cout<<"输入姓名:"; stu[no-1].SetName(); break; } case 3: { cout<<"输入出生日期:"; stu[no-1].SetBirthday(); break; } case 4: { cout<<"输入性别:"; stu[no-1].SetSex(); break; } case 5: { cout<<"输入政治面貌:"; stu[no-1].SetPolitical_appearance(); break; } case 6: { cout<<"输入手机号码:"; stu[no-1].SetPhone_number(); break; } case 7: { cout<<"输入家庭地址:"; stu[no-1].SetAddress(); break; } } } } cout<<endl<<endl<<"已完成, 修改后:"<<endl; //输出修改后信息 for(i= 0;i<80;i++) cout<<"-"; stu[no-1].Show(); for(i= 0;i<80;i++) cout<<"-"; cout<<endl; cout<<"按任意键继续..."; getchar();getchar(); } else if(op_num == 4) { int no; int Begin,End; //开始,结束位置 int num = 0; //符合搜索条件的人数 string Ss; //临时对象 no = face.Searchface(); //获取搜索界面操作数 if(no == 0) { cout<<"已退出,按任意键继续..."; getchar(); getchar(); return 0;} int flag = 0; switch(no) { case 1: //按学号搜索 { cout<<"输入关键字:"; cin>>Num1; for( i = 0;i<Total;i++) { Ss = stu[i].GetNum(); Begin = Ss.find(Num1,0); if(Begin != string::npos) { End = Num1.length() + Begin; flag ++; if(flag == 1) face.Search_afterface(); stu[i].Numshow(Begin,End); num++; } } if(!flag) cout<<"没此相关信息!"<<endl; else { cout<<endl<<"共有"<<num<<"条相关信息!"<<endl; num = 0; for(i = 0;i<80;i++) cout<<"~"; } break; } case 2: //按名字搜索 { cout<<"输入关键字:"; cin>>Name1; for( i = 0;i<Total;i++) { Ss = stu[i].GetName(); Begin = Ss.find(Name1,0); if(Begin != string::npos) { flag ++; if(flag == 1) face.Search_afterface(); stu[i].Show(); num++; } } if(!flag) cout<<"没此相关信息!"<<endl; else { cout<<endl<<"共有"<<num<<"条相关信息!"<<endl; num = 0; for(i = 0;i<80;i++) cout<<"~"; } break; } case 3: //按性别搜索 { cout<<"输入关键字:"; cin>>Sex1; for( i = 0;i<Total;i++) { Ss = stu[i].GetSex(); Begin = Ss.find(Sex1,0); if(Begin != string::npos) { flag ++; if(flag == 1) face.Search_afterface(); stu[i].Show(); num++; } } if(!flag) cout<<"没此相关信息!"<<endl; else { cout<<endl<<"共有"<<num<<"条相关信息!"<<endl; num = 0; for(i = 0;i<80;i++) cout<<"~"; } break; } case 4: //按政治面貌搜索 { cout<<"输入关键字:"; cin>>Political_appearance1; for( i = 0;i<Total;i++) { Ss = stu[i].GetPolitical_appearance(); Begin = Ss.find(Political_appearance1,0); if(Begin != string::npos) { flag ++; if(flag == 1) face.Search_afterface(); stu[i].Show(); num++; } } if(!flag) cout<<"没此相关信息!"<<endl; else { cout<<endl<<"共有"<<num<<"条相关信息!"<<endl; num = 0; for(i = 0;i<80;i++) cout<<"~"; } break; } case 5: //按家庭地址搜索 { cout<<"输入关键字:"; cin>>Address1; for( i = 0;i<Total;i++) { Ss = stu[i].GetAddress(); Begin = Ss.find(Address1,0); if(Begin != string::npos) { End = Address1.length() + Begin; flag ++; if(flag == 1) face.Search_afterface(); stu[i].Addressshow(Begin,End); num++; } } if(!flag) cout<<"没此相关信息!"<<endl; else { cout<<endl<<"共有"<<num<<"条相关信息!"<<endl; num = 0; for(i = 0;i<80;i++) cout<<"~"; } break; } } cout<<"任意键继续..."; getchar();getchar(); } else if(op_num == 5) //显示所有信息 { Allshow(); } return 1; } void Allshow() { int i; cout<<endl; for(i = 0;i<36;i++) cout<<"~"; cout<<"显示结果"; for(i = 0;i<36;i++) cout<<"~"; for(i = 0;i<Total;i++) stu[i].Show(); for(i = 0;i<80;i++) cout<<"~"; cout<<"继续按任意键...."; getchar();getchar(); } void InFile() { int i; Infile.open_in_file(); int total = 0; while(Infile.Filecin()) //把文件中的所有数据导入学生对象中 { stu[total++] = Infile.Getstudent(); } Total = total -1 ; for (i = 0;i<Total;i++) stu[i].SetNo(i+1); Infile.Closefile(); } void OutFile() { int i; Outfile.open_out_file(); for(i = 0; i < Total; i++) //把学生对象中的数据读入到文件中 { Outfile.Filecout(stu[i]); } Outfile.Closefile(); }
main.cpp
#include "Student.h" #include <iostream> using namespace std; void Student::Setinformation(string num,string name,string birthday,string sex, string political_app,string phone_num,string address) { Num = num; Name = name; Birthday = birthday; Sex = sex; Political_appearance = political_app; Phone_number = phone_num; Address = address; } void Student::SetNum() { unsigned int i = 0; int flag,nflag = 0; string num; do { flag = 0; if (nflag) cout<<"格式错误..Again:"; cin>>num; for(int i = 0;i<num.length();i++) { if(num[i] > '9'||num[i] < '0') { flag = 1; nflag++; break; } } }while(flag); if(!flag) Num = num; } void Student::SetBirthday() { unsigned int i = 0; int flag,nflag = 0; string birthday; do { flag = 0; if (nflag) cout<<"格式错误..Again:"; cin>>birthday; for(int i = 0;i< birthday.length();i++) { if((birthday[i] > '9'||birthday[i] < '0')&&birthday[i]!='/') { flag = 1; nflag++; break; } } }while(flag); if(!flag) Birthday = birthday; } void Student::SetPhone_number() { int i = 0; int flag,nflag = 0; string phone_number; do { flag = 0; if (nflag) cout<<"格式错误..Again:"; cin>>phone_number; for(int i = 0;i<phone_number.length();i++) { if(phone_number[i] > '9'||phone_number[i] < '0') { flag = 1; nflag++; break; } } }while(flag); if(!flag) Phone_number = phone_number; } void Student::Numshow(int Begin,int End) { int i = 0; cout<<"序号:"<<No<<endl; cout<<"学号"; HANDLE ohandle = GetStdHandle(STD_OUTPUT_HANDLE); for(int i = 0;i<Begin;i++) cout<<Num[i]; SetConsoleTextAttribute(ohandle,BACKGROUND_INTENSITY); //背景高亮 for(i = Begin;i<End;i++) cout<<Num[i]; SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), //恢复默认系统颜色 FOREGROUND_RED| FOREGROUND_GREEN| FOREGROUND_BLUE); for(i = End;i<Num.length();i++) cout<<Num[i]; cout<<" " <<"姓名:"<<Name<<" " <<"出生日期:"<<Birthday<<" " <<"性别:"<<Sex<<" " <<"政治面貌:"<<Political_appearance<<endl <<"手机号码:"<<Phone_number<<" "; cout<<endl<<endl; } void Student::Addressshow(int Begin,int End) { int i = 0; cout<<"序号:"<<No<<endl <<"学号"<<Num<<" " <<"姓名:"<<Name<<" " <<"出生日期:"<<Birthday<<" " <<"性别:"<<Sex<<" " <<"政治面貌:"<<Political_appearance<<endl <<"手机号码:"<<Phone_number<<" "; HANDLE ohandle = GetStdHandle(STD_OUTPUT_HANDLE); for(int i = 0;i<Begin;i++) cout<<Address[i]; SetConsoleTextAttribute(ohandle,BACKGROUND_INTENSITY); //背景高亮 for(i = Begin;i<End;i++) cout<<Address[i]; SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), //恢复默认系统颜色 FOREGROUND_RED| FOREGROUND_GREEN| FOREGROUND_BLUE); for(i = End;i<Address.length();i++) cout<<Address[i]; cout<<endl<<endl; } void Student::Show() //普通方式显示信息 { cout<<"序号:"<<No<<endl <<"学号"<<Num<<" " <<"姓名:"<<Name<<" " <<"出生日期:"<<Birthday<<" " <<"性别:"<<Sex<<" " <<"政治面貌:"<<Political_appearance<<endl <<"手机号码:"<<Phone_number<<" " <<"家庭地址:"<<Address<<endl<<endl; }
TXT文件格式
学号 姓名 生日 性别 政治面貌 手机号 家庭住址
以上就是“怎么使用C++代码实现学生信息管理系统”这篇文章的所有内容,感谢各位的阅读!相信大家阅读完这篇文章都有很大的收获,小编每天都会为大家更新不同的知识,如果还想学习更多的知识,请关注亿速云行业资讯频道。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。