int main(int argc,char *argv[]) { int a=0; char buf[1024]; int cmdno; int quit=0; char name[20]; char id[20]; int rt; int age; char sex; struct student *pstu; struct student stu; struct listhead phead; phead.next = phead.pre = &phead; // readfromfile(&phead,STUFILE); //struct allstudent allstu; //rt=init_allstu(&allstu); printf("****************************************************************\n"); printf("please input cmd:"); if(rt==-1) { return -1; } while(0==quit) { fgets(buf,1024,stdin); buf[strlen(buf)-1] = '\0'; cmdno=getCmdno(buf); switch(cmdno) { case HELP: help(); break; case ADD: printf("add\n"); printf("please input student info\n"); printf("please input student id\n"); scanf("%s",id); printf("please input student name\n"); scanf("%s",name); printf("please input student age\n"); scanf("%d",&age); printf("please input student sex\n"); fgets(buf,1024,stdin); scanf("%c",&sex); // strcpy(stu.id,id); // strcpy(stu.name,name); // stu.age=(char)age; // stu.sex = sex; // addbyorder(&allstu,&stu); pstu = malloc(sizeof(struct student)); if(NULL==pstu) { printf("no\n"); break; } strcpy(pstu->id,id); strcpy(pstu->name,name); pstu->age=(char)age; pstu->sex=sex; addstu(&phead,pstu); break; case DEL: printf("please input student id\n"); scanf("%s",id); // pstu=findbyid(&phead,id); if(NULL==pstu) { printf("%s can not exit\n",id); break; } delstu(pstu); free(pstu); pstu=NULL; break; /* case FIND: printf("please input student id\n"); scanf("%s",id); pstu=findbyid(&phead,id); if(NULL==pstu) { printf("no this student\n"); break; } showone(pstu); break; case FNAME: printf("please input name:"); scanf("%s",name); pstu=NULL; do { pstu=findbyname(&phead,name,pstu); if(NULL!=pstu) { showone(pstu); } }while(NULL!=pstu); break;*/ case SHOW: showall(&phead); printf("please input cmd:"); break; /* case RSHOW: rshowall(&phead); printf("please input cmd:"); break; case REVER: reverse(&phead); printf("please input cmd:"); break;*/ case QUIT: quit=1; break; /* case SAVE: savefile(&phead,STUFILE); printf("please input cmd:"); break; case MODIFY: break; */ default: printf("please input cmd:"); break; } } destroyall(&phead); return 0; }
int main() { struct idhash allstu; struct student *p; struct student tempstu; struct student *stu; int tempage; char cmd[1024]; int cmdnum; int quit = 0; int select; FILE *studata; studata = fopen("studata","r+"); if(NULL == studata) { studata = fopen("studata","w+"); if(NULL == studata) { printf("cannot open file !\n"); return -1; } } initallidhash(&allstu); readfromfile(&allstu,studata); printf("|-------------------------------------------|\n"); printf("|----Welcome to Student Database System-----|\n"); printf("|-------------------------------------------|\n\n"); printf("%d student have!\n",count(&allstu)); while(0 == quit) { printf("|-------------------------------------------|\n"); printf("|-------Please Input \"help\" for Help--------|\n"); printf("|-------------------------------------------|\n"); fgets(cmd,1024,stdin); cmd[strlen(cmd)-1] = 0; cmdnum = getcmd(cmd); switch(cmdnum) { case CMDSHOW: showallstu(&allstu); break; case CMDQUIT: printf("quit successful!\n"); quit = 1; break; case CMDADD: printf("please input a new student:\nstudent's ID:"); scanf("%s",tempstu.id); printf("please input student's Name:"); scanf("%s",tempstu.name); printf("please input student's age:"); scanf("%d",&tempage); tempstu.age = (char)tempage; fgets(cmd,1024,stdin); printf("please input student's sex:"); scanf("%c",&tempstu.sex); fgets(cmd,1024,stdin); tempstu.next = NULL; p = findstu(&allstu,tempstu.id); if(NULL != p) { printf("This id is already exist!\n"); break; } stu = newstu(&tempstu); addstu(&allstu,stu); break; case CMDDEL: printf("please input the id you wante to delete:"); scanf("%s",tempstu.id); fgets(cmd,1024,stdin); p = findstu(&allstu,tempstu.id); if(NULL == p) { printf("not found the id!\n"); break; } delstu(&allstu,p); break; case CMDFIND: printf("please input 1 for id,2 for name:"); scanf("%d",&select); fgets(cmd,1024,stdin); if(1 == select) { scanf("%s",tempstu.id); fgets(cmd,1024,stdin); p = findstu(&allstu,tempstu.id); if(NULL == p) { printf("not found the id!\n"); break; } showstu(p); } /* else if(2 == select) { printf("please input the name you want to find:"); scanf("%s",tempstu.name); fgets(cmd,1024,stdin); p = phead; do { p = findnamenext(p,tempstu.name); if(NULL != p) { showstu(p); p = p->next; } }while(NULL != p); break; } */ case CMDSAVE: save(&allstu,studata); break; case CMDREVERSE: //phead = reverse(phead); break; case CMDHELP: showhelp(); break; default: printf("not this cmd\n"); break; } } fclose(studata); }