您好,登錄后才能下訂單哦!
今天小編給大家分享一下怎么使用C++代碼實現(xiàn)學生信息管理系統(tǒng)的相關(guān)知識點,內(nèi)容詳細,邏輯清晰,相信大部分人都還太了解這方面的知識,所以分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后有所收獲,下面我們一起來了解一下吧。
編譯環(huán)境:
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> //調(diào)用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;} //設(shè)置學號 void Setinformation(string,string,string,string,string,string,string); // 修改函數(shù) 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函數(shù),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> //調(diào)用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;} //設(shè)置學號 void Setinformation(string,string,string,string,string,string,string); // 修改函數(shù) 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()) //文件結(jié)束,返回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<<" 學 生 信 息 管 理 系 統(tǒng) "<<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<<"請輸入要添加學生的人數(shù)(若退出,輸入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() //搜索結(jié)果 { int i; cout<<endl; for(i = 0;i<36;i++) cout<<"~"; cout<<"搜索結(jié)果"; for(i = 0;i<36;i++) cout<<"~"; }
Student.cpp
#include <iostream> #include <cstdlib> #include <string> #include <algorithm> //sort函數(shù) #include "Interface.h" //界面類 #include "Student.h" //學生類 #include "Fileoperate.h" //文本類 using namespace std; const int student_num = 120 + 1; //人數(shù)上限 int No = 1; //序號 int Total = 0; //總?cè)藬?shù) Interface face; //界面 Fileoperate Infile; //讀入文件類對象 Fileoperate Outfile; //讀出文件類對象 Student stu[student_num]; //學生類對象數(shù)組 int switchfun(int); //功能函數(shù)選擇 void Allshow(); //顯示所有學生信息 void Searchshow(int [],int,int,int,int); //顯示搜索信息 void InFile(); //讀入文件函數(shù) void OutFile(); //讀出文件函數(shù) int main() { int op_num; //操作序數(shù) char op_char; //選擇數(shù) 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) //程序執(zhí)行過程 { if(Total != 0) op_num = face.Operateface(); //獲取操作數(shù) else op_num = face.Operateface2(); //獲取操作數(shù) if(op_num == 6) break; //輸入6,跳出 switchfun(op_num); //功能選擇 if(Total != 0 ) face.Subloadface(); else { cout<<endl; cout<<"學生數(shù)為零!"; face.Subloadface2(); } } cout<<"要保存數(shù)據(jù)嗎?(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; //添加界面操作數(shù) string Num1; //學號 string Name1; //姓名 string Birthday1; //出生日期 string Sex1; //性別 string Political_appearance1; //政治面貌 string Phone_number1; //手機號碼 string Address1; if (op_num == 1) { num = face.Addface(); //獲取添加界面操作數(shù) if(num == 0) { cout<<"已退出,任意鍵繼續(xù)..."; 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); //設(shè)置序號 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; //刪除界面操作數(shù) no = face.Delface(); //獲取刪除界面操作數(shù) if(no == 0) { cout<<"已退出,任意鍵繼續(xù)..."; getchar(); getchar(); return 0;} for (i = no;i<Total;i++) { stu[i].SetNo(i);stu[i-1] = stu[i];} cout<<endl<<"刪除該學生信息成功!"<<endl; cout<<"任意鍵繼續(xù)..."; 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(); //獲取修改界面操作數(shù) if(no == 0) { cout<<"已退出,按任意鍵繼續(xù)..."; 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<<"按任意鍵繼續(xù)..."; getchar();getchar(); } else if(op_num == 4) { int no; int Begin,End; //開始,結(jié)束位置 int num = 0; //符合搜索條件的人數(shù) string Ss; //臨時對象 no = face.Searchface(); //獲取搜索界面操作數(shù) if(no == 0) { cout<<"已退出,按任意鍵繼續(xù)..."; getchar(); getchar(); return 0;} int flag = 0; switch(no) { case 1: //按學號搜索 { cout<<"輸入關(guān)鍵字:"; 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<<"沒此相關(guān)信息!"<<endl; else { cout<<endl<<"共有"<<num<<"條相關(guān)信息!"<<endl; num = 0; for(i = 0;i<80;i++) cout<<"~"; } break; } case 2: //按名字搜索 { cout<<"輸入關(guān)鍵字:"; 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<<"沒此相關(guān)信息!"<<endl; else { cout<<endl<<"共有"<<num<<"條相關(guān)信息!"<<endl; num = 0; for(i = 0;i<80;i++) cout<<"~"; } break; } case 3: //按性別搜索 { cout<<"輸入關(guān)鍵字:"; 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<<"沒此相關(guān)信息!"<<endl; else { cout<<endl<<"共有"<<num<<"條相關(guān)信息!"<<endl; num = 0; for(i = 0;i<80;i++) cout<<"~"; } break; } case 4: //按政治面貌搜索 { cout<<"輸入關(guān)鍵字:"; 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<<"沒此相關(guān)信息!"<<endl; else { cout<<endl<<"共有"<<num<<"條相關(guān)信息!"<<endl; num = 0; for(i = 0;i<80;i++) cout<<"~"; } break; } case 5: //按家庭地址搜索 { cout<<"輸入關(guān)鍵字:"; 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<<"沒此相關(guān)信息!"<<endl; else { cout<<endl<<"共有"<<num<<"條相關(guān)信息!"<<endl; num = 0; for(i = 0;i<80;i++) cout<<"~"; } break; } } cout<<"任意鍵繼續(xù)..."; getchar();getchar(); } else if(op_num == 5) //顯示所有信息 { Allshow(); } return 1; } void Allshow() { int i; cout<<endl; for(i = 0;i<36;i++) cout<<"~"; cout<<"顯示結(jié)果"; for(i = 0;i<36;i++) cout<<"~"; for(i = 0;i<Total;i++) stu[i].Show(); for(i = 0;i<80;i++) cout<<"~"; cout<<"繼續(xù)按任意鍵...."; getchar();getchar(); } void InFile() { int i; Infile.open_in_file(); int total = 0; while(Infile.Filecin()) //把文件中的所有數(shù)據(jù)導入學生對象中 { 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++) //把學生對象中的數(shù)據(jù)讀入到文件中 { 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), //恢復默認系統(tǒng)顏色 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), //恢復默認系統(tǒng)顏色 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++代碼實現(xiàn)學生信息管理系統(tǒng)”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家閱讀完這篇文章都有很大的收獲,小編每天都會為大家更新不同的知識,如果還想學習更多的知識,請關(guān)注億速云行業(yè)資訊頻道。
免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。