vb课程设计:通讯录管理系统
2个回答
展开全部
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct addr
{
char name[30];
char street[40];
char city[20];
char state[3];
char tel[11];
struct addr *next;
}record;
struct addr *head,*start;
struct addr *last;
int menu_select();
void enter();
void deleted();
void search();
void main()
{
char s[80],choice;
struct addr *info;
start=last=NULL;
switch(menu_select())
{
case 1:enter();
break;
case 2:deleted();
break;
case 3:search();
break;
case 4:exit(0);
}
}
int menu_select()
{
char s[80];
int c;
printf("1__Enter a name\n");
printf("2__Delete a record\n");
printf("3__Search\n");
printf("4__Quit\n");
do
{
printf("\n Enter your choice:");
gets(s);
c=atoi(s);
}while(c<1||c>4);
return(c);
}
struct addr* des_store(struct addr *i,struct addr *top)
{
if(!last)
{
last=i;
return(i);
}
else
{
top->next=i;
i->next=NULL;
last=i;
return(i);
}
}
void enter()
{
struct addr *info;
void inputs(char *,char *,int );
int n;
for(n=0;n<2;n++)
{
info=(struct addr *)malloc(sizeof(record));
if(info==NULL)
{
printf("\n out of memory");
return;
}
inputs("enter name:",info->name,30);
if(info->name[0]=='0')
break;
else
{
inputs("enter street:",info->street,40);
inputs("enter city:",info->city,20);
inputs("enter state:",info->state,3);
inputs("enter tel:",info->tel ,11);
start=des_store(info,start);
if(n==0)
head=start;
}
}
}
void inputs(char *p1,char* s,int count)
{
char p[40];
do
{
printf("%s",p1);
gets(p);
if (strlen(p)>count)
printf("\n too long\n");
}while(strlen(p)>count);
strcpy(s,p);
}
void display(struct addr *info)
{
printf("%s\n",info->name);
printf("%s\n",info->street);
printf("%s\n",info->city);
printf("%s\n",info->tel);
printf("\n\n");
}
struct addr *find(char *name)
{
struct addr *info;
info=head;
while(info)
{
if(!strcmp(name,info->name))
return(info);
else
info=info->next;
}
return(info);
}
void search()
{
char name[40];
struct addr *info;
printf("enter name to find:");
gets(name);
if((info=find(name))==NULL)
printf("not found\n");
else
display(info);
}
void deleted()
{
char s[80];
struct addr*p1,*p2,*info;
printf("enter name:");
gets(s);
info=find(s);
if(info!=NULL)
{
if (head==info)
{
head=info->next;
printf("deleted:%s\n",info->name);
free(info);
}
else
{
p1=head->next;
while(info!=p1)
{
p2=p1;
p1=p1->next;
}
p2->next=p1->next;
printf("deleted:%s\n",info->name);
free(info);
}
}
else
printf("%s not find!\n",info->name);
}
这是一段通讯录的代码!!!你可以自己调试下!!!我反正用了没问题!望采纳
#include <stdlib.h>
#include <string.h>
struct addr
{
char name[30];
char street[40];
char city[20];
char state[3];
char tel[11];
struct addr *next;
}record;
struct addr *head,*start;
struct addr *last;
int menu_select();
void enter();
void deleted();
void search();
void main()
{
char s[80],choice;
struct addr *info;
start=last=NULL;
switch(menu_select())
{
case 1:enter();
break;
case 2:deleted();
break;
case 3:search();
break;
case 4:exit(0);
}
}
int menu_select()
{
char s[80];
int c;
printf("1__Enter a name\n");
printf("2__Delete a record\n");
printf("3__Search\n");
printf("4__Quit\n");
do
{
printf("\n Enter your choice:");
gets(s);
c=atoi(s);
}while(c<1||c>4);
return(c);
}
struct addr* des_store(struct addr *i,struct addr *top)
{
if(!last)
{
last=i;
return(i);
}
else
{
top->next=i;
i->next=NULL;
last=i;
return(i);
}
}
void enter()
{
struct addr *info;
void inputs(char *,char *,int );
int n;
for(n=0;n<2;n++)
{
info=(struct addr *)malloc(sizeof(record));
if(info==NULL)
{
printf("\n out of memory");
return;
}
inputs("enter name:",info->name,30);
if(info->name[0]=='0')
break;
else
{
inputs("enter street:",info->street,40);
inputs("enter city:",info->city,20);
inputs("enter state:",info->state,3);
inputs("enter tel:",info->tel ,11);
start=des_store(info,start);
if(n==0)
head=start;
}
}
}
void inputs(char *p1,char* s,int count)
{
char p[40];
do
{
printf("%s",p1);
gets(p);
if (strlen(p)>count)
printf("\n too long\n");
}while(strlen(p)>count);
strcpy(s,p);
}
void display(struct addr *info)
{
printf("%s\n",info->name);
printf("%s\n",info->street);
printf("%s\n",info->city);
printf("%s\n",info->tel);
printf("\n\n");
}
struct addr *find(char *name)
{
struct addr *info;
info=head;
while(info)
{
if(!strcmp(name,info->name))
return(info);
else
info=info->next;
}
return(info);
}
void search()
{
char name[40];
struct addr *info;
printf("enter name to find:");
gets(name);
if((info=find(name))==NULL)
printf("not found\n");
else
display(info);
}
void deleted()
{
char s[80];
struct addr*p1,*p2,*info;
printf("enter name:");
gets(s);
info=find(s);
if(info!=NULL)
{
if (head==info)
{
head=info->next;
printf("deleted:%s\n",info->name);
free(info);
}
else
{
p1=head->next;
while(info!=p1)
{
p2=p1;
p1=p1->next;
}
p2->next=p1->next;
printf("deleted:%s\n",info->name);
free(info);
}
}
else
printf("%s not find!\n",info->name);
}
这是一段通讯录的代码!!!你可以自己调试下!!!我反正用了没问题!望采纳
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询