标题:[求助]再下写了一个通讯录,可运行。但没结果,帮忙找找问题出在那。
只看楼主
city505
Rank: 1
等 级:新手上路
帖 子:26
专家分:0
注 册:2005-11-5
 问题点数:0 回复次数:2 
[求助]再下写了一个通讯录,可运行。但没结果,帮忙找找问题出在那。

#include "stdio.h"
#include "stdlib.h"
#include "string.h"
void insert(void);
void del(); /*定义函数*/
void ser();
void output();
struct student
{
int num;
char name;
long tel;
long QQ;
struct student *next;
};
struct student *head,*p,*q,*newp;
main()
{

char a;
head=NULL;
while(a)
{
printf("please chiose one:\n");
printf("1 insert\n");
printf("2 del\n");
printf("3 serch\n"); /*主菜单*/
printf("4 output\n");
printf("5 exit\n");
scanf("%d",&a);
switch(a)
{
case 1: insert();break;
case 2: del();break; /*函数调用*/
case 3: ser();break;
case 4: output();break;
default :exit(0);
}
}
}
void insert() /*尾部插入方法*/
{
newp=(struct student *)malloc(sizeof(struct student));
if(head==NULL)
head=newp;
else
{
p=head;
while(p)
p=p->next;
p=newp;
}
printf("please enter num:\n");
scanf("%d",&p->num);
printf("please enter name:\n");
scanf("%s",&p->name);
printf("please enter tel:");
scanf("%ld",&p->tel);
printf("please enter QQ:");
scanf("%ld",&p->QQ);
p->next=NULL;
free(newp);
return ;
}
void del(void) /*删除函数*/
{
long i;
printf("please enter num:\n");
scanf("%ld",&i);
p=head;
if(p->next==NULL)
p->next=NULL;

else
{
while(p->next->num==i)
{
q=(struct student *)malloc(sizeof(struct student));
q=p->next;
p->next=q->next;

}
}
free(q);
return ;

}
void ser(void) /*查找函数*/
{
int i;
printf("please enter num:\n");
scanf("%ld",&i);
if(head==NULL)
printf("the student is no.\n");
for(p=head;p;p=p->next)
{
if(p->num==i)
printf("%ld %s %ld %ld\n",p->num,p->name,p->tel,p->QQ);
else
printf("the student is no.\n");
}
return ;

}
void output() /*输出函数*/
{
p=head;
for(;p;p=p->next)
printf("%ld %s %ld %ld\n",p->num,p->name,p->tel,p->QQ);
return ;
}

搜索更多相关主题的帖子: 通讯录 void printf student 
2005-12-23 11:28
沉默的羔羊1013
Rank: 1
等 级:新手上路
帖 子:72
专家分:0
注 册:2005-12-10
得分:0 
你这程序问题挺多的,你再自己看看吧,建议你少设点全局变量吧,否则太混乱了
2005-12-23 20:03
maoguoqing
Rank: 6Rank: 6
来 自:重庆
等 级:贵宾
威 望:28
帖 子:2980
专家分:19
注 册:2005-12-5
得分:0 

和我这个程序挺象的,你自己在找找吧!
可以参考一下:
/******************************************/
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define LEN sizeof(struct Directory)

struct Directory /*定义通讯录结构体*/
{
char name[15];/*姓名,最多15个字符*/
int sex;/*性别,1为男,2为女*/
char adress[50];/*地址,最多50个字符*/
char phone[11];/*电话,最多11位数*/
struct Directory *next;/*指向下一个记录*/
};

struct Directory *head;

void creat()/*输入数据,建立链表*/
{
char ch;
struct Directory *p,*q;

printf("开始生成通讯录,请输入数据:\n");
p=head=(struct Directory *)malloc(LEN);
printf("姓名:");
scanf("%s",p->name);
printf("性别(1为男,0为女):");
scanf("%d",&p->sex);
printf("地址:");
scanf("%s",p->adress);
printf("电话:");
scanf("%s",p->phone);
printf("继续请输入‘Y’,完毕请输入‘N’:");
scanf("%c",&ch); scanf("%c",&ch);

while((ch=='y')||(ch=='Y'))
{
q=p;
p=(struct Directory *)malloc(LEN);
printf("姓名:");
scanf("%s",p->name);
printf("性别(1为男,0为女):");
scanf("%d",&p->sex);
printf("地址:");
scanf("%s",p->adress);
printf("电话:");
scanf("%s",p->phone);
printf("继续请输入‘Y’,完毕请输入‘N’:");
scanf("%c",&ch);scanf("%c",&ch);
q->next=p;
}
p->next=NULL;
printf("通讯录建立完成!\n");
}

/*采用向链表末尾插入数据的方法*/
void insert()
{
struct Directory *q,*p;

q=head;

while(q->next!=NULL) q=q->next;

p=(struct Directory *)malloc(LEN);
printf("姓名:");
scanf("%s",p->name);
printf("性别(1为男,0为女):");
scanf("%d",&p->sex);
printf("地址:");
scanf("%s",p->adress);
printf("电话:");
scanf("%s",p->phone);
q->next=p;
p->next=NULL;
printf("操作成功!\n");
}

int compare(char a[50],char b[50])
{
int i,yes=1,n1,n2;
n1=strlen(a);n2=strlen(b);
if (n1!=n2) return(0);
else for (i=0;i<n1;i++) if (a[i]!=b[i]) yes=0;
return(yes);
}

void print(struct Directory *p)/*打印结点信息*/
{
printf("姓名:");printf("%s\n",p->name);
printf("性别:");printf("%d\n",p->sex);
printf("地址:");printf("%s\n",p->adress);
printf("电话:");printf("%s\n",p->phone);
}

void del()
{
int fun;
char ch,inf[50];
struct Directory *p,*q;

loop: printf("请输入你想删除的记录的信息\n");
printf("1输入姓名\n");
printf("2输入地址\n");
printf("3输入电话\n");
printf("请输入你知道的选项(1,2,3):");
scanf("%d",&fun);

switch (fun)
{
case 1:printf("输入姓名:");break;
case 2:printf("输入地址:");break;
case 3:printf("输入电话:");break;
default:printf("输入错误!");goto loop;
}
scanf("%s",inf);
p=head;

while (p!=NULL)
{
if((fun==1)&&(compare(p->name,inf))){print(p);break;}
if((fun==2)&&(compare(p->adress,inf))){print(p);break;}
if((fun==3)&&(compare(p->phone,inf))){print(p);break;}
q=p;
p=p->next;
}
if (p==NULL) {printf("没有该记录!请从新输入!\n");goto loop;}
printf("确定删除请输入'y',放弃请输入‘n’:");
scanf("%c",&ch);scanf("%c",&ch);
if((ch=='n')||(ch=='N')) return;
else
{ if (p==head) {head=p->next;free(p);}
else
{ q->next=p->next;free(p);}
} printf("操作成功\n");
}

void find()
{
int fun;
char inf[50];
struct Directory *p,*q;

loop: printf("请输入你想删除的记录的信息\n");
printf("1输入姓名\n");
printf("2输入地址\n");
printf("3输入电话\n");
printf("请输入你知道的选项(1,2,3):");
scanf("%d",&fun);

switch (fun)
{
case 1:printf("输入姓名:");break;
case 2:printf("输入地址:");break;
case 3:printf("输入电话:");break;
default:printf("输入错误!");goto loop;
}
scanf("%s",inf);
p=head;

while (p!=NULL)
{
if((fun==1)&&(compare(p->name,inf))){print(p);break;}
if((fun==2)&&(compare(p->adress,inf))){print(p);break;}
if((fun==3)&&(compare(p->phone,inf))){print(p);break;}
q=p;
p=p->next;
}
if (p==NULL) {printf("没有该记录!请从新输入!\n");goto loop;}
}

void main()
{
int fun;

creat();

do /*主功能界面*/
{
printf("1为向通讯录中插入一个记录\n");
printf("2为删除通讯录中的一个记录\n");
printf("3为查找通讯录中的一个记录\n");
printf("0为退出系统。\n");
printf("输入你需要的操作:");
scanf("%d",&fun);

switch (fun)
{
case 1:insert();break;
case 2:del();break;
case 3:find();break;
case 0:break;
default:printf("输入错误!");
}
}while(fun!=0);
}


天行健,君子以自强不息!!QQ:68660681
2005-12-23 21:23



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-39614-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.188668 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved