
求一个C语言编写的通讯录程序?急急急
展开全部
#include<stdio.h>
#define LL sizeof(struct Information)
#include<stdlib.h>
struct Information
{
char name[25];
int number;
double Tel_number;
struct Information *next;
};
int n,t;
struct Information *creat()
{
struct Information *head,*p1,*p2;
n=0;
p1=p2=(struct Information *)malloc(LL);
printf("请输入第 %d 个学生通讯信息:\n",n+1);
printf("姓名\t学号\t电话号码\n");
scanf("%s%d%lf",p1->name,&p1->number,&p1->Tel_number);
getchar();
head=NULL;
while(p1->number!=0)
{
n++;
if(n==1)head=p1;
else
p2->next=p1;
p2=p1;
p1=(struct Information *)malloc(LL);
scanf("%s%d%lf",p1->name,&p1->number,&p1->Tel_number);
getchar();
}
p2->next=NULL;
free(p1);
p1=NULL;
return head;
}
void output(struct Information *p3)
{
printf("\n你建立的是多少人的通讯录: n=%d\n ",n);
printf("\n输出存入的通信信息:\n");
printf("\n姓名\t学号\t电话号码\n");
while(p3!=NULL)
{
printf("%s\t%d\t%.0lf\n",p3->name,p3->number,p3->Tel_number);
p3=p3->next;
}
}
void file(struct Information *p8)
{
FILE *fp;
int i;
t=n;
if((fp=fopen("Infor.dat","wb"))==NULL)
{
printf("此文件无法打开!\n");
exit(0);
}
for(i=0;i<t;i++)
{
if(fwrite(&p8->name,LL,1,fp)!=1)
if(fwrite(&p8->number,LL,1,fp)!=1)
if(fwrite(&p8->Tel_number,LL,1,fp)!=1)
p8=p8->next;
else
printf("此文件写入错误\n");
}
fclose(fp);
}
void Insert(struct Information *head,struct Information *node,int num1)
{
struct Information *p;
if(head==NULL)
{
head=node;
node->next=NULL;
n++;
printf("你插入后总共有结点:n=%d\n",n);
}
p=head;
while(p->number!=num1 && p->next!=NULL)
{
p=p->next;
}
if(p->number==num1)
{
node->next=p->next;
p->next=node;
n++;
printf("你插入后总共有结点:n=%d\n",n);
}
else
printf("\nthe num1 is't found!\n");
printf("\n输出插入一个学生的通讯信息后的通讯录:\n");
printf("\n姓名\t学号\t电话号码\n");
p=head;
while(p!=NULL)
{
printf("%s\t%d\t%.0lf\n",p->name,p->number,p->Tel_number);
p=p->next;
}
}
void Delete(struct Information *head,int num2)
{
struct Information *p5,*p6,*p7;
if(head==NULL)
{
printf("\nthis is a null list!\n");
}
p6=head;
while(p6->number!=num2 && p6->next!=NULL)
{
p5=p6;
p6=p6->next;
}
if(p6->number==num2)
{
if(head==p6)
{
head=head->next;
}
else
p5->next=p6->next;
n--;
printf("你 删除 后总共有结点:n=%d\n",n);
}
else
printf("\nthe %d didn't been found!\n");
printf("\n输出 删除 一个学生的通讯信息后的通讯录:\n");
printf("\n姓名\t学号\t电话号码\n");
free(p6);
p6=NULL;
p7=head;
while(p7!=NULL)
{
printf("%s\t%d\t%.0lf\n",p7->name,p7->number,p7->Tel_number);
p7=p7->next;
}
}
int main()
{
struct Information *p0,*p4;
int r,m;
p0=creat();
output(p0);
file(p0);
printf("请输入要 插入 信息:\n");
p4=(struct Information *)malloc(LL);
scanf("%s%d%lf",p4->name,&p4->number,&p4->Tel_number);
getchar();
printf("请输入 插在 那个学号:\n");
scanf("%d",&r);
Insert(p0,p4,r);
printf("请输入 删除 那个学号:\n");
scanf("%d",&m);
Delete(p0,m);
return 0;
}
#define LL sizeof(struct Information)
#include<stdlib.h>
struct Information
{
char name[25];
int number;
double Tel_number;
struct Information *next;
};
int n,t;
struct Information *creat()
{
struct Information *head,*p1,*p2;
n=0;
p1=p2=(struct Information *)malloc(LL);
printf("请输入第 %d 个学生通讯信息:\n",n+1);
printf("姓名\t学号\t电话号码\n");
scanf("%s%d%lf",p1->name,&p1->number,&p1->Tel_number);
getchar();
head=NULL;
while(p1->number!=0)
{
n++;
if(n==1)head=p1;
else
p2->next=p1;
p2=p1;
p1=(struct Information *)malloc(LL);
scanf("%s%d%lf",p1->name,&p1->number,&p1->Tel_number);
getchar();
}
p2->next=NULL;
free(p1);
p1=NULL;
return head;
}
void output(struct Information *p3)
{
printf("\n你建立的是多少人的通讯录: n=%d\n ",n);
printf("\n输出存入的通信信息:\n");
printf("\n姓名\t学号\t电话号码\n");
while(p3!=NULL)
{
printf("%s\t%d\t%.0lf\n",p3->name,p3->number,p3->Tel_number);
p3=p3->next;
}
}
void file(struct Information *p8)
{
FILE *fp;
int i;
t=n;
if((fp=fopen("Infor.dat","wb"))==NULL)
{
printf("此文件无法打开!\n");
exit(0);
}
for(i=0;i<t;i++)
{
if(fwrite(&p8->name,LL,1,fp)!=1)
if(fwrite(&p8->number,LL,1,fp)!=1)
if(fwrite(&p8->Tel_number,LL,1,fp)!=1)
p8=p8->next;
else
printf("此文件写入错误\n");
}
fclose(fp);
}
void Insert(struct Information *head,struct Information *node,int num1)
{
struct Information *p;
if(head==NULL)
{
head=node;
node->next=NULL;
n++;
printf("你插入后总共有结点:n=%d\n",n);
}
p=head;
while(p->number!=num1 && p->next!=NULL)
{
p=p->next;
}
if(p->number==num1)
{
node->next=p->next;
p->next=node;
n++;
printf("你插入后总共有结点:n=%d\n",n);
}
else
printf("\nthe num1 is't found!\n");
printf("\n输出插入一个学生的通讯信息后的通讯录:\n");
printf("\n姓名\t学号\t电话号码\n");
p=head;
while(p!=NULL)
{
printf("%s\t%d\t%.0lf\n",p->name,p->number,p->Tel_number);
p=p->next;
}
}
void Delete(struct Information *head,int num2)
{
struct Information *p5,*p6,*p7;
if(head==NULL)
{
printf("\nthis is a null list!\n");
}
p6=head;
while(p6->number!=num2 && p6->next!=NULL)
{
p5=p6;
p6=p6->next;
}
if(p6->number==num2)
{
if(head==p6)
{
head=head->next;
}
else
p5->next=p6->next;
n--;
printf("你 删除 后总共有结点:n=%d\n",n);
}
else
printf("\nthe %d didn't been found!\n");
printf("\n输出 删除 一个学生的通讯信息后的通讯录:\n");
printf("\n姓名\t学号\t电话号码\n");
free(p6);
p6=NULL;
p7=head;
while(p7!=NULL)
{
printf("%s\t%d\t%.0lf\n",p7->name,p7->number,p7->Tel_number);
p7=p7->next;
}
}
int main()
{
struct Information *p0,*p4;
int r,m;
p0=creat();
output(p0);
file(p0);
printf("请输入要 插入 信息:\n");
p4=(struct Information *)malloc(LL);
scanf("%s%d%lf",p4->name,&p4->number,&p4->Tel_number);
getchar();
printf("请输入 插在 那个学号:\n");
scanf("%d",&r);
Insert(p0,p4,r);
printf("请输入 删除 那个学号:\n");
scanf("%d",&m);
Delete(p0,m);
return 0;
}
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询