C语言 实验设备管理系统

实验设备信息管理系统

简单的思路,简单的算法  

题目简述:实验室设备信息用文件存储,提供文件的输入输出操作;要能够完成设备的录入和修改,需要提供设备添加和修改操作;实现对设备进行分类统计,需要提供排序操作;实现对设备的查询需要提供查找操作。

结构体定义如下:

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 }

 

posted @ 2018-07-06 15:05  落月成孤倚♪  阅读(3352)  评论(1编辑  收藏  举报