大家好,我们做c语言的课程设计,我选的是通讯录系统。总是找不出错误,有谁懂c 语言的帮我看一下好吗?用链表做的。大家帮个忙啊!跪谢!以下是我写的源程序!
#include <stdio.h>
#define M sizeof(struct mingdan)
#define H p1->comm[i].name,p1->comm[i].phone,p1->comm[i].addrss,p1->comm[i].qq,p1->comm[i].email
#define T p->comm[i].name,p->comm[i].phone,p->comm[i].addrss,p->comm[i].qq,p->comm[i].email
#define N 10
struct mingdan
{
char name[20];
char phone[20];
char addrss[20];
char qq[20];
char email[20];
struct mingdan *next;
};
struct mingdan comm[N];
struct mingdan *head=NULL;
void input();
main()
{
char t;
for(;;)
{
printf(" *************Tong Xun Lu*************\n\n");
printf(" -------------------------------------\n");
printf(" [0]---Input information\n");
printf(" [1]---Modify info\n");
printf(" [2]---Increase info\n");
printf(" [3]---Delete info\n");
printf(" [4]---Search info\n");
printf(" [5]---Display all\n");
printf(" [6]---Exit\n");
printf("Please choose:");
scanf("%c",&t);
switch(t)
{
case '0':input();break;
case '6':exit(1);
}
}
}
void input()
{
char flag;
int i,t=0;
FILE *fp;
struct mingdan *p1,*p2;
head=p2=p1=(struct mingdan *)malloc(M);
if((fp=fopen("C:\\list.txt","at+"))==NULL)
{
printf("Can't open this file!!!");
getch();
exit(1);
}
fprintf(fp,"%s","Name Telephone Addrss QQ E-mail");
fprintf(fp,"%s","~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
printf("Input num name score of math,c and english:\n");
scanf("%s%s%s%s%s",H);
printf("Input info:\n");
printf("Name Phone Addrss QQ E-mail\n");
printf("-------------------------------\n");
for(i=t;i<N;i++)
{
printf("Input the next infomation:\n");
scanf("%s%s%s%s%s",H);
t++;
if(fwrite(&comm,sizeof(struct mingdan),1,fp)!=1)
{
printf("Error!!!");
getch();
exit(1);
}
else printf("Success!!!\n");
fprintf(fp,"\n");
p2->next=p1;
p2=p1;
}
p2->next=NULL;
}
void delete()
{
FILE *fp;
int i;
struct mingdan *p,*p0;
char name[20];
if((fp=fopen("c:\\list.txt","rt"))==NULL)
{
printf("Open this file failed!!!");
getch();
exit(1);
}
for(i=0;i<N;i++)
{
if(fread(&comm[i],sizeof(struct mingdan),N,fp)!=1)
printf("Read file error!!!");
}
fcolse(fp);
if(head==NULL) {printf("There isn't information in system,couldn't delete!!!\n"); getch(); return;}
printf("Input the name will be delete:\n");
scanf("%s",&name);
p0=head;
if(*(p0->name)==name)
{
head=p0->next;
free(p0);
printf("Delete success!!!\n");
getch();
return;
}
p=p0->next;
while(p!=NULL)
{
if(*(p->name)==name)
{
p0->next=p->next;
free(p);
printf("Delete success!!!\n");
getch();
return;
}
p0=p;
p=p->next;
}
printf("Haven't information of this name!!!\n");
getch();
return;
}
/*注意字符串比较大小*/
void insert()
{
FILE *fp;
int i;
struct mingdan *p,*p0;
if((fp=fopen("c:\\list.txt","rt"))==NULL)
{
printf("Open this file failed!!!");
getch();
exit(1);
}
for(i=0;i<N;i++)
{
if(fread(&comm[i],sizeof(struct mingdan),N,fp)!=1)
printf("Read file error!!!");
}
fcolse(fp);
p=(struct mingdan*)malloc(N);
printf("Input the information that will be insert name phone Addrss QQ E-mail:\n");
scanf("%s %s %s %s %s",T);
p->next=NULL;
if(head==NULL)
{
head=p;
printf("Insert success!!!\n");
getch();
return;
}
p0=head;
if(p0->name>p->name)
{
p->next=head;
head=p;
printf("Insert success!!!\n");
getch(); return;
}
while(p0->next!=NULL&&p0->next->name<p->name)
p0=p0->next;
if(p0->next->name==p->name)
{
free(p);
printf("Have a same name,insert fail!!!\n");
getch(); return;
}
else if(p0->next==NULL)
{
p0->next=p;
p->next=NULL;
printf("Insert success!!!\n");
getch();
return;
}
p->next=p0->next;
p0->next=p;
printf("Insert sucess!!!\n");
getch();
}
void modify()
{
FILE *fp;
int i;
char name[9];
if((fp=fopen("c:\\list.txt","rt"))==NULL)
{
printf("Open this file failed!!!");
getch();
exit(1);
}
for(i=0;i<N;i++)
{
if(fread(&comm[i],sizeof(struct mingdan),N,fp)!=1)
printf("Read file error!!!");
}
fcolse(fp);
struct mingdan *p=head;
printf("Input name:\n");
scanf("%s",name);
while(p!=NULL&&strcmp(p->name,name))
p=p->next;
if(p!=NULL)
{
printf("%s%s%s%s%s",T);
printf("Input num name score of math,c and english:\n");
scanf("%s %s %s %s %s",T);
printf("Modify success!!!");
}
else printf("Haven't info!!!");
getch();
}
void search()
{
FILE *fp;
int i;
if((fp=fopen("c:\\list.txt","rt"))==NULL)
{
printf("Open this file failed!!!");
getch();
exit(1);
}
for(i=0;i<N;i++)
{
if(fread(&comm[i],sizeof(struct mingdan),N,fp)!=1)
printf("Read file error!!!");
}
fcolse(fp);
struct mingdan *p=head;
char name[9];
printf("Input name:\n");
scanf("%s",name);
while(p!=NULL&&strcmp(p->name,name))
p=p->next;
if(p!=NULL)
{
printf("Name Phone Addrss QQ E-mail");
printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
printf("%s%s%s%s%s",T);
}
getch();
}
void display(struct mingdan *p)
{
int i;
FILE *fp;
if((fp=fopen("c:\\list.txt","rt"))==NULL)
{
printf("Open this file failed!!!");
getch();
exit(1);
}
for(i=0;i<N;i++)
{
if(fread(&comm[i],sizeof(struct mingdan),N,fp)!=1)
printf("Read file error!!!");
}
fcolse(fp);
printf("Name Phone Addrss QQ E-mail\n");
while(p!=NULL)
{
printf("%s %s %s %s",T);
p=p->next;
}
}