|
|
|
|
#include<stdio.h>
|
|
|
|
|
#include<stdlib.h>
|
|
|
|
|
#include<conio.h>
|
|
|
|
|
#include<dos.h>
|
|
|
|
|
#include<string.h>
|
|
|
|
|
#define LEN sizeof(struct addritem)
|
|
|
|
|
#define FORMAT "%-15s%-20s%-10s%-15s%-30s%\n"//<2F>涨<EFBFBD><E6B6A8><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʽ
|
|
|
|
|
#define DATA addrinfo[i].name,addrinfo[i].occu,addrinfo[i].tel,addrinfo[i].email,addrinfo[i].address//<2F>ṹ<EFBFBD><E1B9B9><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ԫ<EFBFBD><D4AA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>г<EFBFBD>Ա<EFBFBD><D4B1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
struct addritem
|
|
|
|
|
{
|
|
|
|
|
char name[10];
|
|
|
|
|
char occu[10];
|
|
|
|
|
char address[35];
|
|
|
|
|
char tel[15];
|
|
|
|
|
char email[25];
|
|
|
|
|
};
|
|
|
|
|
struct addritem addrinfo[100];
|
|
|
|
|
void input();
|
|
|
|
|
void search();
|
|
|
|
|
void update();
|
|
|
|
|
void del();
|
|
|
|
|
void display();
|
|
|
|
|
void sort();
|
|
|
|
|
void menu();
|
|
|
|
|
|
|
|
|
|
void menu()
|
|
|
|
|
{
|
|
|
|
|
system("cls");
|
|
|
|
|
printf("\n\n\n\n\n");
|
|
|
|
|
printf("\t\t|----------contact--------|\n");
|
|
|
|
|
printf("\t\t|0.exit |\n");
|
|
|
|
|
printf("\t\t|1.input record |\n");
|
|
|
|
|
printf("\t\t|2.search record |\n");
|
|
|
|
|
printf("\t\t|3.update record |\n");
|
|
|
|
|
printf("\t\t|4.delete record |\n");
|
|
|
|
|
printf("\t\t|5.sort |\n");
|
|
|
|
|
printf("\t\t|6.display |\n");
|
|
|
|
|
printf("\t\t|-------------------------|\n\n");
|
|
|
|
|
printf("\t\t\tchoose(0-6):");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int main()
|
|
|
|
|
{
|
|
|
|
|
int n;
|
|
|
|
|
menu();
|
|
|
|
|
scanf("%d",&n);
|
|
|
|
|
while(n)
|
|
|
|
|
{
|
|
|
|
|
switch(n)
|
|
|
|
|
{ case 1: input(); break;
|
|
|
|
|
case 2: search(); break;
|
|
|
|
|
case 3: update(); break;
|
|
|
|
|
case 4: del(); break;
|
|
|
|
|
case 5: sort(); break;
|
|
|
|
|
case 6: display(); break;
|
|
|
|
|
default:break;
|
|
|
|
|
}
|
|
|
|
|
getch();
|
|
|
|
|
menu();
|
|
|
|
|
scanf("%d",&n);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
void input()
|
|
|
|
|
{
|
|
|
|
|
int i,count=0;//count<6E><74><EFBFBD><EFBFBD><EFBFBD><EFBFBD>¼ͨѶ¼<D1B6><C2BC><EFBFBD><EFBFBD>ϵ<EFBFBD>˵ĸ<CBB5><C4B8><EFBFBD>
|
|
|
|
|
char ch[2];//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>¼<EFBFBD>û<EFBFBD><C3BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
FILE *fp;
|
|
|
|
|
if((fp=fopen("data.txt","a+"))==NULL)//<2F><><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD>ļ<EFBFBD><C4BC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD><C4BC><EFBFBD><EFBFBD><EFBFBD>ʧ<EFBFBD><CAA7><EFBFBD>ؿ<F2B7B5BB>ֵ
|
|
|
|
|
{
|
|
|
|
|
printf("can not open\n");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
while(!feof(fp))//<2F>ж<EFBFBD><D0B6>ļ<EFBFBD><C4BC>Ƿ<EFBFBD><C7B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>û<EFBFBD>е<EFBFBD><D0B5><EFBFBD>fread<61><64><EFBFBD><EFBFBD>ͳ<EFBFBD>Ƽ<EFBFBD>¼<EFBFBD><C2BC><EFBFBD><EFBFBD>
|
|
|
|
|
{
|
|
|
|
|
if(fread(&addrinfo[count],LEN,1,fp)==1)//LENͳ<4E>ƽṹ<C6BD><E1B9B9><EFBFBD><EFBFBD><EFBFBD><EFBFBD>addritem<65>б<EFBFBD><D0B1><EFBFBD><EFBFBD><EFBFBD><EFBFBD>г<EFBFBD>Ա<EFBFBD>ij<EFBFBD><C4B3>ȣ<EFBFBD><C8A3><EFBFBD><EFBFBD><EFBFBD>ռ<EFBFBD><D5BC><EFBFBD>ֽ<EFBFBD><D6BD><EFBFBD>
|
|
|
|
|
count++;
|
|
|
|
|
}
|
|
|
|
|
fclose(fp);
|
|
|
|
|
if(count==0)
|
|
|
|
|
printf("No contact record!\n");
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
system("cls");
|
|
|
|
|
display();
|
|
|
|
|
}
|
|
|
|
|
if((fp=fopen("data.txt","wb"))==NULL)
|
|
|
|
|
{
|
|
|
|
|
printf("can not open!\n");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
for(i=0;i<count;i++)
|
|
|
|
|
fwrite(&addrinfo[i],LEN,1,fp);//<2F><>addrinfo<66>Ľṹ<C4BD><E1B9B9><EFBFBD><EFBFBD><EFBFBD><EFBFBD>д<EFBFBD><D0B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD>
|
|
|
|
|
printf("please input(y/n)");
|
|
|
|
|
scanf("%s",ch);
|
|
|
|
|
while(strcmp(ch,"y")==0||(ch,"Y")==0)//<2F>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD>ıȽϺ<C8BD><CFBA><EFBFBD>
|
|
|
|
|
{ printf("name:");
|
|
|
|
|
scanf("%s",&addrinfo[count].name);//count<6E><74><EFBFBD><EFBFBD><EFBFBD><EFBFBD>¼<EFBFBD>ṹ<EFBFBD><E1B9B9><EFBFBD><EFBFBD><EFBFBD>鵱ǰ <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>λ<EFBFBD><CEBB>
|
|
|
|
|
for(i=0;i<count;i++)
|
|
|
|
|
if(strcmp(addrinfo[i].name,addrinfo[count].name)==0)
|
|
|
|
|
{ printf("the name already exists,press any key to continue.");
|
|
|
|
|
getch();
|
|
|
|
|
fclose(fp);
|
|
|
|
|
return;
|
|
|
|
|
} //<2F><>ͨѶ¼<D1B6>Ҳ<EFBFBD><D2B2><EFBFBD><EFBFBD><EFBFBD>ϵ<EFBFBD>˾<EFBFBD><CBBE><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϣ
|
|
|
|
|
printf("occupation:");
|
|
|
|
|
scanf("%s",&addrinfo[count].occu);
|
|
|
|
|
printf("telephone:");
|
|
|
|
|
scanf("%s",&addrinfo[count].tel);
|
|
|
|
|
printf("address:");
|
|
|
|
|
scanf("%s",&addrinfo[count].address);
|
|
|
|
|
printf("email:");
|
|
|
|
|
scanf("%s",&addrinfo[count].email);
|
|
|
|
|
if(fwrite(&addrinfo[count],LEN,1,fp)!=1)//<2F><>¼<EFBFBD><C2BC><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ϵ<EFBFBD><CFB5><EFBFBD><EFBFBD>Ϣд<CFA2><D0B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD>
|
|
|
|
|
{printf("can not save thr record!");
|
|
|
|
|
getch();
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{printf("%s saved!\n",addrinfo[count].name);
|
|
|
|
|
count++;
|
|
|
|
|
}
|
|
|
|
|
printf("continue?(y/n):");
|
|
|
|
|
scanf("%s",ch);
|
|
|
|
|
}
|
|
|
|
|
fclose(fp);
|
|
|
|
|
printf("ok\n");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void search()
|
|
|
|
|
{
|
|
|
|
|
FILE *fp;
|
|
|
|
|
int i,count=0;
|
|
|
|
|
char ch[2],name[15];
|
|
|
|
|
if((fp=fopen("data.txt","r"))==NULL)
|
|
|
|
|
{
|
|
|
|
|
printf("can not open\n");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
while(!feof(fp))//<2F>ж<EFBFBD><D0B6>Ƿ<C7B7><F1B5BDB4>ļ<EFBFBD>β<EFBFBD><CEB2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD>û<EFBFBD>н<EFBFBD><D0BD><EFBFBD>ʹ<EFBFBD><CAB9>if<69><66><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȡ
|
|
|
|
|
{
|
|
|
|
|
if(fread(&addrinfo[count],LEN,1,fp)==1)
|
|
|
|
|
count++;
|
|
|
|
|
}
|
|
|
|
|
fclose(fp);
|
|
|
|
|
if(count==0)
|
|
|
|
|
{
|
|
|
|
|
printf("no record\n");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
printf("please input the name:");
|
|
|
|
|
scanf("%s",name);
|
|
|
|
|
for(i=0;i<count;i++)
|
|
|
|
|
if(strcmp(name,addrinfo[i].name)==0)
|
|
|
|
|
{
|
|
|
|
|
printf("find the contact,display?(y/n)");
|
|
|
|
|
scanf("%s",ch);
|
|
|
|
|
if(strcmp(ch,"Y")==0||strcmp(ch,"y")==0)
|
|
|
|
|
{
|
|
|
|
|
printf("name occupation telephone email addresse\t\n");
|
|
|
|
|
printf(FORMAT,DATA);
|
|
|
|
|
}break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
void update()
|
|
|
|
|
{
|
|
|
|
|
FILE *fp;
|
|
|
|
|
int i,j,count=0;
|
|
|
|
|
char name[15];
|
|
|
|
|
if((fp=fopen("data.txt","r+"))==NULL)
|
|
|
|
|
{
|
|
|
|
|
printf("can not open");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
while(!feof(fp)){
|
|
|
|
|
printf("%d",count);
|
|
|
|
|
if(fread(&addrinfo[count],LEN,1,fp)==1)
|
|
|
|
|
count++;
|
|
|
|
|
}
|
|
|
|
|
if(count==0)
|
|
|
|
|
{
|
|
|
|
|
printf("no record\n");
|
|
|
|
|
fclose(fp);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
display();
|
|
|
|
|
printf("please input the name of the contact which you want to update\n");
|
|
|
|
|
printf("update name:");
|
|
|
|
|
scanf("%s",&name);
|
|
|
|
|
for(i=0;i<count;i++)
|
|
|
|
|
{ if(strcmp(name,addrinfo[i].name)==0)
|
|
|
|
|
{printf("find the contact!you can update!\n");
|
|
|
|
|
printf("name:");
|
|
|
|
|
scanf("%s",&addrinfo[i].name);
|
|
|
|
|
printf("occuption:");
|
|
|
|
|
scanf("%s",&addrinfo[i].occu);
|
|
|
|
|
printf("telephone<EFBFBD><EFBFBD>");
|
|
|
|
|
scanf("%s",&addrinfo[i].tel);
|
|
|
|
|
printf("email:");
|
|
|
|
|
scanf("%s",&addrinfo[i].email);
|
|
|
|
|
printf("address:");
|
|
|
|
|
scanf("%s",&addrinfo[i].address);
|
|
|
|
|
printf("update successful");//<2F><>Ϣ<EFBFBD><CFA2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>н<EFBFBD><D0BD><EFBFBD><EFBFBD>ģ<DEB8><C4A3>轫<EFBFBD><E8BDAB>Ϣд<CFA2>ش<EFBFBD><D8B4><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
if((fp=fopen("data.txt","wb"))==NULL)
|
|
|
|
|
{
|
|
|
|
|
printf("can not open\n");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
for(j=0;j<count;j++)
|
|
|
|
|
if(fwrite(&addrinfo[j],LEN,1,fp)!=1)
|
|
|
|
|
{
|
|
|
|
|
printf("can not save!");
|
|
|
|
|
getch();
|
|
|
|
|
}
|
|
|
|
|
fclose(fp);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}printf("not find contact");//<2F><>δ<EFBFBD>ҵ<EFBFBD><D2B5><EFBFBD>ϵ<EFBFBD><CFB5><EFBFBD><EFBFBD>Ϣ
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void del()
|
|
|
|
|
{
|
|
|
|
|
FILE *fp;
|
|
|
|
|
int i,j,count=0;
|
|
|
|
|
char ch[2];
|
|
|
|
|
char name[15];
|
|
|
|
|
if((fp=fopen("data.txt","r+"))==NULL)
|
|
|
|
|
{
|
|
|
|
|
printf("can not open\n");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
while(!feof(fp))
|
|
|
|
|
if(fread(&addrinfo[count],LEN,1,fp)==1)
|
|
|
|
|
count++;
|
|
|
|
|
fclose(fp);
|
|
|
|
|
if(count==0)
|
|
|
|
|
{
|
|
|
|
|
printf("no record!\n");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
display();
|
|
|
|
|
printf("please input the name");
|
|
|
|
|
scanf("%s",&name);
|
|
|
|
|
for(i=0;i<count;i++)
|
|
|
|
|
{
|
|
|
|
|
if(strcmp(name,addrinfo[i].name)==0)
|
|
|
|
|
{
|
|
|
|
|
printf("find the contact,del?(y\n)");
|
|
|
|
|
scanf("%s",ch);
|
|
|
|
|
if(strcmp(ch,"Y")||strcmp(ch,"y")==0)
|
|
|
|
|
for(j=i;j<count;j++)//iΪ<69><CEAA><EFBFBD><EFBFBD>ɾ<EFBFBD><C9BE><EFBFBD><EFBFBD>ϵ<EFBFBD>ˣ<EFBFBD><CBA3>轫i<E8BDAB><69><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ϵ<EFBFBD>˶<EFBFBD>ǰ<EFBFBD><C7B0>һ<EFBFBD><D2BB>λ<EFBFBD><CEBB>
|
|
|
|
|
addrinfo[j]=addrinfo[j+1];
|
|
|
|
|
count--;
|
|
|
|
|
if((fp=fopen("data.txt","w"))==NULL)
|
|
|
|
|
{
|
|
|
|
|
printf("can not open");
|
|
|
|
|
return;
|
|
|
|
|
}for(j=0;j<count;j++)
|
|
|
|
|
if(fwrite(&addrinfo[j],LEN,1,fp)!=1)
|
|
|
|
|
{
|
|
|
|
|
printf("can not save!\n");
|
|
|
|
|
getch();
|
|
|
|
|
}
|
|
|
|
|
fclose(fp);
|
|
|
|
|
printf("del successfully!\n");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
printf("not find the contact");
|
|
|
|
|
}
|
|
|
|
|
void sort()
|
|
|
|
|
{
|
|
|
|
|
FILE *fp;
|
|
|
|
|
struct addritem t;
|
|
|
|
|
int i,j,count=0;
|
|
|
|
|
if((fp=fopen("data.txt","r+"))==NULL)
|
|
|
|
|
{
|
|
|
|
|
printf("can not open\n");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
while(!feof(fp))
|
|
|
|
|
if(fread(&addrinfo[count],LEN,1,fp)==1)
|
|
|
|
|
count++;
|
|
|
|
|
fclose(fp);
|
|
|
|
|
if(count==0)
|
|
|
|
|
{
|
|
|
|
|
printf("no record!\n");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
for(i=0;i<count-1;i++)
|
|
|
|
|
for(j=i+1;j<count;j++)
|
|
|
|
|
if(strcmp(addrinfo[i].name,addrinfo[j].name)>0)
|
|
|
|
|
{
|
|
|
|
|
t=addrinfo[i];
|
|
|
|
|
addrinfo[i]=addrinfo[j];
|
|
|
|
|
addrinfo[j]=t;
|
|
|
|
|
}
|
|
|
|
|
if((fp=fopen("data.txt","w"))==NULL)
|
|
|
|
|
{
|
|
|
|
|
printf("can not open\n");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
for(i=0;i<count;i++)
|
|
|
|
|
if(fwrite(&addrinfo[i],LEN,1,fp)!=1)
|
|
|
|
|
{
|
|
|
|
|
printf("can not save!\n");
|
|
|
|
|
getch();
|
|
|
|
|
}
|
|
|
|
|
fclose(fp);
|
|
|
|
|
printf("successfully!\n");
|
|
|
|
|
}
|
|
|
|
|
void display()
|
|
|
|
|
{
|
|
|
|
|
FILE *fp;
|
|
|
|
|
int i,count=0;
|
|
|
|
|
fp=fopen("data.txt","r");
|
|
|
|
|
while(!feof(fp))
|
|
|
|
|
{
|
|
|
|
|
if(fread(&addrinfo[count],LEN,1,fp)==1)
|
|
|
|
|
count++;
|
|
|
|
|
}
|
|
|
|
|
fclose(fp);
|
|
|
|
|
printf("name occupation telephone email address\t\n");
|
|
|
|
|
for(i=0;i<count;i++)
|
|
|
|
|
printf(FORMAT,DATA);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|