一个简单的电话簿,我用TC3.0 VC++ MYTC5.2都编译不过去,而且错误的位置还不一样
三.函数原形说明
#define NULL 0
#define LEN sizeof(struct telephone)
#include<stdio.h>
struct telephone *insert(struct );
struct telephone *del(char );
int bijiao_f();
struct telephone *search(char );
struct telephone *change(char );
struct telephone *print();
struct telephone *list_f();
void insert_f();
void del_f();
void search_f();
void change_f();
void print_f();
void exit_f();
void menu_f();
mainmenu();
main();
四.电话簿系统的原程序
struct telephone /*结构体类型定义,包括:用户姓名和电话号码*/
{ char name[20];
char number[20];
struct telephone *next;
};
int n,k=0;
FILE *fp;
struct telephone *head; /*全程变量,链头指针*/
struct telephone *creat(void)/*全程变量,用于指明当前在链表中的位置*/
{ struct telephone *p1,*p2;
fp=fopen("d:\\cage.txt","r"); /*建立cage储存文件,用户资料将存入其中*/
n=0;
p1=p2=(struct telephone *) malloc(LEN);
fscanf(fp,"%s %s", p1->name,p2->number);
head=NULL;
while(p1->name[0]!='0')
{ n=n+1;
if(n==1) head=p1;
else p2->next=p1;
p2=p1;
p1=(struct telephone *) malloc(LEN);
fscanf(fp,"%s %s",p1->name,p1->number);
}
p2->next=NULL;
fclose(fp);
}
struct telephone *insert(struct telephone *insert_name)
/* 在链表中增加一个记录 */
{ struct telephone *p0,*p1,*p2;
p1=head; p0=insert_name;
if(head==NULL) {head=p0;p0->next=NULL;}
else
{ while((strcmp(p0->name,p1->name)>0)&&(p1->next!=NULL))
/* 根据姓氏,返回其在链表中的正确位置。新节点即将插入此点*/
{ p2=p1; p1=p1->next;}
if(strcmp(p0->name,p1->name)<= 0)
{ if(head==p1) head=p0;
else p2->next=p0;
p0->next=p1;}
else
{ p1->next=p0;p0->next=NULL;}
}
n=n+1;k=1;
}
struct telephone *del(char name_del[20]) /* 删除节点*/
{ struct telephone *p1,*p2;
p1=p2=head;
if(head==NULL){printf("\n电话簿为空!\n");system("pause");mainmenu();}
else
{
while((strcmp(name_del,p1->name)!=0)&&(p1->next!=NULL))
{p2=p1;p1=p1->next;}
if (strcmp(name_del,p1->name)==0)
{ if(p1==head) head=p1->next;
else p2->next=p1->next;
n=n-1;k=1;
}
else printf("%s不存在!\n",name_del);
}
}
int bijiao_f(char name_search[20],char name[20])/*比较字符,使查询功能完备*/
{int k=0;int i,t;
t=strlen(name_search);
for(i=0;i<=t-1&&name_search[i]==name[i];i++) {k++;}
if(k==t) return(1);
else return(0);
}
struct telephone *search(char name_search[20])
/*利用姓名进行查询(可输入前几位字母进行查询)*/
{
struct telephone *p;int j=0,h=0;
p=head;
if(head==NULL){printf("\n电话簿为空!\n"); system("pause");mainmenu();}
else {
while(p->next!=NULL)
{h=bijiao_f(name_search,p->name);
if(h==1)
{j=1;
printf("恭喜你!找到用户: ");
printf("%s\n号码:%s\n",p->name,p->number);
}
p=p->next;
}
if(j==0) {printf(" %s不存在\n",name_search); search_f();}
}
system("pause");mainmenu();
}
struct telephone *change(char name_change[20]) /*修改用户资料*/
{ struct telephone *p;
p=head;
if(head==NULL) {printf("\n电话簿为空!\n");system("pause");mainmenu();}
else {
while(strcmp(name_change,p->name)!=0 && p->next!=NULL) p=p->next;
if(strcmp(name_change,p->name)==0)
{ printf("\n");
printf("找到用户: ");
printf("%s",p->name);
printf("\n号码:%s\n",p->number);
del(p->name);
printf("将姓名修改为:\n");
scanf("%s",p->name);
del(p->number);
printf("将号码修改为:\n");
scanf("%s",p->number);
insert(p);
k=1;
printf("修改成功\n完成自动排序.\n");
system("pause");
mainmenu(); /*返回主菜单*/
}
else {printf("%s不存在!\n",name_change);change_f();}
}
}
struct telephone *print()
{ struct telephone *p;
fp=fopen("d:\\cage.txt","w");
p=head;
if (head!=NULL)
do
{
fprintf(fp,"%s ",p->name);
p=p->next;
}while(p!=NULL);
fprintf(fp,"0");
fclose(fp);
k=0;
}
void insert_f() /*添加用户资料*/
{struct telephone *name_insert;
printf("输入要插入的用户的姓名和号码<输入0返回主菜单>:");
name_insert=(struct telephone *)malloc(LEN);
scanf("%s %s",name_insert->name,name_insert->number);
while(name_insert->name[0]!='0')
{ insert(name_insert);
printf("success\n");
printf("输入要插入的用户的姓名和号码<输入0返回主菜单>:");
name_insert=(struct telephone *)malloc(LEN);
scanf("%s %s",name_insert->name,name_insert->number);
}
mainmenu(); /*返回主菜单*/
}
void del_f() /*删除用户资料*/
{char name_del[20];
printf("输入要删除的用户<输入0返回主菜单>:");
scanf("%s",&name_del);
while(name_del[0]!='0')
{ del(name_del);printf("success\n");
printf("输入要删除的用户<输入0返回主菜单>:");
scanf("%s",&name_del);
}