- 论坛徽章:
- 0
|
模仿网络上的一个程序写了一个链表实现人员基本信息管理的程序,很长很长,bug也超多,求各位高手找出bug,互相学习,已知bug:保存时无法写入文本,list()为空,find()找不到联系人,保存时会凭空多出一条记录,代码很长,写的很仓促,希望大家耐心看,挺简单的倒是,还是求助各位找出问题所在.
代码:- #include<stdio.h>
- #include<stdlib.h>
- struct card
- {
- char name[30];
- char tel[30];
- char zip[255];
- struct card *pre;
- struct card *next;
- };
- struct card *head;
- struct card *end;
- struct card *now = NULL;
- void uppoint();
- void downpoint();
- void save();
- void new();
- void ver();
- void del();
- void list();
- void point();
- void quit();
- void find();
- void cls();
- void load();
- void command();
- void show(struct card *);
- void error(int);
- void trade(struct card *,struct card *); //交换链表
- struct card *search(char *,int);
- void main()
- {
- ver();
- load();
- command();
- }
- void command()
- {
- char command[100];
- printf("Card Master!\n");
- for(;;)
- {
- printf("CMD:");
- gets(command);
- if(strcmp(command,"new")==0) new();
- else if(strcmp(command,"del")==0) del();
- else if(strcmp(command,"find")==0) find();
- else if(strcmp(command,"list")==0) list();
- else if(strcmp(command,"point")==0) point();
- else if(strcmp(command,"quit")==0) quit();
- else if(strcmp(command,"cls")==0) cls();
- else if(strcmp(command,"ver")==0) ver();
- else if(strcmp(command,"save")==0) save();
- else if(strcmp(command,"uppoint")==0) uppoint();
- else if(strcmp(command,"downpoint")==0) downpoint();
- else error(0);
- }
- }
- void show(struct card *person)
- {
- printf("\n_______________________________\n");
- printf("Name:%s\n",person->name);
- printf("Tel:%s\n",person->tel);
- printf("Zip:%s",person->zip);
- printf("\n_______________________________\n");
- }
- void list()
- {
- struct card *list;
- int count = 0;
- if(head->next = end)
- {
- printf("This is an empty Card\n");
- return;
- }
- list = head->next;
- for(;list->next;list = list->next)
- {
- show(list);
- count++;
- }
- printf("Total %d\n\n",count);
- }
- void point()
- {
- show(now);
- }
- void new()
- {
- struct card *new;
- new = malloc(sizeof(struct card));
-
- new->next = now->next;
- new->pre = now;
- now->next = new;
-
- now = new;
-
- printf("Enter Name:");
- gets(new->name);
- printf("Enter Tel:");
- gets(new->tel);
- printf("Enter Zip:");
- gets(new->zip);
-
- printf("Now has been creatten a new crad\n\n");
- show(new);
- printf("\n");
- }
- void find()
- {
- struct card *find;
- char words[255];
-
- printf("FIND...\n");
- printf("Enter your search about?(name/tel)\n");
- gets(words);
-
- if((strcmp(words,"name")==0))
- {
- printf("Enter Name:");
- gets(words);
- find = search(words,0);
-
- if(find = NULL)
- {
- error(1);
- return;
- }
- else
- {
- show(find);
- now = find;
- return;
- }
- }
- if((strcmp(words,"tel")==0))
- {
- printf("Enter Tel:");
- gets(words);
- find = search(words,1);
- if(!find)
- {
- error(1);
- return;
- }else
- {
- show(find);
- now = find;
- return;
- }
- }
- printf("Error\n\n");
- }
- struct card *search(char *words,int type)
- {
- struct card *search;
- search = head->next;
-
- if(type == 0)
- {
- for(;search->next;search = search->next)
- {
- if(strcmp(search->next,words)==0)
- return search;
- }
- return NULL;
- }
- if(type = 1)
- {
- for(;search->next;search = search->next);
- {
- if(strcmp(search->tel,words)==0)
- return search;
- }
- return NULL;
- }
- }
- void quit()
- {
- char words[10];
- printf("Quit,are you sure?(Y/N)");
- gets(words);
- if(strcmp(words,"Y")!=0)
- {
- printf("Ok,continue\n");
- return;
- }
- exit(0);
- }
- void error(int type)
- {
- if(type == 0)
- {
- printf("Bad command!\n");
- return;
- }
- if(type == 1)
- {
- printf("find Nothing\n");
- return;
- }
- }
- void del()
- {
- struct card *temp;
- char words[255];
-
- if(head->next = NULL)
- {
- error(1);
- return;
- }
- show(now);
- printf("It will delete this card,are you sure?(yes/no)\n");
- gets(words);
- if(strcmp(words,"yes")!=0)
- {
- printf("do nothing\n");
- return;
- }
- now->next->pre = now->pre;
- now->pre->next = now->next;
- free(now);
- printf("Now has been delete this record\n");
- }
- void cls()
- {
- int i;
- for(i = 0;i<60;i++)
- printf("\n");
- }
- void ver()
- {
- printf("\n___________________________________\n");
- printf("\n Build by leekic");
- printf("\n___________________________________\n");
- printf("\n\n");
- }
- void load()
- {
- FILE *fp;
- struct card *load;
- struct card *temp;
- if((fp = fopen("card.log","a+"))==NULL)
- {
- printf("cannot create files\n\n");
- return;
- }
- printf("Loading\n");
-
- head = malloc(sizeof(struct card));
- end = malloc(sizeof(struct card));
- head->pre = NULL;
- head->next = end;
- end->pre = head;
- end->next = NULL;
-
- strcpy(head->name,"HEAD");
- strcpy(head->tel,"HEAD");
- strcpy(head->zip,"HEAD");
-
- strcpy(end->name,"END");
- strcpy(end->tel,"END");
- strcpy(end->tel,"END");
-
- now = head;
-
- while(!feof(fp))
- {
- load = malloc(sizeof(struct card));
- fread(load,sizeof(struct card),1,fp);
- load->next = now->next;
- load->pre = now;
- now->next = load;
- now = load;
- }
- fclose(fp);
-
- printf("load files finished\n");
- return;
- }
- void save()
- {
- FILE *fp;
- struct card *save;
- char words[10];
- printf("It will change file,are your sure?(yes/no)");
- gets(words);
- if((strcmp(words,"yes")!=0))
- {
- printf("nothing to do\n");
- return;
- }
-
- if((fp=fopen("card.log","a+"))==NULL)
- {
- printf("Can't save file\n");
- return;
- }
- save = head->next;
- for(;save->next;save = save->next)
- {
- if((fwrite(save,sizeof(struct card),1,fp)));
- {
- printf("writing...\n");
- }
- }
- printf("Save finished\n");
- }
- void uppoint()
- {
- if(now->pre = head)
- {
- printf("Sorry,can not move Top\n");
- return;
- }
- printf("Trade cards:");
- show(now);
- show(now->pre);
- trade(now->pre,now);
- printf("Trade finished\n");
- }
- void downpoint()
- {
- if(now->next = end)
- {
- printf("Sorry can not move END!\n");
- return;
- }
- printf("Trade cards:");
- show(now);
- show(now->next);
- trade(now,now->next);
- printf("Trade finished!\n");
- }
- void trade(struct card *a,struct card *b)
- {
- a->pre->next = b;
- b->next->pre = a;
-
- a->next = b->next;
- b->pre = a->next;
-
- a->next = b;
- b->next = a;
- }
复制代码 |
|