今天晚上終於做完了學生成績管理系統!激動!開心!!!哈哈哈~~~~
總共298行代碼,第一次寫這么多。
其中遇到了好多困難,也煩惱了好久,不過最終都解決了!
做了之后果然,滿滿的成就感!抑制不住的興奮,特此紀念一下!
可能還有些小問題,,,不過已經很開心啦,啦啦啦~~~

1 #include<iostream> 2 #include<iomanip> 3 4 using namespace std; 5 void menu();//菜單函數 6 int choice(int x); //選項函數 7 void Input(float stu[],int &num);//1.輸入成績函數 8 void Output(float stu[],int &num);//2.輸出成績函數 9 void Order(float stu[],int &num);//3.排序函數 10 void Search(float stu[],int &num);//4.查找學生函數 11 void Insert(float stu[],int &num);//5.插入學生函數 12 void Delete(float stu[],int &num);//6.刪除學生函數 13 void Statistical_number(float stu[],int &num);//7.統計各分數段人數函數 14 15 16 const int MAX_STUDENT_NUM=40;//班級人數 17 float stu[MAX_STUDENT_NUM]; 18 int currentN=0; 19 20 int main() 21 { 22 menu(); 23 int x; 24 cin>>x; 25 choice(x); 26 //菜單循環 27 while (x!=0) 28 { 29 30 menu(); 31 cin>>x; 32 choice(x); 33 } 34 } 35 36 37 //菜單 38 void menu() 39 { 40 cout<<"\t\t\t\t\t\t\t\t\t\t\t\t\t\n"; 41 cout<<"\t\t\t歡迎使用成績管理系統\t\n"; 42 cout<<"\t\t=========================================\n"; 43 cout<<"\t\t1.錄入學生 2.顯示信息\n"; 44 cout<<"\t\t3.排序總評 4.查找學生\n"; 45 cout<<"\t\t5.插入學生 6.刪除學生\n"; 46 cout<<"\t\t7.統計人數 0.退出\n"; 47 cout<<"\t\t=========================================\n"; 48 cout<<"請從0-7中選擇一個數:"; 49 } 50 51 //選項 52 int choice(int x) 53 { 54 55 switch (x) 56 { 57 case 0:return 0; 58 case 1:cout<<"你選擇了 錄入學生"<<endl; 59 Input(stu,currentN); 60 break; 61 case 2:cout<<"你選擇了 顯示信息"<<endl; 62 Output(stu,currentN); 63 break; 64 case 3:cout<<"你選擇了 排序總評"<<endl; 65 Order(stu,currentN); 66 break; 67 case 4:cout<<"你選擇了 查找學生"<<endl; 68 Search(stu,currentN); 69 break; 70 case 5:cout<<"你選擇了 插入學生"<<endl; 71 Insert(stu,currentN); 72 break; 73 case 6:cout<<"你選擇了 刪除學生"<<endl; 74 Delete(stu,currentN); 75 break; 76 case 7:cout<<"你選擇了 統計人數"<<endl; 77 Statistical_number(stu,currentN); 78 break; 79 default:cout<<"輸入非法,請重新輸入!\n"<<endl;break; 80 } 81 } 82 83 84 //1.輸入成績 85 void Input(float stu[],int &num) 86 { 87 char a; 88 while(1) 89 { 90 int temp=num; 91 if(temp>=MAX_STUDENT_NUM) 92 { 93 cout<<"人數已達到上限,無法再輸入!"<<endl; 94 break; 95 } 96 cout<<"請輸入成績(0-100):"<<endl; 97 cin>>stu[num]; 98 if(stu[num]<0 || stu[num]>100) 99 { 100 cout<<"輸入錯誤!成績應該在0到100之間!"<<endl; 101 continue; 102 } 103 cout<<"是否繼續輸入學生數據請按Y或N:"<<endl; 104 cin>>a; 105 num++; 106 if(a=='y'||a=='Y') 107 { 108 109 if(num>=MAX_STUDENT_NUM) 110 { 111 cout<<"人數已達到上限,無法再輸入!"<<endl; 112 break; 113 } 114 } 115 else if(a=='n'||a=='N') 116 { 117 break; 118 } 119 } 120 } 121 122 123 //2.輸出顯示信息 124 void Output(float stu[],int &num) 125 { 126 cout<<"成績信息如下:"<<endl; 127 for(int i=0;i<num;i++) 128 { 129 cout<<fixed<<setprecision(1)<<setw(5)<<stu[i]<<' '; 130 } 131 } 132 133 134 //3.排序 135 void Order(float stu[],int &num) 136 { 137 for(int i=0;i<num-1;i++) 138 for(int i=0;i<num-1;i++) 139 { 140 for(int j=0;j<num-i;j++) 141 { 142 if(stu[i]>stu[i+1]) 143 { 144 float temp=stu[i]; 145 stu[i]=stu[i+1]; 146 stu[i+1]=temp; 147 } 148 } 149 } 150 Output(stu,num); 151 } 152 153 154 //4.查找 155 void Search(float stu[],int &num) 156 { 157 int x,i,j=0,n=1,counter=1; 158 char a; 159 while(1) 160 { 161 162 cout<<"請輸入你要查找的成績:"; 163 cin>>x; 164 for(i=0;i<num;i++) 165 { 166 j++; 167 } 168 for(i=0;i<=j;i++) 169 { 170 171 if(x==stu[i]) 172 { 173 cout<<"查找到第"<<counter<<"個相符的成績"<<fixed<<setprecision(1)<<stu[i]<<endl; 174 n=0;counter++; 175 } 176 } 177 if(n) 178 cout<<"沒有該學生信息!"<<endl; 179 180 cout<<"是否繼續查找請按Y或N:"<<endl; 181 cin>>a; 182 if(a=='y'||a=='Y') 183 { 184 counter=1; 185 continue; 186 } 187 else if(a=='n'||a=='N') 188 { 189 break; 190 } 191 192 193 } 194 } 195 196 197 //5.插入 198 void Insert(float stu[],int &num) 199 { 200 char a; 201 while(1) 202 { 203 int temp=num; 204 if(temp>=MAX_STUDENT_NUM) 205 { 206 cout<<"人數已達到上限,無法再輸入!"<<endl; 207 break; 208 } 209 cout<<"請輸入插入的成績(0-100):"<<endl; 210 cin>>stu[num]; 211 if(stu[num]<0 || stu[num]>100) 212 { 213 cout<<"數據不符合要求!請重新輸入!"<<endl; 214 continue; 215 } 216 cout<<"是否繼續輸入學生數據請按Y或N:"<<endl; 217 cin>>a; 218 num++; 219 if(a=='y'||a=='Y') 220 { 221 if(num>=MAX_STUDENT_NUM) 222 { 223 cout<<"人數已達到上限,無法再輸入!"<<endl; 224 break; 225 } 226 } 227 else if(a=='n'||a=='N') 228 { 229 break; 230 } 231 } 232 } 233 234 235 //6.刪除 236 void Delete(float stu[],int &num) 237 { 238 float x; 239 char a; 240 int counter=1,n=0,temp=num; 241 while(1) 242 { 243 244 cout<<"請輸入要刪除的數據:"<<endl; 245 cin>>x; 246 for(int i=0;i<num;i++) 247 { 248 if(x==stu[i]) 249 { 250 for(int j=i;j<num;j++) 251 stu[j]=stu[j+1]; 252 num--;i--;n=1; 253 } 254 } 255 if(n) 256 { 257 n=0; 258 cout<<x*1.0<<"分學生信息刪除成功!"<<endl; 259 } 260 else 261 cout<<"沒有該學生!"<<endl; 262 263 cout<<"是否繼續刪除其他學生請按Y或N:"<<endl; 264 cin>>a; 265 if(a=='y'||a=='Y') 266 { 267 continue; 268 } 269 else if(a=='n'||a=='N') 270 { 271 Output(stu,num); 272 break; 273 } 274 275 } 276 } 277 278 279 //7.統計各分數段人數 280 void Statistical_number(float stu[],int &num) 281 { 282 int a=0,b=0,c=0,d=0,e=0; 283 for(int i=0;i<num;i++) 284 { 285 if(stu[i]>=90) 286 a++; 287 else if(stu[i]>=80) 288 b++; 289 else if(stu[i]>=70) 290 c++; 291 else if(stu[i]>=60) 292 d++; 293 else 294 e++; 295 } 296 cout<<"100^90 89^80 79^70 69^60 <60"<<endl; 297 cout<<a<<" "<<b<<" "<<c<<" "<<d<<" "<<e<<endl; 298 }