#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct address
{
char name[20];
char street[20];
char city[10];
char state[10];
char eip[7];
} Address;
static struct
{
Address* p;
int n;
int cap;
} record = {0, 0, 0};
void init()
{
record.cap = 10;
record.n= 0;
record.p = (Address*)malloc(sizeof(Address)* record.cap);
}
void remalloc()
{
record.cap += 10;
record.p = (Address*)realloc(record.p, sizeof(Address)* record.cap);
}
void destroy()
{
if(record.p != 0)
free(record.p);
}
int menu_select()
{
int nsel = 0;
printf("\t通讯录管理系统软件V1.0\n");
printf("1 增加新记录\n2 删除记录\n3 显示通讯录的所有信息\n");
printf("4 按名字查询信息\n5 保存通讯录\n0 退出\n");
scanf("%d", &nsel);
return nsel;
}
void add()
{
Address tmp;
char flag;
if(record.cap == 0)
init();
printf("增加新记录\n输入姓名、街道、城市、国家、邮编(以空格分隔)\n");
while(1)
{
printf("是否继续?Y(y)/N(n)\n");
fflush(stdin);
scanf("%c", &flag);
if(flag != 'Y' && flag != 'y')
break;
scanf("%s %s %s %s %s", &tmp.name, &tmp.street, &tmp.city, &tmp.state, &tmp.eip);
if(record.n == record.cap)
remalloc();
memcpy(record.p + record.n, &tmp, sizeof(Address));
record.n++;
}
}
void deleteData()
{
char name[20];
char flag;
int ndel = 0;
printf("按姓名删除记录\n输入要删除的姓名\n");
while(1)
{
int i = 0;
printf("是否继续?Y(y)/N(n)\n");
fflush(stdin);
scanf("%c", &flag);
if(flag != 'Y' && flag != 'y')
break;
scanf("%s", name);
for( ; i < record.n; i++)
{
if(strcmp(name, record.p[i].name) == 0)
{
if(i != record.n - 1)
{
memmove(record.p + i, record.p + i + 1,
sizeof(Address) * (record.n - i - 1));
}
record.n--;
ndel++;
}
}
if(ndel == 0)
printf("通讯录中没有%s的记录\n", name);
else
printf("通讯录中共删除了%d条记录\n", ndel);
}
}
void printdata(FILE* stream, int i)
{
Address* ptr = record.p;
fprintf(stream, "%s\t%s\t%s\t%s\t%s\n", ptr[i].name, ptr[i].street,
ptr[i].city, ptr[i].state, ptr[i].eip);
}
void display()
{
int i = 0;
printf("显示通讯录的所有信息\n");
printf("姓名\t街道\t城市\t国家\t邮编\n");
for( ; i < record.n; i++)
printdata(stdout, i);
}
void searchData()
{
char name[20];
char flag;
int nsear = 0;
printf("按姓名查找显示记录\n输入要查找的姓名\n");
while(1)
{
int i = 0;
printf("是否继续?Y(y)/N(n)\n");
fflush(stdin);
scanf("%c", &flag);
if(flag != 'Y' && flag != 'y')
break;
scanf("%s", name);
for( ; i < record.n; i++)
{
if(strcmp(name, record.p[i].name) == 0)
{
if(nsear == 0)
printf("姓名\t街道\t城市\t国家\t邮编\n");
printdata(stdout, i);
nsear++;
}
}
if(nsear == 0)
printf("通讯录中没有%s的记录\n", name);
else
printf("通讯录中共查找到%d条记录\n", nsear);
}
}
void save()
{
FILE* pf = fopen("data.txt", "w");
int i = 0;
for( ; i < record.n; i++)
printdata(pf, i);
fclose(pf);
}
void load()
{
Address tmp;
FILE* pf = fopen("data.txt", "r");
if(record.cap == 0)
init();
while(fscanf(pf, "%s %s %s %s %s", &tmp.name, &tmp.street,
&tmp.city, &tmp.state, &tmp.eip) != -1)
{
if(record.n == record.cap)
remalloc();
memcpy(record.p + record.n, &tmp, sizeof(Address));
record.n++;
}
}
void list()
{
display();
}
int main()
{
int ret;
load();
while((ret = menu_select()) != 0)
{
switch(ret)
{
case 0: break;
case 1: add(); break;
case 2: deleteData(); break;
case 3: display(); break;
case 4: searchData(); break;
case 5: save(); break;
}
}
destroy();
return 0;
}
温馨提示:内容为网友见解,仅供参考