应保证测试用例测试到程序的各种边缘情况
ex.h //头文件
#define LEN sizeof(struct data)
#define NULL 0
#include<stdio.h>
#include<malloc.h>
#include<conio.h>
#include <graphics.h>
void face();
void drawmat(char *mat,int matsize,int x,int y,int color);
void mainmenu();
void menu3(char filename[10]);
void del(char filename[20]);
void add(char filename[10]);
void search(char filename[10]);
void setup();
void show(char filename[10]);
void hebin();
struct data
{
char name[16];
char tel[18];
char sex;
int age;
char birth[10];
char addres[10];
struct data*next;
};
#include"setup.c"
#include"show.c"
#include"search.c"
#include"add.c"
#include"mainmenu.c"
#include"menu3.c"
#include"del.c"
#include"hebin.c"
//头文件
setup.c //创立函数
void setup() /*创建链表,输入数据*/
{
struct data *head;
struct data *p1;
struct data *p2;
struct data *p;
FILE *fp;
char filename[10];
int i,k,n=0;
printf("how many records want to setup:");
scanf("%d",&k);
printf("input name tel sex age birth addres:/n");
p1=p2=(struct data*) malloc(LEN);
scanf("%s %s %c %d %s %s",p1->name,p1->tel,&p1->sex,&p1->age,p1->birth,
p1->addres);
head=NULL;
head=p1;
for(n=0;n<k-1;n++)
{
p1=(struct data*)malloc(LEN);
scanf("%s %s %c %d %s %s",p1->name,p1->tel,&p1->sex,&p1->age,p1->birth,
p1->addres);
p2->next=p1;
p2=p1;
}
p2->next=NULL;
/*将数据写入文件*/
printf("save press 1,if not press 2:");
scanf("%d",&i);
if(i==1)
{
printf("input the filename to save:");
scanf("%s",filename);
fp=fopen(filename,"wb");
for(p=head;p!=0;p=p->next)
{if(fwrite(p,sizeof(struct data),1,fp)!=1)
printf("file write error/n");
};
fclose(fp);
printf("data has been setted up./n");
printf("press enter into mainmenu");
getch();
}
} //创立函数
search.c //查找函数
void search(char filename[10])
{ int n,j=0;
FILE*fp;
struct data p,head;
char name[16];
char tel[18];
char addres[10];
printf("in which way to check:/n");
printf("*************************/n");
printf(" 1------by name/n");
printf(" 2------by tel/n");
printf(" 3------by addres/n");
printf(" 4------back to menu3/n");
printf("*************************/n");
printf("choose please:");
scanf("%d",&n);
while(n!=1&&n!=2&&n!=3&&n!=4)
{ printf("error,choose again:");
scanf("%d",&n);
}
if(n==1)
{ fp=fopen(filename,"rb");
printf("the name want to search:");
scanf("%s",name);
printf("the result:/n");
while(!feof(fp))
{ if(fread(&p,sizeof(struct data),1,fp)!=0)
if(strcmp(name,p.name)==0)
{printf("name tel sex age birthday address/n ");
printf("%s %s %c %d %s %s/n",p.name,p.tel,p.sex,p.age,p.birth,
p.addres);j=1;}
};if(j==0) printf("can not find the information.");
};
if(n==2)
{ fp=fopen(filename,"rb");
printf("the name want to search:");
scanf("%s",tel);
printf("the result:/n");
while(!feof(fp))
{ if(fread(&p,sizeof(struct data),1,fp)!=0)
if(strcmp(tel,p.tel)==0)
{printf("name tel sex age birthday address/n ");
printf("%s %s %c %d %s %s/n",p.name,p.tel,p.sex,p.age,p.birth,
p.addres);j=1;}
};if(j==0) printf("can not find the information.");
};
if(n==3)
{ fp=fopen(filename,"rb");
printf("the name want to search:");
scanf("%s",addres);
printf("the result:/n");
while(!feof(fp))
{ if(fread(&p,sizeof(struct data),1,fp)!=0)
if(strcmp(addres,p.addres)==0)
{printf("name tel sex age birthday address/n ");
printf("%s %s %c %d %s %s/n",p.name,p.tel,p.sex,p.age,p.birth,
p.addres);j=1;}
};if(j==0) printf("can not find the information.");
};
if(n==4) {clrscr();menu3(filename);};
fclose(fp);
} //查找函数
del.C //删除函数
void del(char filename[20])
{
FILE *fp;
struct data p,*head,*p1,*p2;
char name[18];
char tel[10];
int i,n;
printf("in which way to delete:/n");
printf("*************************/n");
printf(" 1------by name/n");
printf(" 2------by tel/n");
printf(" 3------back to menu3/n");
printf("*************************/n");
printf("choose please:");
scanf("%d",&i);
while(i!=1&&i!=2&&i!=3)
{ printf("error,choose again:");
scanf("%d",&i);
};
fp=fopen(filename,"rb");
n=0;;
p1=p2=(struct data*) malloc(LEN);
fread(p1,sizeof(struct data),1,fp);
while(!feof(fp))
{ n=n+1;
if(n==1) head=p1;
else p2->next=p1;
p2=p1;
p1=(struct data*) malloc(LEN);
fread(p1,sizeof(struct data),1,fp);
};
p2=p1;
p2->next=NULL;
if(i==1)
{printf("input the name want to delete:");
scanf("%s",name);
if(head==NULL) {printf("/nlist null!/n");goto end;}
p1=head;
while(strcmp(name,p1->name)!=0&&p1->next!=NULL)
{p2=p1;p1=p1->next;}
if(strcmp(name,p1->name)==0)
{if(p1==head) head=p1->next;
else p2->next=p1->next;
printf("delete:%s/n",name);
}
else {printf("%s not been found!/n",name);
printf("press enter back to menu3.");getch();goto end;}
}
if(i==2)
{printf("input the tel want to delete:");
scanf("%s",tel);
if(head==NULL) {printf("/nlist null!/n");goto end;}
p1=head;
while(strcmp(tel,p1->tel)!=0&&p1->next!=NULL)
{p2=p1;p1=p1->next;}
if(strcmp(tel,p1->tel)==0)
{if(p1==head) head=p1->next;
else p2->next=p1->next;
printf("delete:%s/n",name);
}
else {printf("%s not been found!/n",tel);
printf("press enter back to menu3.");getch();goto end;}
}
fclose(fp);
fp=fopen(filename,"wb");
for(p1=head;p1!=0;p1=p1->next)
{if(fwrite(p1,sizeof(struct data),1,fp)!=1)
printf("file write error/n");
};
fclose(fp);
if(i==3);
end:{clrscr();menu3(filename);}
} //删除函数
add.c //添加函数
void add(char filename[10]) /*创建链表,输入数据*/
{ FILE *fp;
struct data *head;
struct data *p1;
struct data *p2;
int n=0; int i,j;
printf("how many records want to add:");
scanf("%d",&j);
printf("input name tel sex age birth addres/n");
head=p1=p2=(struct data*)malloc(LEN);
scanf("%s %s %c %d %s %s",p1->name,p1->tel,&p1->sex,&p1->age,p1->birth,
p1->addres);
for(n=0;n<j-1;n++)
{
p1=(struct data*)malloc(LEN);
scanf("%s %s %c %d %s %s",p1->name,p1->tel,&p1->sex,&p1->age,p1->birth,
p1->addres);
p2->next=p1;
p2=p1;
}
p2->next=NULL;
printf("add to data press 1,cover old data press 2,neither press 3:");
scanf("%d",&i);
while(i!=1&&i!=2&&i!=3)
{ printf("error,press again:");
scanf("%d",&i);
};
if(i==1)
{ fp=fopen(filename,"ab");
do
{ if((n=fwrite(head,sizeof(struct data),1,fp))!=1)
printf("file write error/n");
head=head->next;
}while(head!=0);fclose(fp);
}
if(i==2)
{fp=fopen(filename,"wb");
do
{ if((n=fwrite(head,sizeof(struct data),1,fp))!=1)
printf("file write error/n");
head=head->next;
}while(head!=0);fclose(fp);
}
} //添加函数
hebin.c //合并函数
void hebin()
{ FILE *fp1,*fp2,*fp;
struct data p1,p2,p;
char file1[10],file2[10];
printf("input the name of the input file:");
scanf("%s",file1);
printf("input the name of the output file:");
scanf("%s",file2);
if((fp1=fopen(file1,"ab"))==NULL)
{printf("cannot open this file");getch();goto end;}
if((fp2=fopen(file2,"rb"))==NULL)
{printf("cannot open this file");getch();goto end;}
while(!feof(fp2))
{ if(fread(&p2,LEN,1,fp2)!=0&&fwrite(&p2,LEN,1,fp1)==1);
}
printf("hepin finished.");
end:fclose(fp2);fclose(fp1);getch(); clrscr();mainmenu();
} //合并函数
show.c //显示函数
void show(char filename[10])
{ FILE *fp;
struct data e;
fp=fopen(filename,"rb");
while(!feof(fp))
{
if(fread(&e,sizeof(struct data),1,fp)!=0)
printf("%s %s %c %d %s %s",e.name,e.tel,e.sex,e.age,e.birth,
e.addres);printf("/n");
};
fclose(fp);
} //显示函数
mainmenu.C //主目录
void mainmenu()
{
char filename[10];
FILE *fp;
gotoxy(25,5); printf("*************************/n");
gotoxy(25,6); printf(" 1----new /n");
gotoxy(25,7); printf(" 2----hebing/n");
gotoxy(25,8); printf(" 3----open/n");
gotoxy(25,9); printf(" 4----exit/n");
gotoxy(25,10); printf("*************************/n");
gotoxy(25,11); printf("choose please:");
switch(getch()-'0')
{
case 1: clrscr();setup();clrscr();mainmenu();break;
case 2: clrscr();hebin();break;
case 3: clrscr();
printf("input name of the file want to edit:");
scanf("%s",filename);clrscr();
if((fp=fopen(filename,"r"))==NULL)
{ gotoxy(25,5);printf("cannot open this file/n");
gotoxy(25,6);printf("press enter back to mainmenu.");
getch();
clrscr();mainmenu();
}
else {fclose(fp);menu3(filename);}break;
case 4: exit(0);
default: clrscr();mainmenu();
}
} // 主目录
menu3.c //子目录
void menu3(char filename[10])
{ int j;
gotoxy(25,5); printf("you open the file is '%s'./n",filename);
gotoxy(25,6); printf("*************************/n");
gotoxy(25,7); printf(" 1----show/n");
gotoxy(25,8); printf(" 2----search/n");
gotoxy(25,9); printf(" 3----delete/n");
gotoxy(25,10); printf(" 4----add/n");
gotoxy(25,11); printf(" 5----back/n");
gotoxy(25,12); printf("*************************/n");
gotoxy(25,13); printf("choose please:");
scanf("%d",&j);
while(j!=1&&j!=2&&j!=3&&j!=4&&j!=5)
{ printf("error,press again:");
scanf("%d",&j);}
switch(j)
{
case 1: clrscr();
show(filename);break;
case 2: clrscr();
search(filename);break;
case 3: clrscr();del(filename);break;
case 4: clrscr();
add(filename);break;
case 5: clrscr(); mainmenu();break;
};
if(j!=5)
{ printf("press enter back to menu3/n");
getch();
clrscr();
menu3(filename);};
} //子目录
main.c //主函数
#include"ex.h"
main()
{
mainmenu();
} // 主函数
2198

被折叠的 条评论
为什么被折叠?



