编写C语言一个通讯录程序

编写C语言一个通讯录程序,第1张

这是我以前写的课程设计,

电子通讯录功能如下:

Function choose

1.Read

2.Append

3.Delete

4.Search

5.Save and exit

6.Quit

说明:上图是电子通讯录的主菜单,利用它,将能够轻松地录入一个朋友的电话号

码,通讯地址和出生日期,而且它还提供了检索和删除功能。在后面还将提供按生

日先后排序的功能,这些都有助于该通讯录的管理。

电子通讯录是采用线性表作为程序的基本结构的。

设计思想:

1。用顺序表设计电子通讯录的结构

为了表示较为复杂的数据内容,一般用结构这种数据类型,第一步就是在结构中定

义所需要的各项信息。

一般的通讯录都包括姓名,性别,出生年月,通讯地址和联系电话这几项,而

在这几项中,出生年月又包括年份,月份和日期三项,通讯地址包括邮编和家庭地

址二项,我们把这些联系较为紧密的内容又用单独的结构表示,这样就产生了电子

通讯录的基本结构:

struct addr /*通讯地址结构定义*/

{ char post_num[10]/*邮编*/

char addr[40]/*家庭地址*/

}

struct birth /*出生年月结构定义*/

{ int year/*年份*/

int month/*月份*/

int day/*日期*/

}

struct friend /*电子通讯录结构定义*/

{ int number/*序号*/

char name[20] /*姓名*/

char sex/*性别*/

struct birth birth/*出生年月*/

struct addr addr/*通信地址*/

char telephone[13]/*联系电话*/

}

定义的主结构friend包含了前述的五项内容 name,sex,和telephone分别代表

了姓名,性别和联系电话。为了让结构中的各项组分更加清晰,定义了二个

小结构birth 和addr分别代表出生年月和通讯地址,因此实际上friend包含了

8项内容。

有了结构定义后,我们可以很轻松地构造出电子通讯录的主体:

stryct friend friends[50]

采用一维数组 friends[50],正是用到了顺序表这种最简单的数据结构来表示

问题。

2.增添电子通讯录中的内容

对于电子通讯录这样一张顺序表来说,内容的录入是必不可少的 *** 作。由

于采用的是顺序存储结构。这项工作很简单,只需要在把输入的信息按顺序放

在空的friends数组元素中即可。函数Data_Input完成了信息的录入工作:

void Data_input(int j)

{

friends[j].number=j

printf("\n\n\n\tNo %d record",j)

printf("\n\n\tName:")/*读入姓名*/

scanf("%s",friends[j].name)

printf("\n\tSex(m/f):")/*读入姓别*/

scanf("%c",&friends[j].sex)

printf("\n\tbirthday:")/*读入出生年月*/

printf("\n\t\tyear:")

scanf("%d",&friends[j].birth.year)

printf("\n\t\tmonth")

scanf("%d",&friends[j].birth.month)

printf("\n\t\tday")

scanf("%d",&friends[j].birth.day)

printf("\n\tPost number:")/*读入邮编*/

scanf("%s",friends[j].addr.post_num)

printf("\n\tAddress:")/*读入家庭地址*/

scanf("%s",friends[j].addr.addr)

printf("\n\ttelephone:")/*读入联系电话*/

scanf("%s",friends[j].telephone)

}

#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=0i<ti++)

{

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

}


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/yw/8158268.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-04-14
下一篇2023-04-14

发表评论

登录后才能评论

评论列表(0条)

    保存