求大家帮我解决这个问题啊,同学都不会!!!!
#include<stdio.h>#include<stdlib.h>
#include<windows.h>
#define m_name 11
#define m_sex 3
#define m_birthday 10
#define m_tel 21
#define m_mobile 21
#define m_fax 21
#define m_address 101
#define m_youbian 7
////addr_book为一个结构体类型
typedef struct _person
{
char name[m_name];
char sex[m_sex];
char birthday[m_birthday];
char tel[m_tel];
char mobile[m_mobile];
char fax[m_fax];
char address[m_address];
char youbian[m_youbian];
}person;
typedef struct _info
{
person per;
struct _info *next;
} addr_book;
addr_book *first=NULL;
int main()
{
void add();
void show();
void search();
void delete1();
void update();
void save();
void quit();
int print_menu();
addr_book *get_last(addr_book *from);
void print_person(person *p);
void input_person(person *p);
int n;
while(1)
{
n=print_menu();
switch(n)
{
case 1:add();break;
case 2:show();break;
case 3:search();break;
case 4:delete1();break;
case 5:update();break;
case 6:save();break;
case 7:quit();break;
}
}
return 0;
}
void add()
{ addr_book *get_last();
char input;
addr_book *last=NULL;
addr_book *new_addr=(addr_book*)malloc(sizeof(addr_book));
new_addr->next=NULL;
if(first==NULL)
{
first=new_addr;
}
else
{
last=get_last(addr_book *first);
last->next=new_addr;
}
input_person(&(new_addr->per));
printf(">继续输入?(Y 继续,N 返回菜单)");
getchar();
input=getchar();
if(input=='Y' || input=='y')
{
add();
}
}
void show()
{
int i=0;
addr_book *p=first;
while(p!=NULL)
{
i++;
printf("*******第%d个联系人*************\n",i);
print_person(&(p->per));
p=p->next;
}
if(i=0)
{
printf("没有该联系人哦!");
}
printf("请按任意键返回菜单……");
getchar();
getchar();
}
void search()
{
int count=0;
char input;
char name[m_name];
addr_book *p=first;
printf(">请输入亲要查找的联系人的姓名(最多%d个字符哦):",m_name-1);
scanf("%s",name);
while(p!=NULL)
{
if(strcmp(p->per.name,name)==0)
{
print_person(&(p->per));
count++;
}
p=p->next;
}
if(count=0)
{
printf("您好像没有%s的联系方式哦",name);
}
printf("您想继续查找吗?(Y 继续查找,N 返回菜单)");
getchar();
input=getchar();
if(input=='Y' || input=='y')
{
search();
}
}
void delete1()
{
int count=0;
char input;
char name[m_name];
addr_book *p=first;
addr_book *p1=NULL;
printf("请输入你想删除的联系人的姓名(最多%d个字符):",m_name-1);
scanf("%s",name);
while(p!=NULL)
{
if(strcmp(p->per.name,name)==0)
{
print_person(&(p->per));
count++;
break;
}
p=p->next;
}
if(count==0)
{
printf("对不起,没有%s的信息",name);
}
else
{
printf("确定要删除%s这个联系人吗?",name);
getchar();
input=getchar();
if(input=='Y' || input=='y')
{
if(p==first)
{
first=p->next; ////删除了仅有的联系人,头指针归为空
}
else
{
p1=first;
while(p1!=NULL)
{
if(p1->next==p)
{
p1->next=p->next;
break;
}
p1=p1->next;
}
}
free(p);
}
}
printf("继续删除其他联系人吗?(Y 继续删除,N 返回菜单)");
getchar();
input=getchar();
if(input=='Y' || input=='y')
{
delete1();
}
}
void update()
{
int count=0;
char input;
char name[m_name];
addr_book *p=first;
printf("请输入您要更新的联系人的姓名(最多%d个字符)",m_name-1);
scanf("%s",name);
while(p!=NULL)
{
if(strcmp(p->per.name,name)==0)
{
print_person(&(p->per));
count++;
break;
}
p=p->next;
}
if(count=0)
{
printf("没有%d这个联系人",name);
}
else
{
input_person(&(p->per));
}
printf("继续删除其他联系人吗?(Y 继续删除,N 返回菜单)");
getchar();
input=getchar();
if(input=='Y' || input=='y')
{
update();
}
}
void save()
{
FILE *fp;
char name[50];
addr_book *p=first;
printf("请输入需要保存的文件名:");
scanf("%s",name);
fp=fopen(name,"w");
while(p!=NULL)
{
fprintf(fp,"%s,%s,%s,%s,%s,%s,%s,%s\n",
p->per.name,p->per.sex,p->per.birthday,p->per.tel,
p->per.mobile,p->per.fax,p->per.address,p->per.youbian);
p=p->next;
}
fclose(fp);
printf("恭喜你保存成功了!\n请按任意键返回");
getchar();
getchar();
}
void quit()
{
addr_book *p=first;
addr_book *pp=NULL;
if(p=NULL)
{
exit(0);
}
while(p->next!=NULL)
{
pp=p->next;
free(p);
p=pp;
}
free(pp);
exit(0);
}
int print_menu()
{
int choose=0;
/////家动画
printf("请选择:");
scanf("%d",choose);
return choose;
}
void input_peron(person *p)
{
printf("请输入联系人信息:\n");
}
void print_person(person *p)
{
}
addr_book *get_last(addr_book *from)
{
addr_book *p=from;
while(p->next!=NULL)
{
p=p->next;
}
return p;
}