用链表做通讯录的问题
- C/C++ code
#include<stdio.h>#include<stdlib.h>#include<conio.h>#include<string.h>#include <malloc.h>#define NULL 0int length=0;struct student{ int num; char name[10]; char birth[8]; char mov[15]; char fax[15]; char qnum[11]; char addr[50]; struct student *next;};void cover(){ printf("\n"); printf("\n"); printf("\n"); printf("\n"); printf("\t\t****************************************\n"); printf("\t\t********欢迎使用电子通讯录**************\n"); printf("\t\t****************************************\n"); printf("\n"); printf("\t\t\t1:添加信息\n"); printf("\t\t\t2:删除信息\n"); printf("\t\t\t3:修改信息\n"); printf("\t\t\t4:查询信息\n"); printf("\t\t\t5:显示所有信息\n"); printf("\t\t\t0:退出\n"); printf("\t\t\t请输入你的选择:\n");}void add(){ struct student *head,*q,*p; while(p=(struct student *)malloc(sizeof(struct student)), printf("请输入学号:(学号为0时结束添加)\n"),scanf("%d",&p->num),p->num!=0) { if(length==0) { head=p; } else { q->next=p; } q=p; printf("请输入姓名:\n"); scanf("%s",p->name); printf("请输入生日:\n"); scanf("%s",p->birth); printf("请输入电话:\n"); scanf("%s",p->mov); printf("请输入传真:\n"); scanf("%s",p->fax); printf("请输入企鹅号:\n"); scanf("%s",p->qnum); printf("请输入地址:\n"); scanf("%s",p->addr); length++; } q->next=NULL;}void search(){ struct student *head,*q,*p; int num; int choice=1; printf("请输入要查询的学号:\n"); scanf("%d",&num); while(p=(struct student *)malloc(sizeof(struct student)),choice!=0) { if(length==0) { head=p; } else { q->next=p; } q=p; if(num==p->num) { printf("%d\n",p->num); printf("%s\n",p->name); printf("%s\n",p->birth); printf("%s\n",p->mov); printf("%s\n",p->fax); printf("%s\n",p->qnum); printf("%s\n",p->addr); choice=0; } length++; }} void show(){ struct student *head,*p; for(p=head;p!=NULL;p=p->next) { printf("学号:"); printf("%d\n",p->num); printf("姓名:"); printf("%s\n",p->name); printf("生日:"); printf("%s\n",p->birth); printf("电话:"); printf("%s\n",p->mov); printf("传真:"); printf("%s\n",p->fax); printf("企鹅号:"); printf("%s\n",p->qnum); printf("地址:"); printf("%s\n",p->addr); }}main(){ system("color 6b"); int choice; //re(); while(1) { system("cls"); cover(); scanf("%d",&choice); switch(choice) { case 1:add();//save(); break; //case 2:del();//save(); //break; //case 3:update();//save(); //break; case 4:search(); break; case 5:show(); break; //case 0:quit();save(); //break; default: { system("cls"); printf("选择错误,重新输入\n"); system("PAUSE"); } } } }
刚开始学链表,很多地方不懂,目前我做了添加,查找和显示全部。添加好像没问题
问题1:查找只要一输入学号就错误。
问题2:显示全部的时候是乱码。
这2个问题不知道怎么解决啊,我做了2个多小时都没搞定T.T
[解决办法]
太乱了.lz看看这个链接吧,虽然是管理系统的,但是和通讯录差不多,改个名字而已.
http://tieba.baidu.com/f?kz=1109535412
[解决办法]
学号后面跟上getchar();是不是把回车空格什么的也接受了?