#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define SUBJECT_COUNT 500
#define Person_COUNT 500
typedef struct str_Person
{
char number[10];
char name[20];
char sex[10];
char birth[10];
char tel[20];
char address[30];
}Person;
int PersonCount = 0;
void inputPerson(int j);
void inputPersons(Person []);
void outputPersons(Person []);
void deletePersons(Person []);
void modifyPersons(Person []);
void findPersons(Person []);
void sortPersons(Person []);
void savePersons(Person []);
void loadPersons(Person []);
void main()
{
Person Person[Person_COUNT];
char choice;
printf("--------------------------------------------------------------------------------");
printf("| ★ 欢迎使用通讯录管理系统 ★ |");
printf("| ----版权归张萌萌所有! |");
printf("--------------------------------------------------------------------------------");
do{
printf(" 1→输入联系人 ");
printf(" 2→输出联系人 ");
printf(" 3→删除联系人 ");
printf(" 4→修改联系人 ");
printf(" 5→查询联系人 ");
printf(" 6→联系人排序 ");
printf(" 7→保存联系人 ");
printf(" 8→加载联系人 ");
printf(" 0→退出 ");
printf("--------------------------------------------------------------------------------\n");
printf("请选择:");
scanf(" %c",&choice);
switch(choice)
{
case'1':
inputPersons(Person);
break;
case'2':
outputPersons(Person);
break;
case'3':
deletePersons(Person);
break;
case'4':
modifyPersons(Person);
break;
case'5':
findPersons(Person);
break;
case'6':
sortPersons(Person);
break;
case'7':
savePersons(Person);
break;
case'8':
loadPersons(Person);
break;
case'0':
printf("thanks\n");
break;
default:
printf("暂无该功能\n");
}
}while(choice != '0');
printf("谢谢使用!\n");
}
void inputPerson(Person * Person,int j)
{
printf("请输入第%d个人的编号:",j+1);
scanf(" %s", Person->number);
printf("请输入第%d个人的姓名:",j+1);
scanf(" %s", Person->name);
printf("请输入第%d个人的性别:",j+1);
scanf(" %s", Person->sex);
printf("请输入第%d个人的出生年月:",j+1);
scanf(" %s", Person->birth);
printf("请输入第%d个人的电话:",j+1);
scanf(" %s", Person->tel);
printf("请输入第%d个人的地址:",j+1);
scanf(" %s", Person->address);
}
void inputPersons(Person Person [])
{
int j;
char ch;
for(j = PersonCount; PersonCount< Person_COUNT; j++)
{
inputPerson(&Person[j],j);
PersonCount++;
printf("是否继续(Y/N)?\n");
scanf(" %c",&ch);
if(ch!='Y'&&ch!='y')
break;
}
}
void outputPersons(Person Person[])
{
int j;
{
printf("---------------------------------------------------------------------\n");
printf("编号\t姓名\t性别\t生日\t\t电话\t\t地址\n");
printf("---------------------------------------------------------------------\n");
for(j = 0;j < PersonCount; j++)
{
printf("%s\t", Person[j].number);
printf("%s\t", Person[j].name);
printf("%s\t", Person[j].sex);
printf("%s\t", Person[j].birth);
printf("%s\t", Person[j].tel);
printf("%s\t", Person[j].address);
printf("\n");
}
printf("\n");
printf("---------------------------------------------------------------------\n");
}
}
void modifyPersons(Person Person[])
{
int i;
char ch;
char number[10];
char name[20];
printf("---------------------------------------------------------------------\n");
printf("------1.姓名修改 2.性别修改 3.出生日期修改 4.电话修改 5.地址修改-----\n");
printf("---------------------------------------------------------------------\n");
scanf(" %c",&ch);
switch(ch)
{
case '1':
printf("请输入要修改姓名的联系人编号:");
scanf(" %s", &number);
for(i=0;i < PersonCount;i++)
{
if(strcmp(Person[i].number,number)==0)
break;
}
if(i < PersonCount)
{
printf("%s\t", Person[i].number);
printf("%s\t", Person[i].name);
printf("%s\t", Person[i].sex);
printf("%s\t", Person[i].birth);
printf("%s\t", Person[i].tel);
printf("%s\t", Person[i].address);
printf("\n");
printf("请修改:\n");
printf("请输入姓名:",i+1);
scanf(" %s", Person[i].name);
printf("修改成功\n");
}
else
printf("未找到此人。\n");
break;
case '2':
printf("请输入要修改性别的人的姓名:");
scanf(" %s", &name);
for(i=0;i<PersonCount;i++)
{
if(strcmp(Person[i].name,name)==0)
break;
}
if(i < PersonCount)
{
printf("%s\t", Person[i].number);
printf("%s\t", Person[i].name);
printf("%s\t", Person[i].sex);
printf("%s\t", Person[i].birth);
printf("%s\t", Person[i].tel);
printf("%s\t", Person[i].address);
printf("\n");
printf("请修改:");
printf("请输入性别:",i+1);
scanf(" %s", Person[i].sex);
printf("修改成功\n");
}
else
printf("未找到此人。\n");
break;
case '3':
printf("请输入要出生年月的人的姓名:");
scanf(" %s", &name);
for(i=0;i<PersonCount;i++)
{
if(strcmp(Person[i].name,name)==0)
break;
}
if(i<PersonCount)
{
printf("%s\t", Person[i].number);
printf("%s\t", Person[i].name);
printf("%s\t", Person[i].sex);
printf("%s\t", Person[i].birth);
printf("%s\t", Person[i].tel);
printf("%s\t", Person[i].address);
printf("\n");
printf("请修改:");
printf("请输入出生年月:",i+1);
scanf(" %s", Person[i].birth);
printf("修改成功\n");
}
else
printf("未找到此人。\n");
break;
case '4':
printf("请输入要修改电话号码的人的姓名:");
scanf(" %s", &name);
for(i=0;i<PersonCount;i++)
{
if(strcmp(Person[i].name,name)==0)
break;
}
if(i<PersonCount)
{
printf("%s\t", Person[i].number);
printf("%s\t", Person[i].name);
printf("%s\t", Person[i].sex);
printf("%s\t", Person[i].birth);
printf("%s\t", Person[i].tel);
printf("%s\t", Person[i].address);
printf("\n");
printf("请修改:");
printf("请输入电话号码:",i+1);
scanf(" %s", Person[i].tel);
printf("修改成功\n");
}
else
printf("未找到此人。\n");
break;
case '5':
printf("请输入要修改地址的人的姓名:");
scanf(" %s", &name);
for(i=0;i<PersonCount;i++)
{
if(strcmp(Person[i].name,name)==0)
break;
}
if(i<PersonCount)
{
printf("%s\t", Person[i].number);
printf("%s\t", Person[i].name);
printf("%s\t", Person[i].sex);
printf("%s\t", Person[i].birth);
printf("%s\t", Person[i].tel);
printf("%s\t", Person[i].address);
printf("\n");
printf("请修改:");
printf("请输入地址:",i+1);
scanf(" %s", Person[i].address);
printf("修改成功\n");
}
else
printf("未找到此人。\n");
break;
}
}
void deletePersons(Person Person[])
{
int i;
int index;
char ch;
printf("---------------------------------------------------------------------\n");
printf(" 1.单个联系人删除 2.全部联系人删除 \n");
printf("---------------------------------------------------------------------\n");
scanf(" %c",&ch);
switch(ch)
{
case '1':
printf("请输入要删除的位置: ");
do
{
scanf(" %d", &index);
if(index<1 || index>PersonCount)
printf("请输入%d-%d之间的位置",1,PersonCount);
}while(index<1 || index>PersonCount);
for(i=index-1; i<PersonCount-1; i++)
Person[i]=Person[i+1];
PersonCount--;
printf("删除成功");
printf("\n");
break;
case '2':
PersonCount = 0;
printf("全部删除成功");
printf("\n");
break;
}
}
void findPersons(Person Person [])
{
int i;
char tel[200];
char name[200];
char ch;
printf("---------------------------------------------------------------------\n");
printf(" 1.按姓名查找 2.按电话号查找 \n");
printf("---------------------------------------------------------------------\n");
scanf(" %c",&ch);
switch(ch)
{
case'1':
printf("请输入一个姓名:");
scanf(" %s", name);
for(i=0;i<PersonCount;i++)
{
if(strcmp(Person[i].name,name)==0)
break;
}
if(i<PersonCount)
{
printf("%s\t", Person[i].number);
printf("%s\t", Person[i].name);
printf("%s\t", Person[i].sex);
printf("%s\t", Person[i].birth);
printf("%s\t", Person[i].tel);
printf("%s\t", Person[i].address);
}
else
printf("未找到此人。\n");
break;
case'2':
printf("请输入一个电话号:");
scanf(" %s", tel);
for(i=0;i<PersonCount;i++)
{
if(strcmp(Person[i].tel,tel)==0)
break;
}
if(i<PersonCount)
{
printf("%s\t", Person[i].number);
printf("%s\t", Person[i].name);
printf("%s\t", Person[i].sex);
printf("%s\t", Person[i].birth);
printf("%s\t", Person[i].tel);
printf("%s\t", Person[i].address);
}
else
printf("未找到此人。\n");
break;
}
}
void savePersons(Person Person[])
{
FILE * fp = fopen("C:\\temp.txt","w");
int j;
if (fp==NULL)
{
printf("无法保存!\n\a");
return;
}
for(j = 0;j < PersonCount; j++)
{
fprintf(fp,"%s\t", Person[j].number);
fprintf(fp,"%s\t", Person[j].name);
fprintf(fp,"%s\t", Person[j].sex);
fprintf(fp,"%s\t", Person[j].birth);
fprintf(fp,"%s\t", Person[j].tel);
fprintf(fp,"%s\t", Person[j].address);
fprintf(fp, "\n");
}
fclose(fp);
printf("保存成功!\n");
}
void loadPersons(Person Person[])
{
int i = 0;
FILE * fp;
fp = fopen("C:\\temp.txt","r");
if (fp == NULL)
{
printf("文件读写错误! \n\a");
return;
}
for(i = 0;i < 50;i++)
{
fscanf(fp," %s",&Person[PersonCount].number);
fscanf(fp," %s",&Person[PersonCount].name);
fscanf(fp," %s",&Person[PersonCount].sex);
fscanf(fp," %s",&Person[PersonCount].birth);
fscanf(fp," %s",&Person[PersonCount].tel);
fscanf(fp," %s",&Person[PersonCount].address);
fscanf(fp, "\n");
PersonCount++;
if(feof(fp))
break;
}
printf("\n共加载了%d条信息\n",PersonCount);
fclose(fp);
printf("加载完成!\n");
}
void sortPersons(Person person[])
{
int i,j;
system("CLS");
for(i = 0; i < Person_COUNT-1; i++)
{
for(j =0; j < Person_COUNT-1-i; j++)
if(strcmp(person[j].name,person[j+1].name)>0)
{
Person temp;
temp = person[j];
person[j] = person[j+1];
person[j+1] = temp;
}
}
printf("排序成功!\n");
}