1 #include <stdio.h> 2 #include <string.h> 3 #include <stdlib.h> 4 #include <unistd.h> 5 6 struct Student 7 { 8 char id[4]; 9 char name[10]; 10 int score; 11 }; 12 13 typedef struct User 14 { 15 char userName[20]; 16 char userPassword[10]; 17 }UserInfo; 18 19 void inputNameAndPassword(); 20 void logon(); 21 void creatDataBace(); 22 void selectServe(); 23 void checkAchievement(); 24 void addAchievement(); 25 void delectAchievement(); 26 void changeAchievement(); 27 void sortAchievement(); 28 void start(); 29 30 int main(int argc, const char * argv[]) 31 { 32 start(); 33 //清屏未實現 34 system("clear"); 35 36 FILE *file; 37 file=fopen("/Users/5000/Desktop/C:OC作業/Final Project/achievement.txt", "r+"); 38 struct Student aStudent; 39 if (fread(&aStudent, sizeof(struct Student), 1, file)==0) { 40 creatDataBace(); 41 } 42 fclose(file); 43 //清屏未實現 44 45 selectServe(); 46 47 return 0; 48 } 49 50 void start() 51 { 52 int land; 53 printf("1.注冊\n2.登陸\n"); 54 scanf("%d",&land); 55 if (land==1) { 56 logon(); 57 }else 58 { 59 if (land==2) 60 inputNameAndPassword(); 61 else 62 { 63 printf("輸入有誤,請重新選擇\n"); 64 start(); 65 } 66 } 67 } 68 69 void logon() 70 { 71 UserInfo userInput; 72 printf("\n------------------注冊用戶------------------"); 73 printf("\n請輸入注冊用戶名:"); 74 scanf("%s",userInput.userName); 75 printf("請輸入密碼:"); 76 scanf("%s",userInput.userPassword); 77 FILE *fp; 78 if((fp=fopen("/Users/5000/Desktop/C:OC作業/Final Project/user_password.txt", "a+"))==NULL) 79 { 80 printf("注冊失敗!"); 81 exit(0); 82 } 83 if ((fwrite(&userInput, sizeof(UserInfo), 1, fp))!=1) { 84 printf("注冊失敗!"); 85 exit(0); 86 } 87 fclose(fp); 88 inputNameAndPassword(); 89 } 90 91 void inputNameAndPassword() 92 { 93 UserInfo userInput,userBase[50]; 94 int i=0,count=0,chance=0; 95 96 FILE *fp; 97 if((fp=fopen("/Users/5000/Desktop/C:OC作業/Final Project/user_password.txt", "r"))==NULL) 98 { 99 printf("登陸失敗!"); 100 exit(0); 101 } 102 103 while(fread(&userBase[i], sizeof(UserInfo), 1, fp)!=0) 104 i++; 105 count=i; 106 fclose(fp); 107 108 printf("\n------------------用戶登陸------------------\n"); 109 while (1) 110 { 111 printf("用戶名:"); 112 scanf("%s",userInput.userName); 113 printf("密碼:"); 114 scanf("%s",userInput.userPassword); //密碼用星號代替未實現 115 116 for (i=0; i<count; i++) { 117 if((strcmp(userInput.userName, userBase[i].userName)==0)&&(strcmp(userInput.userPassword, userBase[i].userPassword)==0)) 118 return; 119 } 120 if (chance!=3) 121 printf("\n用戶名或密碼輸入錯誤,請重新輸入(還有 %d 次機會):\n",3-chance); 122 chance++; 123 if (chance>=4) { 124 printf("\n嘗試超出次數!退出程序。"); 125 exit(0); 126 } 127 } 128 } 129 130 void selectServe() 131 { 132 int serveNumber; 133 printf("\n------------------操作菜單------------------"); 134 printf("\n1.查詢學生成績\n2.添加學生成績\n3.刪除學生成績\n4.修改學生成績\n5.查看成績排名\n"); 135 printf("\n請選擇您需要的操作(輸入序號選擇,輸入其它退出程序):"); 136 scanf("%d",&serveNumber); 137 switch (serveNumber) { 138 case 1: 139 checkAchievement(); 140 break; 141 case 2: 142 addAchievement(); 143 break; 144 case 3: 145 delectAchievement(); 146 break; 147 case 4: 148 changeAchievement(); 149 break; 150 case 5: 151 sortAchievement(); 152 break; 153 default: 154 break; 155 } 156 printf("\n ^^ 再見....."); 157 } 158 159 void creatDataBace() 160 { 161 FILE *fp; 162 struct Student student[100]; 163 int flag=0; 164 int i=0; 165 int k=0; 166 int count=0; 167 int m=0; 168 char ch; 169 170 if((fp=fopen("/Users/5000/Desktop/C:OC作業/Final Project/achievement.txt", "r"))==NULL) 171 { 172 printf("打開文件錯誤!"); 173 exit(0); 174 } 175 176 for (i=0;fread(&student[i], sizeof(struct Student), 1, fp)!=0;i++); 177 count=i; 178 fclose(fp); 179 180 printf("\n------------------初始化數據庫------------------"); 181 i=0; 182 while (1) { 183 printf("\n請輸入學生的基本信息:\n"); 184 do 185 { 186 printf("第 %d 個學生的ID號:",i+1); 187 scanf("%s",student[i].id); 188 189 for (k=i-1; k>=0; k--) { 190 if(strcmp(student[i].id, student[k].id)==0) 191 { 192 printf("\nID號已存在,請重新輸入\n"); 193 flag=1; 194 break; 195 } 196 } 197 198 for (k=0; k<count; k++) { 199 if(strcmp(student[i].id, student[k].id)==0) 200 { 201 printf("\nID號在整個數據庫中已存在,請重新輸入\n"); 202 flag=1; 203 break; 204 } 205 } 206 }while(flag); 207 208 do 209 { 210 flag=0; 211 printf("學生名稱:"); 212 scanf("%s",student[i].name); 213 }while (flag); 214 215 do 216 { 217 flag=0; 218 printf("學生成績:"); 219 scanf("%d",&student[i].score); 220 if (student[i].score>100||student[i].score<0) { 221 flag=1; 222 continue; 223 } 224 }while (flag); 225 226 i++; 227 m++; 228 printf("\n是否繼續創建學生成績信息?輸入'y'或'Y'繼續,輸入其它進入下一步:"); 229 getchar(); 230 scanf("%c",&ch); 231 if (ch!='Y'&&ch!='y') 232 break; 233 } 234 235 236 if((fp=fopen("/Users/5000/Desktop/C:OC作業/Final Project/achievement.txt", "a"))==NULL) 237 { 238 printf("打開文件錯誤!"); 239 exit(0); 240 } 241 242 for (i=0; i<m; i++) { 243 if (fwrite(&student[i], sizeof(struct Student), 1, fp)!=1) 244 printf("數據保存失敗"); 245 } 246 247 fclose(fp); 248 } 249 250 void checkAchievement() 251 { 252 struct Student student; 253 char id[4]; 254 FILE *fp; 255 256 printf("\n------------------查詢學生成績------------------\n"); 257 printf("請輸入需要查詢的學生ID:"); 258 scanf("%s",id); 259 while (1) 260 { 261 if ((fp=fopen("/Users/5000/Desktop/C:OC作業/Final Project/achievement.txt","r"))==NULL) 262 { 263 printf("打開文件錯誤!"); 264 selectServe(); 265 } 266 267 while (fread(&student, sizeof(struct Student), 1, fp)!=0) { 268 if (strcmp(id,student.id)==0) { 269 printf("ID:%s 姓名:%s 分數:%d\n",student.id,student.name,student.score); 270 break; 271 } 272 } 273 274 if (strcmp(id,student.id)!=0) 275 printf("沒查找到ID為 %s 的學生信息。\n",id); 276 277 printf("\n請輸入需要查詢的學生ID(輸入n返回操作菜單):"); 278 scanf("%s",id); 279 if (strcmp(id,"n")==0||strcmp(id,"N")==0) { 280 fclose(fp); 281 selectServe(); 282 } 283 fclose(fp); 284 } 285 } 286 287 void addAchievement() 288 { 289 FILE *fp; 290 struct Student student[100]; 291 int i=0; 292 int count=0; 293 char ch; 294 295 if((fp=fopen("/Users/5000/Desktop/C:OC作業/Final Project/achievement.txt", "r"))==NULL) 296 { 297 printf("打開文件錯誤!"); 298 selectServe(); 299 } 300 301 for (i=0;fread(&student[i], sizeof(struct Student), 1, fp)!=0;i++); 302 count=i; 303 fclose(fp); 304 305 if((fp=fopen("/Users/5000/Desktop/C:OC作業/Final Project/achievement.txt", "a+"))==NULL) 306 { 307 printf("打開文件錯誤!"); 308 selectServe(); 309 } 310 311 printf("\n------------------添加學生成績------------------"); 312 while (1) 313 { 314 int flag; 315 do { 316 flag=0; 317 printf("\n請輸入需要添加的學生ID:"); 318 scanf("%s",student[count].id); 319 for (int k=0; k<count; k++) { 320 if(strcmp(student[count].id, student[k].id)==0) 321 { 322 printf("ID號在數據庫中已存在,請重新輸入。\n"); 323 flag=1; 324 break; 325 } 326 } 327 } while (flag); 328 printf("請輸入需要添加的學生姓名:"); 329 scanf("%s",student[count].name); 330 printf("請輸入需要添加的學生分數:"); 331 scanf("%d",&student[count].score); 332 if (fwrite(&student[count], sizeof(struct Student), 1, fp)!=1) { 333 printf("添加失敗!"); 334 } 335 printf("添加成功!\n"); 336 printf("\n是否繼續添加學生成績信息?輸入y繼續,輸入其它返回操作菜單:"); 337 getchar(); 338 scanf("%c",&ch); 339 if (ch!='y'&&ch!='Y') { 340 fclose(fp); 341 selectServe(); 342 } 343 count++; 344 } 345 } 346 347 void delectAchievement() 348 { 349 char id[4]; 350 char sure; 351 FILE *infile,*outfile; 352 struct Student students[100]; 353 int i=0,flag=-1,count=0; 354 355 356 printf("\n------------------刪除學生成績------------------"); 357 358 if ((infile=fopen("/Users/5000/Desktop/C:OC作業/Final Project/achievement.txt","r"))==NULL) 359 { 360 printf("打開文件錯誤!"); 361 selectServe(); 362 } 363 364 while (fread(&students[i], sizeof(struct Student), 1, infile)!=0) i++; 365 count=i; 366 fclose(infile); 367 368 printf("\n請輸入需要刪除的學生ID:"); 369 scanf("%s",id); 370 while (1) 371 { 372 for (i=0; i<count; i++) { 373 if (strcmp(id,students[i].id)==0) { 374 flag=i; 375 break; 376 } 377 } 378 379 if (flag==-1) 380 printf("沒查找到ID為 %s 的學生信息。",id); 381 else 382 { 383 printf("確認要刪除ID為 %s 的學生信息嗎?(y確定 n取消)",id); 384 getchar(); 385 scanf("%c",&sure); 386 if (sure=='y') 387 { 388 for (i=flag; i<count-1; i++) { 389 students[i]=students[i+1]; 390 } 391 count--; 392 393 outfile=fopen("/Users/5000/Desktop/C:OC作業/Final Project/temp.txt","w+"); 394 for (i=0; i<count; i++) { 395 if(fwrite(&students[i], sizeof(struct Student), 1, outfile)!=1) 396 { 397 printf("刪除失敗!"); 398 selectServe(); 399 } 400 } 401 fclose(outfile); 402 } 403 printf("刪除成功!\n"); 404 } 405 406 printf("\n請輸入需要刪除的學生ID(輸入n返回操作菜單):"); 407 scanf("%s",id); 408 409 if (strcmp(id,"n")==0||strcmp(id,"N")==0) { 410 /*其實用w打開原來文件就可以直接寫入原來的文件,w打開會清空原來文件的內容*/ 411 unlink("/Users/5000/Desktop/C:OC作業/Final Project/achievement.txt");//#include <unistd.h> 412 rename( "/Users/5000/Desktop/C:OC作業/Final Project/temp.txt","/Users/5000/Desktop/C:OC作業/Final Project/achievement.txt");//#include <unistd.h> 413 selectServe(); 414 } 415 } 416 } 417 418 void changeAchievement() 419 { 420 FILE *fp; 421 char id[4],ch; 422 int score=0,count=0,flag=1; 423 int i=0; 424 struct Student students[100]; 425 426 if ((fp=fopen("/Users/5000/Desktop/C:OC作業/Final Project/achievement.txt","r"))==NULL) 427 { 428 printf("打開文件錯誤!"); 429 selectServe(); 430 } 431 while (fread(&students[i], sizeof(struct Student), 1, fp)!=0) i++; 432 count=i; 433 fclose(fp); 434 435 printf("\n------------------學生成績排名------------------\n"); 436 while (1) 437 { 438 printf("請輸入需要修改成績的學生ID:"); 439 scanf("%s",id); 440 441 442 for (i=0;i<count;i++) { 443 if (strcmp(id,students[i].id)==0) { 444 printf("ID為 %s 的%s同學,分數為:%d\n",students[i].id,students[i].name,students[i].score); 445 printf("更改%s同學的分數值為:",students[i].name); 446 scanf("%d",&score); 447 students[i].score=score; 448 flag=0; 449 break; 450 } 451 } 452 453 if (flag) 454 printf("沒查找到ID為 %s 的學生信息。\n",id); 455 456 if ((fp=fopen("/Users/5000/Desktop/C:OC作業/Final Project/achievement.txt","w+"))==NULL) 457 { 458 printf("打開文件錯誤!"); 459 selectServe(); 460 } 461 for (i=0; i<count; i++) { 462 if(fwrite(&students[i], sizeof(struct Student), 1, fp)!=1) 463 { 464 printf("修改失敗!"); 465 selectServe(); 466 } 467 } 468 469 printf("\n是否繼續查詢學生成績信息?輸入'y'或'Y'繼續,輸入其它返回操作菜單:"); 470 getchar(); 471 scanf("%c",&ch); 472 if (ch!='y'&&ch!='Y') { 473 fclose(fp); 474 selectServe(); 475 } 476 } 477 } 478 479 void sortAchievement() 480 { 481 char ch; 482 FILE *fp; 483 struct Student students[100],temp; 484 int i=0,j=0,count=0; 485 486 if ((fp=fopen("/Users/5000/Desktop/C:OC作業/Final Project/achievement.txt","r"))==NULL) 487 { 488 printf("打開文件錯誤!"); 489 selectServe(); 490 } 491 492 while (fread(&students[i], sizeof(struct Student), 1, fp)!=0) i++; 493 fclose(fp); 494 count=i; 495 496 printf("\n------------------學生成績排名------------------\n"); 497 for(i=0;i<count-1;i++) 498 for(j=0;j<count-1-i;j++) 499 { 500 if (students[j].score<students[j+1].score) { 501 temp=students[j]; 502 students[j]=students[j+1]; 503 students[j+1]=temp; 504 } 505 } 506 printf("ID Name Score\n"); //Id****Name******Score 507 for (i=0; i<count; i++) { 508 printf("%s",students[i].id); 509 for (j=0; j<6-strlen(students[i].id); j++) 510 printf(" "); 511 512 printf("%s",students[i].name); 513 for (j=0; j<10-strlen(students[i].name); j++) 514 printf(" "); 515 516 printf("%d\n",students[i].score); 517 } 518 519 printf("\n輸入任意鍵返回操作菜單:"); 520 getchar(); //接收選擇操作4時輸入的回車 521 scanf("%c",&ch); 522 selectServe(); 523 524 }