實驗設備信息管理系統
簡單的思路,簡單的算法
題目簡述:實驗室設備信息用文件存儲,提供文件的輸入輸出操作;要能夠完成設備的錄入和修改,需要提供設備添加和修改操作;實現對設備進行分類統計,需要提供排序操作;實現對設備的查詢需要提供查找操作。
結構體定義如下:
typedef struct equipmentInfo { char equipCode[10]; //設備編號
char equipType[20]; //設備總類
char equipName[20]; //設備名稱
char equipPrice[20]; //設備價格
char buyDate[20]; //設備購入日期
int scrap; //是否報廢,0表示沒有報廢,1表示報廢
char scrapDate[20]; //報廢日期
}EquInfo;
在此做了七個模塊,分別是:實驗設備信息輸入模塊、實驗設備信息添加模塊、實驗設備信息修改模塊、實驗設備分類統計模塊、實驗設備查詢模塊、刪除模塊。
函數定義如下:
int Scaninfor();//瀏覽設備信息
int Inputinformation();//設備信息輸入模塊
int Addinfor();//設備信息添加模塊
int Modifyinfor();//設備信息修改模塊
int Classifyinfor();//設備分類統計模塊
int Searchinfor();//設備查詢模塊
int Deleteinfor();//刪除模塊
設計思路:
1.設備信息輸入模塊為新建文件,已存在的文件會被覆蓋;
2.設備信息添加模塊,向文件尾部添加信息;
3.修改信息模塊,先選擇需要修改的設備編號,再選擇需要修改的內容;
4.統計模塊,計數功能(源碼中提供了排序的代碼,但沒有將排序后的寫入文件);
5.查詢模塊,查到就會輸出;
6.刪除模塊,暫時只提供了按照設備編碼刪除的功能;
7.瀏覽,任何時候都可以瀏覽文件內的信息;
8.由main函數提供菜單,用戶選擇功能,功能實現后,函數返回用戶的按鍵的ASCII碼,判斷繼續執行main函數or結束程序。
遇到的問題及解決辦法:
1.信息輸入%s用的市scanf(),如果在某一個字符串中輸入空格,將會出錯。所以改為gets(),注意若在scanf()后使用,需要提前getchar();
2.信息修改模塊,一開始提供的修改是讓用戶重新輸入所有信息,改為重新輸入需要修改的相應信息更高效;
3.刪除模塊,最好可以實現分類刪除,比如刪除所有已經報廢的設備信息,刪除在某個日期之前或某個時間段購入的設備信息等;
4.函數返回值判斷是否結束程序時,最早只能輸入數字,輸入別的會出錯(因為最后用戶輸入的時候我用的是%d啦)。改為%c后可以按鍵盤的 any key 了;
5.輸出格式問題,雖然我在printf()內使用了\t,但后來發現某項內容長度不一樣(本身長度有的不到8個字節,有的超過了8個字節),這樣會發生錯位。輸出格式%s可以改為%-15s(左對齊),這樣就可以了。
6.歡迎提出問題繼續改進~
源代碼:
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <math.h>
4 #include <string.h>
5 #include <conio.h>
6 #define N 20
7 typedef struct equipmentInfo 8 { 9 char equipCode[10]; //設備編號
10 char equipType[20]; //設備總類
11 char equipName[20]; //設備名稱
12 char equipPrice[20]; //設備價格
13 char buyDate[20]; //設備購入日期
14 int scrap; //是否報廢,0表示沒有報廢,1表示報廢
15 char scrapDate[20]; //報廢日期
16 }EquInfo; 17 EquInfo equip[N]; 18 int Scaninfor();//瀏覽設備信息
19 int Inputinformation();//設備信息輸入模塊
20 int Addinfor();//設備信息添加模塊
21 int Modifyinfor();//設備信息修改模塊
22 int Classifyinfor();//設備分類統計模塊
23 int Searchinfor();//設備查詢模塊
24 int Deleteinfor();//刪除模塊
25 int i = 0; 26 int main() 27 { 28 printf("*************************************\n"); 29 printf("***** 輸入對應數字標號選擇菜單\t*****\n"); 30 printf("*****\t[0] 瀏覽實驗設備信息\t*****\n"); 31 printf("*****\t[1] 輸入實驗設備信息\t*****\n"); 32 printf("*****\t[2] 添加實驗設備信息\t*****\n"); 33 printf("*****\t[3] 修改實驗設備信息\t*****\n"); 34 printf("*****\t[4] 設備信息分類統計\t*****\n"); 35 printf("*****\t[5] 查詢實驗設備信息\t*****\n"); 36 printf("*****\t[6] 刪除實驗設備信息\t*****\n"); 37 printf("*****\t[7] 退出 \t*****\n"); 38 printf("*************************************\n"); 39 int menu,t; 40 do
41 { 42 printf("請輸入數字標號:\n"); 43 scanf("%d",&menu); 44 }while(menu < 0 || menu > 6); 45 switch(menu) 46 { 47 case 0: 48 t = Scaninfor();break; 49 case 1: 50 t = Inputinformation();break; 51 case 2: 52 t = Addinfor();break; 53 case 3: 54 t = Modifyinfor();break; 55 case 4: 56 t = Classifyinfor();break; 57 case 5: 58 t = Searchinfor();break; 59 case 6: 60 t = Deleteinfor();break; 61 case 7: 62 exit(0);break; 63 default: 64 printf("INPUT ERROR !"); 65 } 66 getchar(); 67 if(t == 48) 68 return 0; 69 else
70 main(); 71 } 72 int Scaninfor() 73 { 74 FILE *fp; 75 int j,k; 76 char a; 77 if((fp = fopen("equipInfor.txt","r")) == NULL) 78 { 79 printf("Failure to open equipInfor.txt!\n"); 80 exit(0); 81 } 82 for(k = 0;!feof(fp);k++) 83 { 84 fread(&equip[k], sizeof(EquInfo), 1, fp); 85 } 86 printf("code\ttype \t\tname\tprice\tbuydate \tscrap\tscrapdate\n\n\n"); 87 for(j = 0;j < k-1;j++) 88 { 89 printf("%s\t%-15s\t%-7s\t%s\t%s\t%d\t%s\n\n",equip[j].equipCode, equip[j].equipType, equip[j].equipName, equip[j].equipPrice, equip[j].buyDate, equip[j].scrap, equip[j].scrapDate); 90 } 91 fclose(fp); 92 printf("press 0 to exit or other key to return menu:\n"); 93 scanf(" %c",&a); 94 return a; 95 } 96 int Inputinformation() 97 { 98 int n; 99 char a; 100 printf("Please input the number of equipments:"); 101 scanf("%d",&n); 102 getchar(); 103 printf("Please input the equipCode, equipType, equipName,"); 104 printf(" equipPrice, buyDate, scrap(no 0 or yes 1), scrapDate:\n"); 105 do
106 { 107 printf("please input the information of %d:\n",i+1); 108 strcpy(equip[i].scrapDate, "no scrap"); 109 printf("please input the equipCode:\n"); 110 gets(equip[i].equipCode); 111 printf("please input the equipType:\n"); 112 gets(equip[i].equipType); 113 printf("please input the equipName:\n"); 114 gets(equip[i].equipName); 115 printf("please input the equipPrice:\n"); 116 gets(equip[i].equipPrice); 117 printf("please input the buyDate:\n"); 118 gets(equip[i].buyDate); 119 printf("please input is(1) or not(0) scrap:\n"); 120 scanf("%d",&equip[i].scrap); 121 getchar(); 122 if(equip[i].scrap == 1) 123 { 124 printf("please input the scrap date:\n"); 125 gets(equip[i].scrapDate); 126 } 127 i++; 128 }while(i < n); 129 FILE *fp; 130 if((fp = fopen("equipInfor.txt","w")) == NULL) 131 { 132 printf("Failure to open equipInfor.txt!\n"); 133 exit(0); 134 } 135 fwrite(equip, sizeof(EquInfo), n, fp); 136 fclose(fp); 137 printf("press 0 to exit or other key to return menu:\n"); 138 scanf(" %c",&a); 139 return a; 140 } 141 int Addinfor() 142 { 143 int k = 0,j; 144 FILE *fp; 145 if((fp = fopen("equipInfor.txt","a")) == NULL) 146 { 147 printf("Failure to open equipInfor.txt!\n"); 148 exit(0); 149 } 150 int n; 151 char a; 152 printf("Please input the number of the adding equipments:"); 153 scanf("%d",&n); 154 getchar(); 155 printf("Please input the equipCode,equipType,equipName,"); 156 printf("equipPrice,buyDate,scrap(no 0 or yes 1),scrapDate:\n"); 157 do
158 { 159 printf("please input the adding information of %d:\n",k+1); 160 strcpy(equip[k].scrapDate, "no scrap"); 161 printf("please input the equipCode:\n"); 162 gets(equip[k].equipCode); 163 printf("please input the equipType:\n"); 164 gets(equip[k].equipType); 165 printf("please input the equipName:\n"); 166 gets(equip[k].equipName); 167 printf("please input the equipPrice:\n"); 168 gets(equip[k].equipPrice); 169 printf("please input the buyDate:\n"); 170 gets(equip[k].buyDate); 171 printf("please input is(1) or not(0) scrap:\n"); 172 scanf("%d",&equip[k].scrap); 173 getchar(); 174 if(equip[k].scrap == 1) 175 { 176 printf("please input the scrap date:\n"); 177 gets(equip[k].scrapDate); 178 } 179 k++; 180 }while(k < n); 181 fseek(fp,0,SEEK_END); 182 for(j = 0;j<n;j++) 183 fwrite(&equip[j], sizeof(EquInfo), 1, fp); 184 fclose(fp); 185 printf("press 0 to exit or other key to return menu:\n"); 186 scanf(" %c",&a); 187 return a; 188 } 189 int Modifyinfor() 190 { 191 FILE *fp; 192 int k,j,a,b,l; 193 char c; 194 char code[20]; 195 getchar(); 196 printf("please input the equipCode of the equipment which you want to modify:\n"); 197 gets(code); 198 if((fp = fopen("equipInfor.txt","r")) == NULL) 199 { 200 printf("Failure to open equipInfor.txt!\n"); 201 exit(0); 202 } 203 for(k = 0;!feof(fp);k++) 204 { 205 fread(&equip[k], sizeof(EquInfo), 1, fp); 206 } 207 for(j = 0;j < k;j++) 208 { 209 a = strcmp(equip[j].equipCode,code); 210 if(a == 0) 211 { 212 printf("Which information do you want to modify?\n"); 213 printf("***[1] equipcode\t***\n"); 214 printf("***[2] equiptype\t***\n"); 215 printf("***[3] equipname\t***\n"); 216 printf("***[4] equipprice\t***\n"); 217 printf("***[5] buydate \t***\n"); 218 printf("***[6] scrap \t***\n"); 219 printf("***[7] scrapdate\t***\n"); 220 printf("please input the number:"); 221 scanf("%d",&b); 222 getchar(); 223
224 switch(b) 225 { 226 case 1: 227 printf("please input the equipCode:\n"); 228 gets(equip[j].equipCode); 229 break; 230 case 2: 231 printf("please input the equipType:\n"); 232 gets(equip[j].equipType); 233 break; 234 case 3: 235 printf("please input the equipName:\n"); 236 gets(equip[j].equipName); 237 break; 238 case 4: 239 printf("please input the equipPrice:\n"); 240 gets(equip[j].equipPrice); 241 break; 242 case 5: 243 printf("please input the buyDate:\n"); 244 gets(equip[j].buyDate); 245 break; 246 case 6: 247 printf("please input is(1) or not(0) scrap:\n"); 248 scanf("%d",&equip[j].scrap); 249 getchar(); 250 if(equip[j].scrap == 1) 251 { 252 printf("please input the scrap date:\n"); 253 gets(equip[j].scrapDate); 254 } 255 else
256 strcpy(equip[j].scrapDate, "no scrap"); 257 break; 258 case 7: 259 printf("please input the scrap date:\n"); 260 gets(equip[j].scrapDate); 261 break; 262 default: 263 printf("INPUT ERROR !\n"); 264 } 265 break; 266 } 267 } 268 fclose(fp); 269 if(a) 270 printf("don't find the equipCode that you input\n"); 271 else
272 { 273 if((fp = fopen("equipInfor.txt","w")) == NULL) 274 { 275 printf("Failure to open equipInfor.txt!\n"); 276 exit(0); 277 } 278 for(l = 0;l < k-1;l++) 279 fwrite(&equip[l], sizeof(EquInfo), 1, fp); 280 fclose(fp); 281 printf("MODIFY SUCCESSFULLY !\n"); 282 } 283 printf("press 0 to exit or other key to return menu:\n"); 284 scanf(" %c",&c); 285 return a; 286 } 287 int Classifyinfor() 288 { 289 char a; 290 FILE *fp; 291 int q,count = 0,j,k; 292 int total; 293 char s[N],temp[N],*te; 294 printf("*****\t[1] 設備種類\t*****\n"); 295 printf("*****\t[2] 設備名稱\t*****\n"); 296 printf("*****\t[3] 購買日期\t*****\n"); 297 printf("請輸入你想要的分類方式:"); 298 scanf("%d",&q); 299 getchar(); 300 if((fp = fopen("equipInfor.txt","r")) == NULL) 301 { 302 printf("Failure to open equipInfor.txt!\n"); 303 exit(0); 304 } 305 for(total = 0;!feof(fp);total++) 306 { 307 fread(&equip[total], sizeof(EquInfo), 1, fp); 308 } 309 switch(q) 310 { 311 case 1: 312 printf("please input the equipType:\n"); 313 gets(s); 314 for(j = 0;j < total-1;j++) 315 { 316 for(k = 1;k < total;k++) 317 { 318 if(strcmp(equip[k].equipType,equip[j].equipType) < 0) 319 { 320 strcpy(temp, equip[k].equipType); 321 strcpy(equip[k].equipType, equip[j].equipType); 322 strcpy(equip[j].equipType, temp); 323 } 324 } 325 } 326 for(j = 0;j < total;j++) 327 { 328 if(strcmp(s,equip[j].equipType) == 0) 329 { 330 count++; 331 } 332 } 333 printf("%s類型的實驗儀器有%d台\n",s,count); 334 break; 335 case 2: 336 printf("please input the equipName:\n"); 337 gets(s); 338 for(j = 0;j < total - 1;j++) 339 { 340 for(k = 1;k < total;k++) 341 { 342 if(strcmp(temp,equip[j].equipName) < 0) 343 { 344 strcpy(temp, equip[k].equipName); 345 strcpy(equip[k].equipName, equip[j].equipName); 346 strcpy(equip[j].equipName, temp); 347 } 348 } 349 } 350 for(j = 0;j < total;j++) 351 { 352 if(strcmp(s,equip[j].equipName) == 0) 353 { 354 count++; 355 } 356 } 357 printf("%s名稱的儀器有%d台\n",s,count); 358 break; 359 case 3: 360 printf("please input the buyDate:\n"); 361 gets(s); 362 for(j = 0;j < total - 1;j++) 363 { 364 for(k = 1;k < total;k++) 365 { 366 if(strcmp(s, equip[j].buyDate) < 0) 367 { 368 strcpy(temp, equip[k].buyDate); 369 strcpy(equip[k].buyDate, equip[j].buyDate); 370 strcpy(equip[j].buyDate, temp); 371 } 372 } 373 } 374 for(j = 0;j < total;j++) 375 { 376 if(strcmp(s,equip[j].buyDate) == 0) 377 { 378 count++; 379 } 380 } 381 printf("%s日期購買的儀器有%d台\n",s,count); 382 break; 383 default: 384 printf("INPUT ERROR !\n"); 385 } 386 printf("press 0 to exit or other key to return menu:\n"); 387 scanf(" %c",&a); 388 return a; 389 } 390 int Searchinfor() 391 { 392 int n,m,k; 393 char a; 394 FILE *fp; 395 printf("*************************************\n"); 396 printf("*****\t[1] 按設備編號查詢\t*****\n"); 397 printf("*****\t[2] 按設備種類查詢\t*****\n"); 398 printf("*****\t[3] 按設備名稱查詢\t*****\n"); 399 printf("*****\t[4] 按設備購入日期查詢\t*****\n"); 400 printf("*****\t[5] 按設備狀態查詢\t*****\n"); 401 printf("*************************************\n"); 402 printf("請輸入所需要的查詢方式:"); 403 scanf("%d",&n); 404 getchar(); 405 if((fp = fopen("equipInfor.txt","r")) == NULL) 406 { 407 printf("Failure to open equipInfor.txt!\n"); 408 exit(0); 409 } 410 for(k = 0;!feof(fp);k++) 411 { 412 fread(&equip[k], sizeof(EquInfo), 1, fp); 413 } 414 int j,flag,sc; 415 switch(n) 416 { 417 case 1: 418 flag = 0; 419 char code[N]; 420 printf("please input the equipCode:"); 421 gets(code); 422 for(j = 0;j < k-1;j++) 423 { 424 if(strcmp(equip[j].equipCode, code) == 0) 425 { 426 printf("%s\t%s\t%s\t%s\t%s\t%d\t%s\n",equip[j].equipCode, equip[j].equipType, equip[j].equipName, equip[j].equipPrice, equip[j].buyDate, equip[j].scrap, equip[j].scrapDate); 427 flag = 1; 428 } 429 } 430 if(!flag) 431 printf("not find !"); 432 break; 433 case 2: 434 flag = 0; 435 char type[N]; 436 printf("please input the equipType:"); 437 gets(type); 438 for(j = 0;j < k-1;j++) 439 { 440 if(strcmp(equip[j].equipType, type) == 0) 441 { 442 printf("%s\t%s\t%s\t%s\t%s\t%d\t%s\n",equip[j].equipCode, equip[j].equipType, equip[j].equipName, equip[j].equipPrice, equip[j].buyDate, equip[j].scrap, equip[j].scrapDate); 443 flag = 1; 444 } 445 } 446 if(!flag) 447 printf("not find !"); 448 break; 449 case 3: 450 flag = 0; 451 char name[N]; 452 printf("please input the equipName:"); 453 gets(name); 454 for(j = 0;j < k-1;j++) 455 { 456 if(strcmp(equip[j].equipName, name) == 0) 457 { 458 printf("%s\t%s\t%s\t%s\t%s\t%d\t%s\n",equip[j].equipCode, equip[j].equipType, equip[j].equipName, equip[j].equipPrice, equip[j].buyDate, equip[j].scrap, equip[j].scrapDate); 459 flag = 1; 460 } 461 } 462 if(!flag) 463 printf("not find !"); 464 break; 465 case 4: 466 flag = 0; 467 char date[N]; 468 printf("please input the buyDate:"); 469 gets(date); 470 for(j = 0;j < k-1;j++) 471 { 472 if(strcmp(equip[j].buyDate, date) == 0) 473 { 474 printf("%s\t%s\t%s\t%s\t%s\t%d\t%s\n",equip[j].equipCode, equip[j].equipType, equip[j].equipName, equip[j].equipPrice, equip[j].buyDate, equip[j].scrap, equip[j].scrapDate); 475 flag = 1; 476 } 477 } 478 if(!flag) 479 printf("not find !"); 480 break; 481 case 5: 482 flag = 0; 483 printf("please input the scrap:"); 484 scanf("%d",&sc); 485 for(j = 0;j < k-1;j++) 486 { 487 if(equip[j].scrap == sc) 488 { 489 printf("%s\t%s\t%s\t%s\t%s\t%d\t%s\n",equip[j].equipCode, equip[j].equipType, equip[j].equipName, equip[j].equipPrice, equip[j].buyDate, equip[j].scrap, equip[j].scrapDate); 490 flag = 1; 491 } 492 } 493 if(!flag) 494 printf("not find !\n"); 495 break; 496 default: 497 printf("INPUT ERROR !"); 498 } 499 printf("press 0 to exit or other key to return menu\n"); 500 scanf(" %c",&a); 501 return a; 502 } 503 int Deleteinfor() 504 { 505 FILE *fp; 506 int k,j,a,b,l; 507 char c; 508 char code[20]; 509 getchar(); 510 printf("please input the equipCode of the equipment which you want to delete:\n"); 511 gets(code); 512 if((fp = fopen("equipInfor.txt","r")) == NULL) 513 { 514 printf("Failure to open equipInfor.txt!\n"); 515 exit(0); 516 } 517 for(k = 0;!feof(fp);k++) 518 { 519 fread(&equip[k], sizeof(EquInfo), 1, fp); 520 } 521 for(j = 0;j < k;j++) 522 { 523 a = strcmp(equip[j].equipCode,code); 524 if(a == 0) 525 { 526 b = j; 527 break; 528 } 529 } 530 fclose(fp); 531 if(j == k) 532 { 533 printf("INPUT ERROR !\nDon't find the code !\n"); 534 } 535 else
536 { 537 if((fp = fopen("equipInfor.txt","w")) == NULL) 538 { 539 printf("Failure to open equipInfor.txt!\n"); 540 exit(0); 541 } 542 for(l = 0;l < b;l++) 543 fwrite(&equip[l], sizeof(EquInfo), 1, fp); 544 for(l = b+1;l < k-1;l++) 545 fwrite(&equip[l], sizeof(EquInfo), 1, fp); 546 fclose(fp); 547 printf("DELETE SUCCESSFULLY !\n"); 548 } 549 printf("press 0 to exit or other key to return menu:\n"); 550 scanf(" %c",&c); 551 return a; 552 }