C++實現(xiàn)簡單信息管理系統(tǒng)
本文實例為大家分享了C++實現(xiàn)簡單信息管理系統(tǒng)的具體代碼,供大家參考,具體內(nèi)容如下
信息管理系統(tǒng)
因為學(xué)校布置了寫一個信息管理系統(tǒng)的作業(yè),所以寫下了這個信息管理系統(tǒng),這是用cpp寫的一個簡單程序。
基本思路是通過控制臺程序,然后通過數(shù)字命令來進行操作。
該程序編譯了多文件;
因為是學(xué)校的作業(yè)所以我把萬能頭換成了
#include"stdc++.h"
功能和萬能頭一致。
該系統(tǒng)使用了鏈表來鏈接各個數(shù)據(jù)。
程序的基本功能是打算寫兩個系統(tǒng),輸入1和賬號密碼進入學(xué)生管理系統(tǒng)
輸入2和賬號密碼進入教師管理系統(tǒng)
但是我只寫了教師管理系統(tǒng),因為這是學(xué)校的作業(yè),而且之前我有寫過學(xué)生管理系統(tǒng)了,所以沒打算再寫一次,以后有時間再說。
進入教師管理系統(tǒng)以后 :
輸入1:用戶自己增加教師信息
輸入2:用戶可以通過教師工號更改教師的姓名或身份證號或年齡或教師工號或者工資或者執(zhí)教課程或課程學(xué)分。
輸入3:通過教師工號來刪除特定教師
輸入4:顯示所有的教師信息
輸入5:從指定文件(D:\1808190126許達峰\Data.txt)讀入信息。
輸入6:顯示符合某個特定信息的教師,可以用教師姓名、身份證、工號、工資、年齡、執(zhí)教課程進行檢索
輸入7:可以通過教師工號、工資、年齡進行排序(從小到大)
輸入0:退出系統(tǒng)。
主程序代碼如下:
#include"stdc++.h" #include"interface.h" #include"actions.h" #include"teacherMessage.h" #include<stdlib.h> using namespace std; int main(){ ?? ?system("color 0F"); ?? ?//信息管理系統(tǒng)選項 ?? ?tasks(); ?? ?return 0; }
這里包括了萬能頭、界面樣式、用戶操作、教師類定義四個頭文件。
萬能頭文件如下:
#ifndef _GLIBCXX_NO_ASSERT #include <cassert> #endif #include <cctype> #include <cerrno> #include <cfloat> #include <ciso646> #include <climits> #include <clocale> #include <cmath> #include <csetjmp> #include <csignal> #include <cstdarg> #include <cstddef> #include <cstdio> #include <cstdlib> #include <cstring> #include <ctime> #if __cplusplus >= 201103L #include <ccomplex> #include <cfenv> #include <cinttypes> #include <cstdalign> #include <cstdbool> #include <cstdint> #include <ctgmath> #include <cwchar> #include <cwctype> #endif // C++ #include <algorithm> #include <bitset> #include <complex> #include <deque> #include <exception> #include <fstream> #include <functional> #include <iomanip> #include <ios> #include <iosfwd> #include <iostream> #include <istream> #include <iterator> #include <limits> #include <list> #include <locale> #include <map> #include <memory> #include <new> #include <numeric> #include <ostream> #include <queue> #include <set> #include <sstream> #include <stack> #include <stdexcept> #include <streambuf> #include <string> #include <typeinfo> #include <utility> #include <valarray> #include <vector> #if __cplusplus >= 201103L #include <array> #include <atomic> #include <chrono> #include <condition_variable> #include <forward_list> #include <future> #include <initializer_list> #include <mutex> #include <random> #include <ratio> #include <regex> #include <scoped_allocator> #include <system_error> #include <thread> #include <tuple> #include <typeindex> #include <type_traits> #include <unordered_map> #include <unordered_set> #endif
界面樣式代碼如下:
.h文件代碼:
#ifndef INTERFACE #define INTERFACE #include"stdc++.h" using namespace std; //教師信息管理系統(tǒng)界面樣式 void MainFace(); //信息管理系統(tǒng)界面樣式 void outFace(); #endif INTERFACE
.cpp文件代碼:
#include"interface.h" //教師信息管理系統(tǒng)界面樣式 void MainFace(){ ? ? cout<<" ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?"<<"---------------------------------------------------------------"<<endl; ? ? cout<<" ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?"<<"------------- ? ? ? 歡迎進入教師信息管理系統(tǒng) ? ? ?-------------"<<endl; ? ? cout<<" ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?"<<"---------------------------------------------------------------"<<endl; ? ? cout<<" ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?"<<"---- 1.添加教師信息 ? ? ? ---------- 2.更改一位教師信息 -------"<<endl; ? ? cout<<" ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?"<<"---- 3.刪除教師信息 ? ? ? ---------- 4.顯示所有教師信息 -------"<<endl; ? ? cout<<" ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?"<<"---- 5.從文件錄入教師信息 ---------- 6.顯示教師信息 ? ? -------"<<endl; ? ? cout<<" ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?"<<"---- 7.對教師信息進行排序 ---------- 0.退出當(dāng)前界面 ? ? -------"<<endl; ?? ?cout<<" ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?"<<"---------------------------------------------------------------"<<endl; } //信息管理系統(tǒng)界面樣式 void outFace(){ ?? ?cout<<" ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?"<<"---------------------------------------------------------------"<<endl; ? ? cout<<" ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?"<<"------------ ? ? ? ? ?歡迎進入信息管理系統(tǒng) ? ? ?---------------"<<endl; ? ? cout<<" ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?"<<"---------------------------------------------------------------"<<endl; ? ? cout<<" ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?"<<"---- 1.學(xué)生登入 ? ? ? ? ?-------- ? ? ? ? 2.教師登入 ----------"<<endl; ?? ?cout<<" ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?"<<"---- 3.關(guān)于 ? ? ? ? ? ? ?-------- ? ? ? ? 0.退出系統(tǒng) ----------"<<endl; ?? ?cout<<" ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?"<<"---------------------------------------------------------------"<<endl; ?? ?cout<<" ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?"<<"測試使用的教師賬號:teacher 密碼:123456789 ? ? ? ? ? ? ? ? ? ?"<<endl; }
用戶操作代碼如下:
.h文件
#ifndef ACTIONS #define ACTIONS #include"stdc++.h" #include"teacherMessage.h" using namespace std; typedef struct teacherDate * List; typedef struct teacherDate Data; //鏈表的構(gòu)成元素 struct teacherDate { ? ? Teacher self; ? ? List next; }; //教師信息管理系統(tǒng)選項 void acts(); //主界面選項 void tasks(); #endif
.cpp文件
#include"actions.h" #include"interface.h" #include"teacherSystem.h" #include <windows.h> //賬號密碼 const string R_Id="teacher"; const string R_password="123456789"; //主界面選項 void tasks(){ ? ? int task; ? ? outFace(); ? ? cout<<"請輸入0-2:";? ? ? while(cin>>task){ ? ? ? ? if(task==0){ ?? ??? ??? ?cout<<"拜拜"<<endl; ?? ??? ??? ?Sleep(1000); ? ? ? ? ? ? break; ? ? ? ? }else if (task==1){ ?? ??? ??? ?//學(xué)生信息管理系統(tǒng) ? ? ? ? ? ? cout<<"該功能暫未開放,敬請期待"<<endl; ? ? ? ? }else if(task==2){ ?? ??? ??? ?//教師信息管理系統(tǒng) ? ? ? ? ? ? string Id,passwrod; ? ? ? ? ? ? cout<<"請輸入教師賬號、密碼:"; ? ? ? ? ? ? cin>>Id>>passwrod; ?? ??? ??? ?//檢驗賬號密碼是否正確 ? ? ? ? ? ? if(Id==R_Id){ ?? ??? ??? ??? ?if(passwrod==R_password){ ? ? ? ? ? ? ? ? acts(); ?? ??? ??? ??? ?}else{ ?? ??? ??? ??? ??? ?cout<<"密碼錯誤"<<endl; ?? ??? ??? ??? ?} ? ? ? ? ? ? }else{ ?? ??? ??? ??? ?cout<<"賬號錯誤"<<endl; ?? ??? ??? ?} ? ? ? ? }else if(task==3){ ?? ??? ??? ?cout<<"-----------------------------------------------------"<<endl; ?? ??? ??? ?cout<<"完整代碼和使用方法請參觀網(wǎng)址: ? ? ? ? ? ? ? ? ? ? ? "<<endl; ? ? ? ? ? ? cout<<"https://blog.csdn.net/TeqGin/article/details/92760558"<<endl; ?? ??? ??? ?cout<<"-----------------------------------------------------"<<endl; ?? ??? ??? ?cout<<"作者:1808190126 軟件一班 許達峰 ? ? ? ? ? ? ? ? ? ? "<<endl; ?? ??? ??? ?cout<<"-----------------------------------------------------"<<endl; ?? ??? ?}else{ ? ? ? ? ? ? cout<<"無效輸入"<<endl; ? ? ? ? } ? ? ? ? //清屏 ?? ??? ?system("pause"); ?? ??? ?system("CLS"); ? ? ? ? outFace(); ?? ??? ?//選擇 ? ? ? ? cout<<"請輸入0-2"<<endl; ? ? } } //教師信息管理系統(tǒng)選項 void acts(){ ?? ?int a; ?? ?List head=NULL; ?? ?//清屏 ?? ?system("pause"); ?? ?system("CLS"); ?? ?MainFace(); ?? ?cout<<"請輸入0-7:"; ? ? while(cin>>a){ ? ? ? ? if(a==1){ ?? ??? ??? ?//增加教師信息 ?? ??? ??? ?head=add(head); ? ? ? ? }else if(a==2){ ?? ??? ??? ?//更改教師信息 ?? ??? ??? ?head=changeActs(head); ? ? ? ? }else if(a==3){ ?? ??? ??? ?//刪除教師信息 ?? ??? ??? ?head=excludeActs(head); ? ? ? ? }else if(a==4){ ?? ??? ??? ?//顯示所有教師信息 ?? ??? ??? ?head=dataFace(head); ? ? ? ? }else if(a==5){ ?? ??? ??? ?//從文件錄入教師信息 ?? ??? ??? ?head=readin(head); ? ? ? ? }else if(a==6){ ?? ??? ??? ?//顯示教師信息 ?? ??? ??? ?head=showTeacherActs(head); ? ? ? ? }else if(a==7){ ?? ??? ??? ?//對教師信息進行排序 ? ? ? ? ? ? head=softData(head); ? ? ? ? }else if(a==0){ ?? ??? ??? ?cout<<"將返回主界面"<<endl; ?? ??? ??? ?head=clearAll(head); ?? ??? ??? ?Sleep(1000); ?? ??? ??? ?break; ?? ??? ?} ?? ??? ?//清屏 ?? ??? ?system("pause"); ?? ??? ?system("CLS"); ?? ??? ?MainFace(); ?? ??? ?cout<<"請輸入0-7:"; ? ? } }
用戶操作定義還包括下屬的數(shù)字命令文件
數(shù)字命令文件代碼如下:
.h文件
#ifndef TEACHERSYSTEM #define TEACHERSYSTEM #include"stdc++.h" #include"actions.h" using namespace std; //增加教師信息 List add(List head); //更改教師信息 List changeActs(List head); //刪除教師信息 List excludeActs(List head); //顯示所有教師信息 List dataFace(List head); //從文件錄入教師信息 List readin(List head); //顯示教師信息 List showTeacherActs(List head); //對教師信息進行排序 List softData(List head); //清除所有數(shù)據(jù),防止內(nèi)存泄露 List clearAll(List head); //根據(jù)教師工號搜索指定教師 List findTeacher(List head,string teacherId); #endif TEACHERSYSTEM
.cpp文件
#include"teacherSystem.h" #include"interface.h" #include"teacherMessage.h" #include"changeActions.h" //增加教師信息 List add(List head){ ? ? string Name,Age,teacherId,salary; ?? ?string Identify; ?? ?string LesName,Point; ?? ?Lesson course; ? ? if(head==NULL){? ? ? ? ? //為首結(jié)點動態(tài)分配空間 ? ? ? ? head=new Data; ? ? ? ? ? ? ? ? ? ? ? ? ? ? cout<<"請依次輸入教師的姓名、身份證號(后四位)、年齡、教師工號、工資、執(zhí)教課程、課程學(xué)分:"; ? ? ? ? cin>>Name>>Identify>>Age>>teacherId>>salary>>LesName>>Point; ? ? ? ? //將數(shù)據(jù)傳入Teacher類中 ?? ??? ?course.getData(LesName,Point); ? ? ? ? head->self.getData(Name,Identify,Age,teacherId,salary,course); ? ? ? ? //令下一個結(jié)點為空 ? ? ? ? ? ? ? ? ? ? head->next=NULL; ? ? }else{ ? ? ? ? List current,record; ? ? ? ? current=head; ? ? ? ? //通過遍歷找到空結(jié)點,把數(shù)據(jù)分配到空結(jié)點中 ? ? ? ? while(current->next){ ? ? ? ? ? ? current=current->next; ?? ??? ??? ?record=current->next; ? ? ? ? } ?? ??? ?if(current==head) ?? ??? ??? ?record=current->next; ? ? ? ? cout<<"請依次輸入教師的姓名、身份證號(后四位)、年齡、教師工號、工資、執(zhí)教課程、課程學(xué)分:"; ? ? ? ? cin>>Name>>Identify>>Age>>teacherId>>salary>>LesName>>Point; ?? ??? ?course.getData(LesName,Point); ? ? ? ? //為新結(jié)點動態(tài)分配空間 ? ? ? ? record= new Data; ? ? ? ? record->self.getData(Name,Identify,Age,teacherId,salary,course); ?? ??? ?//令下一個結(jié)點為空 ?? ??? ?record->next=NULL; ?? ??? ?current->next=record; ? ? } ?? ?return head; } //更改教師信息 List changeActs(List head){ ? ? int choose; ? ? string teacherId; ? ? cout<<"---------------------------------------------------"<<endl; ? ? cout<<"--1更改姓名 ? ? ?2更改身份證號 ? ? ?3更改年齡 ? ?--"<<endl; ?? ?cout<<"---------------------------------------------------"<<endl; ? ? cout<<"--4更改教師工號 ?5更改工資 ? ? ? ? ?6.更改執(zhí)教課程-"<<endl; ? ? cout<<"---------------------------------------------------"<<endl; ?? ?cout<<"--7.更改課程學(xué)分 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?-"<<endl; ?? ?cout<<"---------------------------------------------------"<<endl; ? ? cout<<"請輸入1-7:"; ?? ?cin>>choose; ?? ?cout<<endl; ? ? cout<<"請輸入你希望更改的教師的工號"<<endl; ? ? cin>>teacherId; ? ? if(choose==1){ ? ? ? ? changeName(head,teacherId); ? ? }else if(choose==2){ ? ? ? ? changeIdentify(head,teacherId); ? ? }else if(choose==3){ ? ? ? ? changeAge(head,teacherId); ? ? }else if(choose==4){ ? ? ? ? changeTeacherId(head,teacherId); ? ? }else if(choose==5){ ? ? ? ? changeSalary(head,teacherId); ? ? }else if(choose==6){ ?? ??? ?changeLesName(head,teacherId); ?? ?}else if(choose==7){ ?? ??? ?changePoint(head,teacherId); ?? ?}else{ ? ? ? ? cout<<"無效輸入"<<endl; ? ? } ?? ?return head; } //顯示所有教師信息 List dataFace(List head){ ?? ?List current=head; ?? ?cout<<"---------------------------------------------------------------------------"<<endl; ?? ?cout<<" 姓名 ? ? ?身份證號 ? ? 年齡 ? ?教師工號 ? ? 工資 ? ?執(zhí)教課程 ? 課程學(xué)分 ? "<<endl; ?? ?cout<<"---------------------------------------------------------------------------"<<endl; ?? ?if(current==NULL){ ?? ??? ?cout<<"表格為空!"<<endl; ?? ?}else{ ?? ??? ?while(current){ ?? ??? ??? ?current->self.showmessage(); ?? ??? ??? ?cout<<endl; ?? ??? ??? ?cout<<"---------------------------------------------------------------------------"<<endl; ?? ??? ??? ?current=current->next; ?? ??? ?} ?? ?} ? ? return head; } //刪除教師信息 List excludeActs(List head){ ? ? string teacherId; ? ? cout<<"請輸入你要刪除的教師的教師工號:"; ? ? cin>>teacherId; ? ? List current=head,before; ? ?? ? ? if(current){ ?? ??? ?while(current->next!=NULL&¤t->self.getTeacherId()!=teacherId){ ?? ??? ??? ?before=current; ?? ??? ??? ?current=current->next; ?? ??? ?} ? ? ? ? if(current->self.getTeacherId()==teacherId){ ? ? ? ? ? ? if(current==head){ ? ? ? ? ? ? ? ? List record=head; ? ? ? ? ? ? ? ? head=head->next; ? ? ? ? ? ? ? ? delete record; ? ? ? ? ? ? ? ? return head; ? ? ? ? ? ? }else{ ? ? ? ? ? ? ? ? before->next=current->next; ? ? ? ? ? ? ? ? delete current; ? ? ? ? ? ? } ? ? ? ? }else{ ? ? ? ? ? ? cout<<"該教師不存在!"<<endl; ? ? ? ? } ? ? }else{ ?? ??? ?cout<<"該表為空!"<<endl; ? ? } ?? ?return head; } //顯示教師信息 List showTeacherActs(List head){ ? ? int choose; ? ? cout<<"----------------------------------------------"<<endl; ? ? cout<<"1.根據(jù)姓名檢索 ? ? ? ? ? ? ? ? 2.根據(jù)身份證號檢索"<<endl; ? ? cout<<"----------------------------------------------"<<endl; ? ? cout<<"3.根據(jù)教師工號檢索 ? ? ? ? ? ? 4.根據(jù)工資檢索 ? "<<endl; ? ? cout<<"-----------------------------------------------"<<endl; ?? ?cout<<"5.根據(jù)教師年齡檢索 ? ? ? ? ? ? 6.根據(jù)執(zhí)教課程檢索"<<endl; ?? ?cout<<"-----------------------------------------------"<<endl; ? ? cin>>choose; ? ? if(choose==1){ ? ? ? ? searchName(head); ? ? }else if(choose==2){ ? ? ? ? searchIdentify(head); ? ? }else if(choose==3){ ? ? ? ? searchTeacherId(head); ? ? }else if(choose==4){ ? ? ? ? searchSalary(head); ? ? }else if(choose==5){ ?? ??? ?searchAge(head); ?? ?}else if(choose==6){ ?? ??? ?searchCourseLesName(head); ?? ?}else{ ? ? ? ? cout<<"無效操作"<<endl; ? ? } ? ? return head; } //從文件錄入教師信息 List readin(List head){ ? ? ifstream fin; ?? ?string Point,LesName; ?? ?Lesson course; ? ? cout<<"如果要使用本功能,請確保1808190126許達峰文件夾位于D盤。"<<endl; ? ? char address[]="D:\\1808190126許達峰\\Data.txt"; ? ? string Name,Identify,Age,teacherId,salary; ?? ?List current,record,recall; ? ? fin.open(address); ?? ?current=head; ?? ?recall=head; ?? ?if(head==NULL){ ?? ??? ?recall=head; ?? ?}else{ ?? ??? ?//通過遍歷找到空結(jié)點 ?? ??? ?while(current->next){ ?? ??? ??? ?current=current->next; ?? ??? ??? ?recall=current->next; ?? ??? ?} ?? ??? ?if(current==head) ?? ??? ??? ?recall=current->next; ?? ?} ?? ?if(!fin.is_open()){ ?? ??? ?cout<<"打開文件失??!"<<endl; ?? ?}else{ ?? ??? ?while(fin>>Name>>Identify>>Age>>teacherId>>salary>>LesName>>Point){ ?? ??? ??? ?recall=new Data;?? ??? ??? ? ?? ??? ??? ?if(head==NULL){ ?? ??? ??? ??? ?head=recall; ?? ??? ??? ?}else{ ?? ??? ??? ??? ?current->next=recall; ?? ??? ??? ?} ?? ??? ??? ?course.getData(LesName,Point); ?? ??? ??? ?recall->self.getData(Name,Identify,Age,teacherId,salary,course); ?? ??? ??? ?recall->next=NULL; ?? ??? ??? ?current=recall; ?? ??? ?} ?? ??? ?fin.clear(); ?? ?} ? ? return head; } //對教師信息進行排序 List softData(List head){ ?? ?int choose; ? ? cout<<"-----------------------------------------"<<endl; ? ? cout<<"1.根據(jù)教師年齡排序 ? ? ? ?2.根據(jù)教師工號排序"<<endl; ? ? cout<<"-----------------------------------------"<<endl; ? ? cout<<"3.根據(jù)教師工資排序 ? ? ? ? ? ? ? ? ? ? ? ?"<<endl; ? ? cout<<"-----------------------------------------"<<endl; ?? ?cin>>choose; ? ? if(choose==1){ ? ? ? ? head=bubbleSofAge(head); ? ? }else if(choose==2){ ? ? ? ? head=bubbleSoftTeacherId(head); ? ? }else if(choose==3){ ? ? ? ? head=bubbleSoftSalary(head); ? ? }else{ ? ? ? ? cout<<"a無效操作!"<<endl; ? ? } ? ? return head; } //根據(jù)教師工號搜索指定教師 List findTeacher(List head,string teacherId){ ? ? List current=head; ?? ?if(current==NULL) return current; ? ? while(current->next!=NULL&¤t->self.getTeacherId()!=teacherId){ ? ? ? ? current=current->next; ? ? } ? ? return current; } //清除所有數(shù)據(jù),防止內(nèi)存泄露 List clearAll(List head){ ? ? List current=head,record; ?? ?if(head!=NULL){ ? ? ? ? while(current){ ? ? ? ? ? ? record=current->next; ? ? ? ? ? ? delete current; ? ? ? ? ? ? current=record; ? ? ? ? } ?? ?} ? ? return head=NULL; }
教師系統(tǒng)操作還包括以下文件:
.h文件:
#ifndef CHANGEACTIONS #define CHANGEACTIONS #include"stdc++.h" #include"actions.h" using namespace std; //更改姓名 void changeName(List head,string teacherId); //更改身份證號 void changeIdentify(List head,string teacherId); //更改年齡 void changeAge(List head,string teacherId); //更改教師工號 void changeTeacherId(List head,string teacherId); //更改工資 void changeSalary(List head,string teacherId); //更改課程名稱 void changeLesName(List head,string teacherId); //更改課程學(xué)分 void changePoint(List head,string teacherId); //檢索姓名 void searchName(List head); //檢索身份證號 void searchIdentify(List head); //檢索教師工號 void searchTeacherId(List head); //檢索工資 void searchSalary(List head); //檢索年齡 void searchAge(List head); //根據(jù)執(zhí)教課程檢索 void searchCourseLesName(List head); //基于冒泡排序的根據(jù)教師年齡排序 List bubbleSofAge(List head); //基于冒泡排序的根據(jù)教師工號排序 List bubbleSoftTeacherId(List head); //基于冒泡排序的根據(jù)教師工資排序 List bubbleSoftSalary(List head); #endif CHANGEACITIONS
.cpp文件:
#include"changeActions.h" #include"teacherMessage.h" #include"teacherSystem.h" //更改姓名 void changeName(List head,string teacherId){ ? ? List current=findTeacher(head,teacherId); ? ? if(current){ ? ? ? ? if(current->self.getTeacherId()==teacherId){ ? ? ? ? ? ? string Name; ? ? ? ? ? ? cout<<"請輸入新名字"<<endl; ? ? ? ? ? ? cin>>Name; ? ? ? ? ? ? current->self.changeName(Name); ? ? ? ? }else{ ? ? ? ? ? ? cout<<"該教師不存在"<<endl; ? ? ? ? } ? ? }else{ ? ? ? ? cout<<"該表為空!"<<endl; ? ? } } //更改身份證號 void changeIdentify(List head,string teacherId){ ? ? List current=findTeacher(head,teacherId); ? ? if(current){ ? ? ? ? if(current->self.getTeacherId()==teacherId){ ? ? ? ? ? ? string Identify; ? ? ? ? ? ? cout<<"請輸入新身份證號"<<endl; ? ? ? ? ? ? cin>>Identify; ? ? ? ? ? ? current->self.changeIdentify(Identify); ? ? ? ? }else{ ? ? ? ? ? ? cout<<"該教師不存在"<<endl; ? ? ? ? } ? ? }else{ ? ? ? ? cout<<"該表為空!"<<endl; ? ? } } //更改年齡 void changeAge(List head,string teacherId){ ?? ?List current=findTeacher(head,teacherId); ? ? if(current){ ? ? ? ? if(current->self.getTeacherId()==teacherId){ ? ? ? ? ? ? string Age; ? ? ? ? ? ? cout<<"請輸入新年齡"<<endl; ? ? ? ? ? ? cin>>Age; ? ? ? ? ? ? current->self.changeAge(Age); ? ? ? ? }else{ ? ? ? ? ? ? cout<<"該教師不存在"<<endl; ? ? ? ? } ? ? }else{ ? ? ? ? cout<<"該表為空!"<<endl; ? ? } } //更改教師工號 void changeTeacherId(List head,string teacherId){ ?? ?List current=findTeacher(head,teacherId); ? ? if(current){ ? ? ? ? if(current->self.getTeacherId()==teacherId){ ? ? ? ? ? ? string TeacgerId; ? ? ? ? ? ? cout<<"請輸入新教師工號"<<endl; ? ? ? ? ? ? cin>>TeacgerId; ? ? ? ? ? ? current->self.changeTeacherId(TeacgerId); ? ? ? ? }else{ ? ? ? ? ? ? cout<<"該教師不存在"<<endl; ? ? ? ? } ? ? }else{ ? ? ? ? cout<<"該表為空!"<<endl; ? ? } } //更改工資 void changeSalary(List head,string teacherId){ ?? ?List current=findTeacher(head,teacherId); ? ? if(current){ ? ? ? ? if(current->self.getTeacherId()==teacherId){ ? ? ? ? ? ? string Salary; ? ? ? ? ? ? cout<<"請輸入新工資"<<endl; ? ? ? ? ? ? cin>>Salary; ? ? ? ? ? ? current->self.changeSalary(Salary); ? ? ? ? }else{ ? ? ? ? ? ? cout<<"該教師不存在"<<endl; ? ? ? ? } ? ? }else{ ? ? ? ? cout<<"該表為空!"<<endl; ? ? } } //更改課程名稱 void changeLesName(List head,string teacherId){ ?? ?List current=findTeacher(head,teacherId); ? ? if(current){ ? ? ? ? if(current->self.getTeacherId()==teacherId){ ? ? ? ? ? ? string LesName; ? ? ? ? ? ? cout<<"請輸入新課程名稱"<<endl; ? ? ? ? ? ? cin>>LesName; ? ? ? ? ? ? current->self.changeCoureseLesName(LesName); ? ? ? ? }else{ ? ? ? ? ? ? cout<<"該教師不存在"<<endl; ? ? ? ? } ? ? }else{ ? ? ? ? cout<<"該表為空!"<<endl; ? ? } } //更改課程學(xué)分 void changePoint(List head,string teacherId){ ?? ?List current=findTeacher(head,teacherId); ? ? if(current){ ? ? ? ? if(current->self.getTeacherId()==teacherId){ ? ? ? ? ? ? string Point; ? ? ? ? ? ? cout<<"請輸入新課程學(xué)分"<<endl; ? ? ? ? ? ? cin>>Point; ? ? ? ? ? ? current->self.changeCouresePoint(Point); ? ? ? ? }else{ ? ? ? ? ? ? cout<<"該教師不存在"<<endl; ? ? ? ? } ? ? }else{ ? ? ? ? cout<<"該表為空!"<<endl; ? ? } } //檢索姓名 void searchName(List head){ ?? ?List current=head; ?? ?string Name; ?? ?cout<<"請輸入教師姓名:"; ?? ?cin>>Name; ?? ? ? ? cout<<"---------------------------------------------------------------------------"<<endl; ?? ?cout<<" 姓名 ? ? ?身份證號 ? ? 年齡 ? ?教師工號 ? ? 工資 ? ?執(zhí)教課程 ? 課程學(xué)分 ? "<<endl; ?? ?cout<<"---------------------------------------------------------------------------"<<endl; ?? ?//遍歷 ? ? if(current){ ? ? ? ? while(current){ ? ? ? ? ? ? if(current->self.getName()==Name){ ? ? ? ? ? ? ? ? current->self.showmessage(); ?? ??? ??? ??? ?cout<<endl; ?? ??? ??? ??? ?cout<<"---------------------------------------------------------------------------"<<endl; ?? ??? ??? ??? ? ? ? ? ? ? ? } ?? ??? ??? ?current=current->next; ? ? ? ? } ? ? }else{ ? ? ? ? cout<<"該表格為空!"<<endl; ? ? } } //檢索身份證號 void searchIdentify(List head){ ?? ?List current=head; ?? ?string Identify; ?? ?cout<<"請輸入教師身份證:"; ?? ?cin>>Identify; ?? ? ? ? cout<<"---------------------------------------------------------------------------"<<endl; ?? ?cout<<" 姓名 ? ? ?身份證號 ? ? 年齡 ? ?教師工號 ? ? 工資 ? ?執(zhí)教課程 ? 課程學(xué)分 ? "<<endl; ?? ?cout<<"---------------------------------------------------------------------------"<<endl; ?? ?//遍歷 ? ? if(current){ ? ? ? ? while(current){ ? ? ? ? ? ? if(current->self.getIdentify()==Identify){ ? ? ? ? ? ? ? ? current->self.showmessage(); ?? ??? ??? ??? ?cout<<endl; ?? ??? ??? ??? ?cout<<"---------------------------------------------------------------------------"<<endl; ?? ??? ??? ??? ? ? ? ? ? ? ? } ?? ??? ??? ?current=current->next; ? ? ? ? } ? ? }else{ ? ? ? ? cout<<"該表格為空!"<<endl; ? ? } } //檢索教師工號 void searchTeacherId(List head){ ?? ?List current=head; ?? ?string teacherId; ?? ?cout<<"請輸入教師工號:"; ?? ?cin>>teacherId; ?? ? ? ? cout<<"---------------------------------------------------------------------------"<<endl; ?? ?cout<<" 姓名 ? ? ?身份證號 ? ? 年齡 ? ?教師工號 ? ? 工資 ? ?執(zhí)教課程 ? 課程學(xué)分 ? "<<endl; ?? ?cout<<"---------------------------------------------------------------------------"<<endl; ?? ?//遍歷 ? ? if(current){ ? ? ? ? while(current){ ? ? ? ? ? ? if(current->self.getTeacherId()==teacherId){ ? ? ? ? ? ? ? ? current->self.showmessage(); ?? ??? ??? ??? ?cout<<endl; ?? ??? ??? ??? ?cout<<"---------------------------------------------------------------------------"<<endl; ?? ??? ??? ??? ? ? ? ? ? ? ? } ?? ??? ??? ?current=current->next; ? ? ? ? } ? ? }else{ ? ? ? ? cout<<"該表格為空!"<<endl; ? ? } } //檢索工資 void searchSalary(List head){ ?? ?List current=head; ?? ?string salary; ?? ?cout<<"請輸入教師工資:"; ?? ?cin>>salary; ?? ? ? ? cout<<"---------------------------------------------------------------------------"<<endl; ?? ?cout<<" 姓名 ? ? ?身份證號 ? ? 年齡 ? ?教師工號 ? ? 工資 ? ?執(zhí)教課程 ? 課程學(xué)分 ? "<<endl; ?? ?cout<<"---------------------------------------------------------------------------"<<endl; ?? ?//遍歷 ? ? if(current){ ? ? ? ? while(current){ ? ? ? ? ? ? if(current->self.getSalary()==salary){ ? ? ? ? ? ? ? ? current->self.showmessage(); ?? ??? ??? ??? ?cout<<endl; ?? ??? ??? ??? ?cout<<"---------------------------------------------------------------------------"<<endl; ?? ??? ??? ??? ? ? ? ? ? ? ? } ?? ??? ??? ?current=current->next; ? ? ? ? } ? ? }else{ ? ? ? ? cout<<"該表格為空!"<<endl; ? ? } } //檢索年齡 void searchAge(List head){ ?? ?List current=head; ?? ?string age; ?? ?cout<<"請輸入教師工資:"; ?? ?cin>>age; ?? ?//輸出樣式 ? ? cout<<"---------------------------------------------------------------------------"<<endl; ?? ?cout<<" 姓名 ? ? ?身份證號 ? ? 年齡 ? ?教師工號 ? ? 工資 ? ?執(zhí)教課程 ? 課程學(xué)分 ? "<<endl; ?? ?cout<<"---------------------------------------------------------------------------"<<endl; ?? ?//遍歷 ? ? if(current){ ? ? ? ? while(current){ ? ? ? ? ? ? if(current->self.getAge()==age){ ? ? ? ? ? ? ? ? current->self.showmessage(); ?? ??? ??? ??? ?cout<<endl; ?? ??? ??? ??? ?cout<<"---------------------------------------------------------------------------"<<endl; ?? ??? ??? ??? ? ? ? ? ? ? ? } ?? ??? ??? ?current=current->next; ? ? ? ? } ? ? }else{ ? ? ? ? cout<<"該表格為空!"<<endl; ? ? } } //根據(jù)執(zhí)教課程檢索 void searchCourseLesName(List head){ ?? ?List current=head; ?? ?string LesName; ?? ?cout<<"請輸入執(zhí)教課程:"; ?? ?cin>>LesName; ?? ?//輸出樣式 ? ? cout<<"---------------------------------------------------------------------------"<<endl; ?? ?cout<<" 姓名 ? ? ?身份證號 ? ? 年齡 ? ?教師工號 ? ? 工資 ? ?執(zhí)教課程 ? 課程學(xué)分 ? "<<endl; ?? ?cout<<"---------------------------------------------------------------------------"<<endl; ?? ?//遍歷 ? ? if(current){ ? ? ? ? while(current){ ? ? ? ? ? ? if(current->self.getCourseLesName()==LesName){ ? ? ? ? ? ? ? ? current->self.showmessage(); ?? ??? ??? ??? ?cout<<endl; ?? ??? ??? ??? ?cout<<"---------------------------------------------------------------------------"<<endl; ?? ??? ??? ??? ? ? ? ? ? ? ? } ?? ??? ??? ?current=current->next; ? ? ? ? } ? ? }else{ ? ? ? ? cout<<"該表格為空!"<<endl; ? ? } } //基于冒泡排序的根據(jù)教師年齡排序 List bubbleSofAge(List head){ ? ? Teacher T; ? ? int n=0; ? ? List current=head,record; ?? ?//統(tǒng)計一共有多少人數(shù) ? ? while(current){ ? ? ? ? n++; ? ? ? ? current=current->next; ? ? } ?? ?current=head;? ?? ?//排序 ? ? for(int i=0;i<n;i++){ ? ? ? ? while(current->next){ ?? ??? ??? ?record=current->next; ?? ??? ??? ?if(record->self.getAge()<current->self.getAge()){ ?? ??? ??? ??? ?T=record->self; ?? ??? ??? ??? ?record->self=current->self; ?? ??? ??? ??? ?current->self=T; ?? ??? ??? ?} ?? ??? ??? ?current=current->next; ? ? ? ? } ?? ??? ?current=head; ? ? ? ? } ? ? return head; } //基于冒泡排序的根據(jù)教師工號排序 List bubbleSoftTeacherId(List head){ ?? ?Teacher T; ? ? int n=0; ? ? List current=head,record; ?? ?//統(tǒng)計一共有多少人數(shù) ? ? while(current){ ? ? ? ? n++; ? ? ? ? current=current->next; ? ? } ?? ?current=head;? ?? ?//排序 ? ? for(int i=0;i<n;i++){ ? ? ? ? while(current->next){ ?? ??? ??? ?record=current->next; ?? ??? ??? ?if(record->self.getTeacherId()<current->self.getTeacherId()){ ?? ??? ??? ??? ?T=record->self; ?? ??? ??? ??? ?record->self=current->self; ?? ??? ??? ??? ?current->self=T; ?? ??? ??? ?}? ?? ??? ??? ?current=current->next; ? ? ? ? } ?? ??? ?current=head;? ? ? } ? ? return head; } //基于冒泡排序的根據(jù)教師工資排序 List bubbleSoftSalary(List head){ ?? ?Teacher T; ? ? int n=0; ? ? List current=head,record; ?? ?//統(tǒng)計一共有多少人數(shù) ? ? while(current){ ? ? ? ? n++; ? ? ? ? current=current->next; ? ? } ?? ?current=head;? ?? ?//排序//排序 ? ? for(int i=0;i<n;i++){ ? ? ? ? while(current->next){ ?? ??? ??? ?record=current->next; ?? ??? ??? ?if(record->self.getSalary()<current->self.getSalary()){ ?? ??? ??? ??? ?T=record->self; ?? ??? ??? ??? ?record->self=current->self; ?? ??? ??? ??? ?current->self=T; ?? ??? ??? ?}? ?? ??? ??? ?current=current->next; ? ? ? ? } ?? ??? ?current=head;? ? ? } ? ? return head; }
教師類定義代碼如下
.h文件
#ifndef TEACHERMESSAGE #define TEACHERMESSAGE #include"stdc++.h" using namespace std; //課程類 class Lesson{ ? ? private: ? ? string LesName; ? ? string Point; ? ? public: ? ? Lesson(){} ? ? ~Lesson(){} ? ? public: ? ? //傳輸數(shù)據(jù) ? ? void getData(string LesName,string Point); ? ? //輸出樣式 ? ? void LesInterFace(); ? ? //更改課程名稱 ?? ?void changeLesName(string LesName); ? ? //更改學(xué)分 ?? ?void changePoint(string Point); ? ? //獲取課程名稱 ?? ?string getLesName(); ? ? //獲取學(xué)分 ?? ?string getPoint(); }; //人員類 class Person{ ? ? ? ? protected: ? ? ? ? string Name; ? ? ? ? string Identify; ? ? ? ? string Age; ?? ??? ?Lesson course; ? ? ? ? public: ? ? ? ?? ? ? ? ? Person(){} ? ? ? ? ~Person(){} ? ? ? ? public: ?? ??? ?//傳輸數(shù)據(jù) ? ? ? ? void getData(string Name,string Identity,string Age,Lesson course); ?? ??? ?//輸出數(shù)據(jù) ? ? ? ? void showmessage(); ?? ??? ?//更改姓名 ? ? ? ? void changeName(string name); ?? ??? ?//更改身份證 ? ? ? ? void changeIdentify(string Identify); ?? ??? ?//更改年齡 ? ? ? ? void changeAge(string Age); ?? ??? ?//更改課程名稱 ?? ??? ?void changeCoureseLesName(string LesName); ?? ??? ?void changeCouresePoint(string Point); ?? ??? ?//獲取姓名 ?? ??? ?string getName(); ?? ??? ?//獲取身份證號 ?? ??? ?string getIdentify(); ?? ??? ?//獲取年齡 ?? ??? ?string getAge(); ?? ??? ?//獲取課程名稱 ?? ??? ?string getCourseLesName(); ? ? }; //教師類 class Teacher:public Person{ ? ? protected: ? ? string teacherId; ? ? string salary; ? ? public: ? ? Teacher(){} ? ? ~Teacher(){} ? ? public: ?? ?//傳輸數(shù)據(jù) ? ? void getData(string Name,string Identity,string Age,string teacherId,string salary,Lesson course); ?? ?//輸出 ? ? void showmessage(); ?? ?//更改教師工號 ? ? void changeTeacherId(string teacherId); ?? ?//更改工資 ? ? void changeSalary(string salary); ?? ?//獲取教師工號 ?? ?string getTeacherId(); ?? ?//獲取工資 ?? ?string getSalary(); }; #endif
.cpp文件
#include"teacherMessage.h" //傳輸數(shù)據(jù) void Person::getData(string Name,string Identity,string Age,Lesson course){ ? ? this->Name=Name; ? ? this->Identify=Identity; ? ? this->Age=Age; ?? ?this->course=course; } //輸出數(shù)據(jù) void Person::showmessage(){ ? ? cout<<" "<<this->Name<<" ? ? ?"<<this->Identify<<" ? ? ? ? ? ? "<<this->Age; } //更改姓名 void Person::changeName(string Name){ ? ? this->Name=Name; } //更改身份證 void Person::changeIdentify(string Identify){ ? ? this->Identify=Identify; } //更改年齡 void Person::changeAge(string Age){ ? ? this->Age=Age; } //更改課程名稱 void Person::changeCoureseLesName(string LesName){ ?? ?this->course.changeLesName(LesName); } void Person::changeCouresePoint(string Point){ ?? ?this->course.changePoint(Point); } //獲取年齡 string Person::getAge(){ ?? ?return this->Age; } //獲取姓名 string Person::getName(){ ?? ?return this->Name; } //獲取身份證號 string Person::getIdentify(){ ?? ?return this->Identify; } //獲取課程名稱 string Person::getCourseLesName(){ ?? ?return this->course.getLesName(); } //傳輸數(shù)據(jù) void Teacher::getData(string Name,string Identity,string Age,string teacherId,string salary,Lesson course){ ? ? Person::getData(Name,Identity,Age,course); ? ? this->teacherId=teacherId; ? ? this->salary=salary; } //輸出 void Teacher::showmessage(){ ? ? Person::showmessage(); ? ? cout<<" ? ? "<<teacherId<<" ? ? ? ? ? "<<salary<<" ? ? ?"; ?? ?this->course.LesInterFace(); } //更改工資 void Teacher::changeSalary(string salary){ ? ? this->salary=salary; } //更改教師工號 void Teacher::changeTeacherId(string teacherId){ ? ? this->teacherId=teacherId; } //獲取教師工號 string Teacher::getTeacherId(){ ?? ?return this->teacherId; } //獲取工資 string Teacher::getSalary(){ ?? ?return this->salary; } void Lesson::getData(string LesName,string Point){ ?? ?this->LesName=LesName; ?? ?this->Point=Point; } void Lesson::changeLesName(string LesName){ ?? ?this->LesName=LesName; } void Lesson::changePoint(string Point){ ?? ?this->Point=Point; } string Lesson::getLesName(){ ?? ?return this->LesName; } string Lesson::getPoint(){ ?? ?return this->Point; } void Lesson::LesInterFace(){ ?? ?cout<<this->LesName<<" ? ? ?"<<this->Point; }
程序已經(jīng)基本完成,但是不知道為什么在文件讀入那邊讀取第二次時程序會崩潰,這個問題亟待解決。
這個問題已經(jīng)解決,是之前的代碼在讀取第二次的時候還是從head開始,導(dǎo)致一個空間被分配了兩次,這樣是不行,修改代碼以后程序能正常運行了?,F(xiàn)在的代碼都是正確代碼。
這個程序?qū)懥藘商欤悴簧虾荛L,但是也不短了,這其實是一個很簡單的程序,主要是使用vs code來寫,然后用VC6.0進行編譯,很多問題沒辦法一下子找到,要慢慢的去一個地方一個地方的測試,花費了很多時間,幾個比較復(fù)雜的功能完成以后,后面就駕輕就熟了,這個程序算是第二次寫,之前有說要用更好的方法完成,確實也做到了,雖然不是很完善,不過下一次就打算使用QT來寫圖形界面程序。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
C++虛函數(shù)表與類的內(nèi)存分布深入分析理解
對C++ 了解的人都應(yīng)該知道虛函數(shù)(Virtual Function)是通過一張?zhí)摵瘮?shù)表(Virtual Table)來實現(xiàn)的。簡稱為V-Table。本文就將詳細講講虛函數(shù)表的原理與使用,需要的可以參考一下2022-08-08C++實現(xiàn)LeetCode(18.四數(shù)之和)
這篇文章主要介紹了C++實現(xiàn)LeetCode(18.四數(shù)之和),本篇文章通過簡要的案例,講解了該項技術(shù)的了解與使用,以下就是詳細內(nèi)容,需要的朋友可以參考下2021-07-07