无聊写了个巨简单无比的后台小程序,算练手-__________-,好久没用纯C了

这是一个使用C语言实现的学生信息管理系统,支持添加、删除、更新学生信息等功能,并能通过姓名、学号等条件查找学生记录。系统还提供了文件操作功能,能够加载和保存学生数据。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

据说不在vc++9.0上就会出N多错误...

  1. #include <stdio.h>
  2. #include <string.h>
  3. #define MAX_LEN 255
  4. #define MAX_STU_NUM 10000
  5. #define ADD_STU_INFO "add"
  6. #define DEL_STU_INFO "del"
  7. #define UP_STU_INFO "update"
  8. #define FIN_STU_INFO "find"
  9. #define OPEN_FILE "open"
  10. #define SAVE_FILE "save"
  11. #define OTH_SAVE_FILE "asave"
  12. #define SHOW_ALL "show"
  13. #define HELP "help"
  14. #define QUIT "quit"
  15. #define _swap(a,b) tmp=a,a=b,b=tmp;/*交换*/
  16. #define _cmp(a,b) strcmp(a,b)<0/*按字典序升*/
  17. FILE *file;
  18. struct String
  19. {
  20.     char str[MAX_LEN];
  21. };
  22. struct Stu
  23. {
  24.     String _stu_name,_stu_num;
  25.     unsigned int _stu_c_score,_stu_eng_score;
  26. };
  27. void print(char *str,int _blank_num)
  28. {int i;for(i=1;i<=_blank_num;i++)putchar(' ');printf("%s/n",str);}
  29. void cin(){printf(">>");}
  30. void add(Stu *stu,int &_cur_num)
  31. {
  32.     if(_cur_num>MAX_STU_NUM){print(">>Stu_Number_Exceed!",0);return;}
  33.     scanf("%s %s %d %d",&stu[_cur_num]._stu_num,&stu[_cur_num]._stu_name,&stu[_cur_num]._stu_c_score,&stu[_cur_num]._stu_eng_score);
  34.     print(">>Add success!",0);
  35.     _cur_num++;
  36. }
  37. void del(Stu *stu,int &_cur_num)
  38. {
  39.     int id,i;
  40.     scanf("%d",&id);
  41.     if(id<0||id>_cur_num){print(">>Stu_Number_Range_Exceed!",0);return;}
  42.     --_cur_num;
  43.     for(i=id;i<_cur_num;i++)
  44.         stu[i]=stu[i+1];
  45.     print(">>Delete success!",0);
  46. }
  47. void sort(Stu *stu,int _num)/*按学号排序_字典序*/
  48. {
  49.     int i,j;
  50.     Stu tmp;
  51.     for(i=0;i<_num-1;i++)
  52.         for(j=i+1;j<_num;j++)
  53.             if(_cmp(stu[j]._stu_num.str,stu[i]._stu_num.str)) _swap(stu[i],stu[j]);
  54. }
  55. void save(Stu *stu,int _stu_num)/*保存信息*/
  56. {
  57.     int i;
  58.     if(file==NULL){print(">>Error!No file open!",0);return;}
  59.     fwrite(&_stu_num,sizeof(int),1,file);
  60.     for(i=0;i<_stu_num;i++)
  61.     fwrite(&stu[i],sizeof(Stu),1,file);
  62.     fclose(file);
  63.     print(">>Save success!",0);
  64. }
  65. void asave(Stu *stu,int _stu_num,String url)/*保存信息*/
  66. {
  67.     int i;FILE *file=fopen(url.str,"wb+");
  68.     if(file==NULL){print("ERROR!",10);return;}
  69.     fwrite(&_stu_num,sizeof(int),1,file);
  70.     for(i=0;i<_stu_num;i++)
  71.     fwrite(&stu[i],sizeof(Stu),1,file);
  72.     fclose(file);
  73.     print(">>Asave success!",0);
  74. }
  75. void change(char *str)/*转化为小写,忽略命令大小写^.^*/
  76. {
  77.     int i,len=strlen(str);
  78.     for(i=0;i<len;i++)
  79.         if(str[i]>='A'&&str[i]<='Z') str[i]+=32;
  80. }
  81. void open(Stu *stu,String url,int& _stu_num)/*打开文件读入信息并判断文件是否存在*/
  82. {
  83.     int i;
  84.     file=fopen(url.str,"rb+");
  85.     if(file==NULL) {print(">>Open error!Please Check whether the file exist or not!",0);return;}
  86.     fread(&_stu_num,sizeof(int),1,file);
  87.     for(i=0;i<_stu_num;i++)
  88.         fread(&stu[i],sizeof(Stu),1,file);
  89.     print(">>Open success!",0);
  90. }
  91. void list_by_name(Stu *stu,int _stu_num)
  92. {
  93.     int i,count=0;
  94.     char name[255];
  95.     scanf("%s",name);
  96.     printf("     Stu_id:     Name:     C:     English:/n");
  97.     for(i=0;i<_stu_num;i++)
  98.         if(strcmp(stu[i]._stu_name.str,name)==0)
  99.             count++,printf("%12s%10s%7d%13d/n",stu[i]._stu_num.str,stu[i]._stu_name.str,stu[i]._stu_c_score,stu[i]._stu_eng_score);
  100.     printf(">>total %d student(s) found!/n",count);
  101. }
  102. void list_by_id(Stu *stu,int _stu_num)
  103. {
  104.     int i,count=0;
  105.     char name[255];
  106.     scanf("%s",name);
  107.     printf("     Stu_id:     Name:     C:     English:/n");
  108.     for(i=0;i<_stu_num;i++)
  109.         if(strcmp(stu[i]._stu_num.str,name)==0)
  110.             count++,printf("%12s%10s%7d%13d/n",stu[i]._stu_num.str,stu[i]._stu_name.str,stu[i]._stu_c_score,stu[i]._stu_eng_score);
  111.     printf(">>total %d student(s) found!/n",count);
  112. }
  113. void list_by_cs(Stu *stu,int _stu_num)
  114. {
  115.     int i,count=0,base;
  116.     char name[255];
  117.     scanf("%s%d",name,base);
  118.     printf("     Stu_id:     Name:     C:     English:/n");
  119.     for(i=0;i<_stu_num;i++)
  120.     {
  121.         if(strcmp(name,"==")==0&base==stu[i]._stu_c_score)
  122.             count++,printf("%12s%10s%7d%13d/n",stu[i]._stu_num.str,stu[i]._stu_name.str,stu[i]._stu_c_score,stu[i]._stu_eng_score);
  123.         if(strcmp(name,"<=")==0&base>=stu[i]._stu_c_score)
  124.             count++,printf("%12s%10s%7d%13d/n",stu[i]._stu_num.str,stu[i]._stu_name.str,stu[i]._stu_c_score,stu[i]._stu_eng_score);
  125.         if(strcmp(name,">=")==0&base<=stu[i]._stu_c_score)
  126.             count++,printf("%12s%10s%7d%13d/n",stu[i]._stu_num.str,stu[i]._stu_name.str,stu[i]._stu_c_score,stu[i]._stu_eng_score);
  127.         if(strcmp(name,">")==0&base<stu[i]._stu_c_score)
  128.             count++,printf("%12s%10s%7d%13d/n",stu[i]._stu_num.str,stu[i]._stu_name.str,stu[i]._stu_c_score,stu[i]._stu_eng_score);
  129.         if(strcmp(name,"<")==0&base>stu[i]._stu_c_score)
  130.             count++,printf("%12s%10s%7d%13d/n",stu[i]._stu_num.str,stu[i]._stu_name.str,stu[i]._stu_c_score,stu[i]._stu_eng_score);
  131.     }
  132.     printf(">>total %d student(s) found!/n",count);
  133. }
  134. void list_by_es(Stu *stu,int _stu_num)
  135. {
  136.     int i,count=0,base;
  137.     char name[255];
  138.     scanf("%s%d",name,base);
  139.     printf("     Stu_id:     Name:     C:     English:/n");
  140.     for(i=0;i<_stu_num;i++)
  141.     {
  142.         if(strcmp(name,"==")==0&base==stu[i]._stu_eng_score)
  143.             count++,printf("%12s%10s%7d%13d/n",stu[i]._stu_num.str,stu[i]._stu_name.str,stu[i]._stu_c_score,stu[i]._stu_eng_score);
  144.         if(strcmp(name,"<=")==0&base>=stu[i]._stu_eng_score)
  145.             count++,printf("%12s%10s%7d%13d/n",stu[i]._stu_num.str,stu[i]._stu_name.str,stu[i]._stu_c_score,stu[i]._stu_eng_score);
  146.         if(strcmp(name,">=")==0&base<=stu[i]._stu_eng_score)
  147.             count++,printf("%12s%10s%7d%13d/n",stu[i]._stu_num.str,stu[i]._stu_name.str,stu[i]._stu_c_score,stu[i]._stu_eng_score);
  148.         if(strcmp(name,">")==0&base<stu[i]._stu_eng_score)
  149.             count++,printf("%12s%10s%7d%13d/n",stu[i]._stu_num.str,stu[i]._stu_name.str,stu[i]._stu_c_score,stu[i]._stu_eng_score);
  150.         if(strcmp(name,"<")==0&base>stu[i]._stu_eng_score)
  151.             count++,printf("%12s%10s%7d%13d/n",stu[i]._stu_num.str,stu[i]._stu_name.str,stu[i]._stu_c_score,stu[i]._stu_eng_score);
  152.     }
  153.     printf(">>total %d student(s) found!/n",count);
  154. }
  155. int find(Stu *stu,int _stu_num,char* id)
  156. {
  157.     int i;
  158.     for(i=0;i<_stu_num;i++)if(strcmp(stu[i]._stu_num.str,id)==0)return i;
  159.     print(">>Can't find the infomation!Please Check your order carefully!",0);
  160.     return 0;
  161. }
  162. void showhelp()
  163. {
  164.     print(">>Here follows some tips:",0);
  165.     print("help:show some tips.",0);
  166.     print("open [dir_name]:load stu_info from the file.",0);
  167.     print("save:save all data,but you must ensure you have open a file.",0);
  168.     print("asave [dir_name]:save all data to [dir_name].",0);
  169.     print("update [stu_id] [option] [value]:[stu_id] means the unique id one student has,[option] means the name,c_score or english_score,while [value] means change the [option] to [value].",0);
  170.     print("find [option]:[option] [<,>,==,>=,<=] [value] means stu_id,name,c_score or english_score,find all that fit the [value]",0);
  171.     print("add [stu_id] [stu_name] [c_score] [english_score]:add a new record.",0);
  172.     print("del [stu_id]:delete the record at [stu_id].",0); 
  173.     cin();
  174. }
  175. void showall(Stu *stu,int _stu_num)
  176. {
  177.     int i;
  178.     printf("     Stu_id:     Name:     C:     English:/n");
  179.     for(i=0;i<_stu_num;i++)
  180.         printf("%12s%10s%7d%13d/n",stu[i]._stu_num.str,stu[i]._stu_name.str,stu[i]._stu_c_score,stu[i]._stu_eng_score);
  181.     printf(">>total %d student(s) found!/n",_stu_num);
  182. }
  183. Stu stu[MAX_STU_NUM+1];
  184. String order,url;
  185. char way[255],tmp[255];
  186. int _stu_num=0,id,tcl;
  187. void main()
  188. {
  189.     print("Welcome to the Stu_Info_System!",20);
  190.     showhelp();
  191.     file=NULL;
  192.     while(scanf("%s",&order.str)!=EOF,strcmp(order.str,QUIT))
  193.         {
  194.             change(order.str);
  195.             if(strcmp(order.str,ADD_STU_INFO)==0)
  196.                 {
  197.                     add(stu,_stu_num);
  198.                     sort(stu,_stu_num);
  199.                     cin();
  200.                     continue;
  201.                 }
  202.             if(strcmp(order.str,DEL_STU_INFO)==0)
  203.                 {
  204.                     del(stu,_stu_num);
  205.                     sort(stu,_stu_num);
  206.                     cin();
  207.                     continue;
  208.                 }
  209.             if(strcmp(order.str,OPEN_FILE)==0)
  210.                 {
  211.                     scanf("%s",url.str);
  212.                     open(stu,url,_stu_num);
  213.                     sort(stu,_stu_num);
  214.                     cin();
  215.                     continue;
  216.                 }
  217.             if(strcmp(order.str,UP_STU_INFO)==0)
  218.                 {
  219.                     scanf("%s %s",tmp,way);
  220.                     if(strcmp(way,"name")==0)
  221.                     {id=-1;
  222.                         id=find(stu,_stu_num,tmp);
  223.                         scanf("%s",&tmp);
  224.                         if(id!=-1)strcpy(stu[id]._stu_name.str,tmp);
  225.                     }
  226.                     if(strcmp(way,"cscore")==0)
  227.                     {id=-1;
  228.                         id=find(stu,_stu_num,tmp);
  229.                         scanf("%d",&tcl);
  230.                         if(id!=-1)stu[id]._stu_c_score=tcl;
  231.                     }
  232.                     if(strcmp(way,"escore")==0)
  233.                     {id=-1;
  234.                         id=find(stu,_stu_num,tmp);
  235.                         scanf("%d",&tcl);
  236.                         if(id!=-1)stu[id]._stu_eng_score=tcl;
  237.                     }
  238.                     cin();
  239.                     continue;
  240.                 }
  241.             if(strcmp(order.str,FIN_STU_INFO)==0)
  242.                 {
  243.                     scanf("%s",way);
  244.                     if(strcmp(way,"name")==0)
  245.                         {
  246.                             list_by_name(stu,_stu_num);
  247.                             cin();
  248.                             continue;
  249.                         }
  250.                     if(strcmp(way,"id")==0)
  251.                         {
  252.                             list_by_id(stu,_stu_num);
  253.                             cin();
  254.                             continue;
  255.                         }
  256.                     if(strcmp(way,"cscore")==0)
  257.                         {
  258.                             list_by_cs(stu,_stu_num);
  259.                             cin();
  260.                             continue;
  261.                         }
  262.                     if(strcmp(way,"escore")==0)
  263.                         {
  264.                             list_by_es(stu,_stu_num);
  265.                             cin();
  266.                             continue;
  267.                         }
  268.                 }
  269.             if(strcmp(order.str,SAVE_FILE)==0)
  270.                 {
  271.                         save(stu,_stu_num);
  272.                         cin();
  273.                         continue;
  274.                 }
  275.             if(strcmp(order.str,OTH_SAVE_FILE)==0)
  276.                 {
  277.                     scanf("%s",&url.str);
  278.                     asave(stu,_stu_num,url);
  279.                     cin();
  280.                     continue;
  281.                 }
  282.             if(strcmp(order.str,HELP)==0)
  283.                 {
  284.                     showhelp();
  285.                     continue;
  286.                 }
  287.             if(strcmp(order.str,SHOW_ALL)==0)
  288.                 {
  289.                     showall(stu,_stu_num);
  290.                     cin();
  291.                     continue;
  292.                 }
  293.             printf(">>type [help] to find more available orders!/n");
  294.             cin();
  295.         }
  296. }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值