据说不在vc++9.0上就会出N多错误...
- #include <stdio.h>
- #include <string.h>
- #define MAX_LEN 255
- #define MAX_STU_NUM 10000
- #define ADD_STU_INFO "add"
- #define DEL_STU_INFO "del"
- #define UP_STU_INFO "update"
- #define FIN_STU_INFO "find"
- #define OPEN_FILE "open"
- #define SAVE_FILE "save"
- #define OTH_SAVE_FILE "asave"
- #define SHOW_ALL "show"
- #define HELP "help"
- #define QUIT "quit"
- #define _swap(a,b) tmp=a,a=b,b=tmp;/*交换*/
- #define _cmp(a,b) strcmp(a,b)<0/*按字典序升*/
- FILE *file;
- struct String
- {
- char str[MAX_LEN];
- };
- struct Stu
- {
- String _stu_name,_stu_num;
- unsigned int _stu_c_score,_stu_eng_score;
- };
- void print(char *str,int _blank_num)
- {int i;for(i=1;i<=_blank_num;i++)putchar(' ');printf("%s/n",str);}
- void cin(){printf(">>");}
- void add(Stu *stu,int &_cur_num)
- {
- if(_cur_num>MAX_STU_NUM){print(">>Stu_Number_Exceed!",0);return;}
- 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);
- print(">>Add success!",0);
- _cur_num++;
- }
- void del(Stu *stu,int &_cur_num)
- {
- int id,i;
- scanf("%d",&id);
- if(id<0||id>_cur_num){print(">>Stu_Number_Range_Exceed!",0);return;}
- --_cur_num;
- for(i=id;i<_cur_num;i++)
- stu[i]=stu[i+1];
- print(">>Delete success!",0);
- }
- void sort(Stu *stu,int _num)/*按学号排序_字典序*/
- {
- int i,j;
- Stu tmp;
- for(i=0;i<_num-1;i++)
- for(j=i+1;j<_num;j++)
- if(_cmp(stu[j]._stu_num.str,stu[i]._stu_num.str)) _swap(stu[i],stu[j]);
- }
- void save(Stu *stu,int _stu_num)/*保存信息*/
- {
- int i;
- if(file==NULL){print(">>Error!No file open!",0);return;}
- fwrite(&_stu_num,sizeof(int),1,file);
- for(i=0;i<_stu_num;i++)
- fwrite(&stu[i],sizeof(Stu),1,file);
- fclose(file);
- print(">>Save success!",0);
- }
- void asave(Stu *stu,int _stu_num,String url)/*保存信息*/
- {
- int i;FILE *file=fopen(url.str,"wb+");
- if(file==NULL){print("ERROR!",10);return;}
- fwrite(&_stu_num,sizeof(int),1,file);
- for(i=0;i<_stu_num;i++)
- fwrite(&stu[i],sizeof(Stu),1,file);
- fclose(file);
- print(">>Asave success!",0);
- }
- void change(char *str)/*转化为小写,忽略命令大小写^.^*/
- {
- int i,len=strlen(str);
- for(i=0;i<len;i++)
- if(str[i]>='A'&&str[i]<='Z') str[i]+=32;
- }
- void open(Stu *stu,String url,int& _stu_num)/*打开文件读入信息并判断文件是否存在*/
- {
- int i;
- file=fopen(url.str,"rb+");
- if(file==NULL) {print(">>Open error!Please Check whether the file exist or not!",0);return;}
- fread(&_stu_num,sizeof(int),1,file);
- for(i=0;i<_stu_num;i++)
- fread(&stu[i],sizeof(Stu),1,file);
- print(">>Open success!",0);
- }
- void list_by_name(Stu *stu,int _stu_num)
- {
- int i,count=0;
- char name[255];
- scanf("%s",name);
- printf(" Stu_id: Name: C: English:/n");
- for(i=0;i<_stu_num;i++)
- if(strcmp(stu[i]._stu_name.str,name)==0)
- 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);
- printf(">>total %d student(s) found!/n",count);
- }
- void list_by_id(Stu *stu,int _stu_num)
- {
- int i,count=0;
- char name[255];
- scanf("%s",name);
- printf(" Stu_id: Name: C: English:/n");
- for(i=0;i<_stu_num;i++)
- if(strcmp(stu[i]._stu_num.str,name)==0)
- 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);
- printf(">>total %d student(s) found!/n",count);
- }
- void list_by_cs(Stu *stu,int _stu_num)
- {
- int i,count=0,base;
- char name[255];
- scanf("%s%d",name,base);
- printf(" Stu_id: Name: C: English:/n");
- for(i=0;i<_stu_num;i++)
- {
- if(strcmp(name,"==")==0&base==stu[i]._stu_c_score)
- 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);
- if(strcmp(name,"<=")==0&base>=stu[i]._stu_c_score)
- 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);
- if(strcmp(name,">=")==0&base<=stu[i]._stu_c_score)
- 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);
- if(strcmp(name,">")==0&base<stu[i]._stu_c_score)
- 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);
- if(strcmp(name,"<")==0&base>stu[i]._stu_c_score)
- 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);
- }
- printf(">>total %d student(s) found!/n",count);
- }
- void list_by_es(Stu *stu,int _stu_num)
- {
- int i,count=0,base;
- char name[255];
- scanf("%s%d",name,base);
- printf(" Stu_id: Name: C: English:/n");
- for(i=0;i<_stu_num;i++)
- {
- if(strcmp(name,"==")==0&base==stu[i]._stu_eng_score)
- 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);
- if(strcmp(name,"<=")==0&base>=stu[i]._stu_eng_score)
- 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);
- if(strcmp(name,">=")==0&base<=stu[i]._stu_eng_score)
- 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);
- if(strcmp(name,">")==0&base<stu[i]._stu_eng_score)
- 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);
- if(strcmp(name,"<")==0&base>stu[i]._stu_eng_score)
- 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);
- }
- printf(">>total %d student(s) found!/n",count);
- }
- int find(Stu *stu,int _stu_num,char* id)
- {
- int i;
- for(i=0;i<_stu_num;i++)if(strcmp(stu[i]._stu_num.str,id)==0)return i;
- print(">>Can't find the infomation!Please Check your order carefully!",0);
- return 0;
- }
- void showhelp()
- {
- print(">>Here follows some tips:",0);
- print("help:show some tips.",0);
- print("open [dir_name]:load stu_info from the file.",0);
- print("save:save all data,but you must ensure you have open a file.",0);
- print("asave [dir_name]:save all data to [dir_name].",0);
- 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);
- print("find [option]:[option] [<,>,==,>=,<=] [value] means stu_id,name,c_score or english_score,find all that fit the [value]",0);
- print("add [stu_id] [stu_name] [c_score] [english_score]:add a new record.",0);
- print("del [stu_id]:delete the record at [stu_id].",0);
- cin();
- }
- void showall(Stu *stu,int _stu_num)
- {
- int i;
- printf(" Stu_id: Name: C: English:/n");
- for(i=0;i<_stu_num;i++)
- 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);
- printf(">>total %d student(s) found!/n",_stu_num);
- }
- Stu stu[MAX_STU_NUM+1];
- String order,url;
- char way[255],tmp[255];
- int _stu_num=0,id,tcl;
- void main()
- {
- print("Welcome to the Stu_Info_System!",20);
- showhelp();
- file=NULL;
- while(scanf("%s",&order.str)!=EOF,strcmp(order.str,QUIT))
- {
- change(order.str);
- if(strcmp(order.str,ADD_STU_INFO)==0)
- {
- add(stu,_stu_num);
- sort(stu,_stu_num);
- cin();
- continue;
- }
- if(strcmp(order.str,DEL_STU_INFO)==0)
- {
- del(stu,_stu_num);
- sort(stu,_stu_num);
- cin();
- continue;
- }
- if(strcmp(order.str,OPEN_FILE)==0)
- {
- scanf("%s",url.str);
- open(stu,url,_stu_num);
- sort(stu,_stu_num);
- cin();
- continue;
- }
- if(strcmp(order.str,UP_STU_INFO)==0)
- {
- scanf("%s %s",tmp,way);
- if(strcmp(way,"name")==0)
- {id=-1;
- id=find(stu,_stu_num,tmp);
- scanf("%s",&tmp);
- if(id!=-1)strcpy(stu[id]._stu_name.str,tmp);
- }
- if(strcmp(way,"cscore")==0)
- {id=-1;
- id=find(stu,_stu_num,tmp);
- scanf("%d",&tcl);
- if(id!=-1)stu[id]._stu_c_score=tcl;
- }
- if(strcmp(way,"escore")==0)
- {id=-1;
- id=find(stu,_stu_num,tmp);
- scanf("%d",&tcl);
- if(id!=-1)stu[id]._stu_eng_score=tcl;
- }
- cin();
- continue;
- }
- if(strcmp(order.str,FIN_STU_INFO)==0)
- {
- scanf("%s",way);
- if(strcmp(way,"name")==0)
- {
- list_by_name(stu,_stu_num);
- cin();
- continue;
- }
- if(strcmp(way,"id")==0)
- {
- list_by_id(stu,_stu_num);
- cin();
- continue;
- }
- if(strcmp(way,"cscore")==0)
- {
- list_by_cs(stu,_stu_num);
- cin();
- continue;
- }
- if(strcmp(way,"escore")==0)
- {
- list_by_es(stu,_stu_num);
- cin();
- continue;
- }
- }
- if(strcmp(order.str,SAVE_FILE)==0)
- {
- save(stu,_stu_num);
- cin();
- continue;
- }
- if(strcmp(order.str,OTH_SAVE_FILE)==0)
- {
- scanf("%s",&url.str);
- asave(stu,_stu_num,url);
- cin();
- continue;
- }
- if(strcmp(order.str,HELP)==0)
- {
- showhelp();
- continue;
- }
- if(strcmp(order.str,SHOW_ALL)==0)
- {
- showall(stu,_stu_num);
- cin();
- continue;
- }
- printf(">>type [help] to find more available orders!/n");
- cin();
- }
- }