首先的查找函數check用的是void作為返回值,在后來我發現在刪除和修改的時候都會需要遍歷一遍鏈表,所以干脆就直接用了check函數,將其返回值設為BOOL,如果可以找到對應的學籍遍返回true,否則就返回false~
在調試的時候發現總是會有一個錯誤error: expected unqualified-id before '->' token,總是找不到錯誤在哪,最后發現是我的linklist鏈表操作類根本就沒有實例化,所以不能調用linklist里面的成員函數,后來在main函數的開始加入了一個linklist X,編譯就可以正常的進行了
碰到了一個"."不能用而“->”可以用的情況,於是去查詢了相關的知識~~
分清了這個概念,首先->操作符是用於通過指針訪問對象的,而.操作符是直接訪問對象的。
如以下代碼,指針B通過->訪問a中的X函數,而類a則直接通過"."訪問自己的成員。
class A{
public: void X(){}; }; int main(){ A a; A *B = A; a.X(); B->X(); }
通過重載->操作符可以將“.”和“->”達到同樣的用處,這里就不多說明。
可以運行了,然后碰到各種錯,繼續調試
調試完成了,可以實現添加,刪除,查找,輸出的簡單學生信息管理系統,大功告成!
1 /* 2 學生成績管理系統 3 功能如下: 4 1.添加學生數據 5 2.查詢學生成績 6 3.修改學生成績 7 4.刪除對應學生成績 8 */ 9 10 #include<iostream> 11 #include <string> 12 #include<iomanip> 13 #define NULL 0 14 15 using namespace std; 16 17 class StudentData{ 18 public: 19 string name; 20 int math,english,lang,chemistry,biology,physics,number; 21 StudentData *next; 22 StudentData *pre; 23 void insertdate(){ 24 cin>>lang>>math>>english>>physics>>chemistry>>biology; 25 } 26 void setlang(){cin >> lang;} 27 void setmath(){cin >> math;} 28 void setenglish(){cin >> english;} 29 void setphysics(){cin >> physics;} 30 void setchemistry(){cin >> chemistry;} 31 void setbiology(){cin >> biology;} 32 }; 33 34 class linklist{ 35 public: 36 StudentData *head,*present; 37 void insert_one(); 38 void delete_data(); 39 void setdata(); 40 bool check(); 41 void creat(); 42 void output(); 43 void setmore(); 44 }; 45 46 void linklist::output(){ 47 cout<<" 姓名 學號 語文 數學 英語 物理 化學 生物"<<endl; 48 present = head; 49 while(present->next != NULL){ 50 cout<<setw(12) 51 <<present->name<<setw(12)<<present->number<<setw(8)<<present->lang<<setw(8)<<present->math<<setw(8)<<present->english 52 <<setw(8)<<present->physics<<setw(8)<<present->chemistry<<setw(8)<<present->biology<<endl; 53 present = present->next; 54 } 55 } 56 57 void linklist::creat(){ 58 present = new StudentData; 59 head = present; 60 present->next = NULL; 61 present->pre = NULL; 62 } 63 64 void linklist::insert_one(){ 65 present = new StudentData; 66 head->pre = present; 67 present->next = head; 68 head = present; 69 present->pre = NULL; 70 cout<<"請輸入學生姓名: "; 71 cin>>present->name; 72 cout<<"請輸入學號:"; 73 cin>>present->number; 74 cout<<"請一次輸入語文,數學,英語,物理,化學,生物各科成績:"<<endl; //插入成績 75 present->insertdate(); //輸入成績 76 } 77 78 void linklist::delete_data(){ 79 string studentname; 80 int sigh = 0; 81 cout<<"請輸入要刪除的學生姓名: "; 82 if (check()) { 83 cout<<"刪除數據成功!"<<endl; 84 delete present; 85 } 86 else cout<<"刪除失敗。"<<endl; 87 } 88 89 bool linklist::check(){ 90 present = head; 91 string thename; 92 cin>>thename; 93 while (present->next != NULL) 94 { 95 if (thename == present->name){ 96 cout<<" 姓名 學號 語文 數學 英語 物理 化學 生物"<<endl<<setw(12) 97 <<present->name<<setw(12)<<present->number<<setw(8)<<present->lang<<setw(8)<<present->math<<setw(8)<<present->english 98 <<setw(8)<<present->physics<<setw(8)<<present->chemistry<<setw(8)<<present->biology<<endl; 99 return true; 100 } 101 else { 102 present = present->next; 103 continue; 104 } 105 } 106 cout<<"查無此人!"<<endl; 107 return false; 108 } 109 110 void linklist::setdata(){ 111 string the_name; 112 int subject; 113 cout<<"請輸入學生姓名: "; 114 if (check()){ 115 while(1){ 116 cout<<"1.語文 2.數學"<<endl 117 <<"3.英語 4.物理"<<endl 118 <<"5.化學 6. 生物"<<endl; 119 cout<<"請輸入需要修改的科目編號: "; 120 cin >> subject; 121 switch (subject) 122 { 123 case 1:present->setlang();break; 124 case 2:present->setmath();break; 125 case 3:present->setenglish();break; 126 case 4:present->setphysics();break; 127 case 5:present->setchemistry();break; 128 case 6:present->setbiology();break; 129 default: { 130 cout<<"回到上級菜單。"<<endl; 131 return; 132 } 133 } 134 cout<<"修改成功!"<<endl; 135 } 136 } 137 } 138 139 void linklist::setmore(){ 140 cout<<"請輸入添加學生人數: "; 141 int n; 142 cin>>n; 143 while(n--) insert_one(); 144 } 145 146 int main(){ 147 linklist X; 148 cout<<"歡迎來到我的學生信息管理系統,請輸入相應序號進行操作:"<<endl 149 <<"輸入0退出系統"<<endl 150 <<"1.**********添加學生信息"<<endl<<"2.**********查詢學生信息"<<endl 151 <<"3.**********刪除學生信息"<<endl<<"4.****將學生信息列表輸出"<<endl 152 <<"5.**********修改學生信息"<<endl<<"6.**********批量插入學籍"; 153 X.creat(); 154 while (true){ 155 int choise; 156 cout<<"請輸入操作序號: "<<endl; 157 cin>>choise; 158 switch (choise) 159 { 160 case 1: 161 X.insert_one();break; 162 case 2:{ 163 cout<<"請輸入查詢的姓名: "; 164 X.check(); 165 break; 166 } 167 case 3: 168 X.delete_data();break; 169 case 4: 170 X.output();break; 171 case 5: 172 X.setdata();break; 173 case 6: 174 X.setmore();break; 175 case 0: 176 break; 177 default: 178 cout<<"對不起,有關功能正在開發!^-^"<<endl;break; 179 } 180 if (!choise) break; 181 } 182 183 return 0; 184 }
希望有路過的神牛不吝賜教~
