#include <stdio.h>
#include <string.h>
#include <cstdlib>
#define N 108
typedef struct telephone
{
char name[25];
char num[25];
char tel[25];
} TEL;
void DisplayMenu(); //显示主菜单
void save(TEL a[], int n);
void CreateList(TEL a[], int n) //1.创建通讯录
{
int i;
FILE *fp;
for (i = 0; i < n; i++)
scanf("%s%s%s", a[i].name, a[i].num, a[i].tel);
save(a, n);
}
int AddList(TEL a[], int n); //2.添加学生信息
void InquireListName(TEL a[], int n); //3.按姓名查询通讯录
void InquireListNum(TEL a[], int n); //3.按学号查询通讯录
void ChangeListName(TEL a[], int n); //4.按姓名修改通讯录
int DelListName(TEL a[], int n); //5.按姓名删除通讯录
int DelListNum(TEL a[], int n); //6.按学号删除通讯录
void DisplayList(TEL a[], int n); //7.显示通讯录
int main()
{
struct telephone a[N];
int n,p=0;
scanf("%d", &n);
DisplayMenu(); //显示主菜单
while (1)
{
int m;
int name[25];
scanf("%d", &m);
//if( select=='l' )
// break;
switch (m)
{
case 1:
CreateList(a,n); //1.创建通讯录
break;
case 2:
if (AddList(a, n)) //2.添加学生信息
n++;
save(a, n);
break;
case 3:
printf("1.按姓名查询 2.按学号查询\n");
scanf("%d",p);
if(p==1){InquireListName(a, n);} //3.按姓名查询通讯录
else {InquireListNum(a, n);} //3.按学号查询通讯录
break;
case 4:
ChangeListName(a, n); //4.按姓名修改通讯录
break;
case 5:
if (DelListName(a, n)) //5.按姓名删除通讯录
n--;
break;
case 6:
if (DelListNum(a, n)) //6.按学号删除通讯录
n--;
break;
case 7:
DisplayList(a, n); //7.显示通讯录
break;
case 8:
exit(0); //8.退出系统
break;
}
}
return 0;
}
void save(TEL a[], int n)
{
int i;
FILE *fp;
fp = fopen("stu.dat", "w");
fprintf(fp, "%d\n", n);
for (i = 0; i < n; i++)
fprintf(fp, "%s\t%s\t%s\n", a[i].name, a[i].num, a[i].tel);
}
void DisplayMenu() //1.显示主菜单
{
printf(" 学生通讯录管理系统\n\n");
printf("*************************系统功能菜单**************************\n");
printf(" -------------------------------------------------------------\n");
printf(" 1.创建通讯录 2.添加学生信息 \n");
printf("************************************************************\n");
printf(" 3.查询通讯录 4.按姓名修改通讯录 \n");
printf("************************************************************\n");
printf(" 5.按姓名删除通讯录 6.按学号删除通讯录\n");
printf("************************************************************\n");
printf(" 7.显示通讯录 8.退出系统\n");
printf("************************************************************\n");
printf(" -------------------------------------------------------------\n");
printf("***************************************************************\n\n");
printf("请选择您需要的操作:\n");
}
void DisplayList(TEL a[], int n) //2.显示通讯录
{
int i;
printf("显示通讯录:\n");
for (i = 0; i < n; i++)
printf("%s\t%s\t%s\n", a[i].name, a[i].num, a[i].tel);
printf("\n");
}
void InquireListName(TEL a[], int n) //3.按姓名查询通讯录
{
int i, flag;
char SearchName[25];
printf("按姓名查询通讯录:\n");
scanf("%s", SearchName);
for (i = 0, flag = 0; i < n; i++)
{
if (strcmp(SearchName, a[i].name) == 0)
{
printf("%s\t", a[i].name);
printf("%s\t", a[i].num);
printf("%s\n", a[i].tel);
flag = 1;
break;
}
}
if (flag == 0)
{
printf("未查询到该姓名信息!\n");
}
printf("\n");
}
void InquireListNum(TEL a[], int n) //3.按学号查询通讯录
{
int i, flag;
char SearchID[25];
printf("按学号查询通讯录:\n");
scanf("%s", SearchID);
for (i = 0, flag = 0; i < n; i++)
{
if (strcmp(SearchID, a[i].num) == 0)
{
printf("%s\t", a[i].name);
printf("%s\t", a[i].num);
printf("%s\n", a[i].tel);
flag = 1;
break;
}
}
if (flag == 0)
{
printf("未查询到该学号信息!\n");
}
printf("\n");
}
void ChangeListName(TEL a[], int n) //4.按姓名修改通讯录
{
int i, flag;
char SearchName[25];
printf("按姓名修改通讯录:\n");
scanf("%s", SearchName);
for (i = 0, flag = 0; i < n; i++)
{
if (strcmp(SearchName, a[i].name) == 0)
{
strcpy(a[i].name, SearchName);
printf("请输入要修改的学号:");
scanf("%s", a[i].num);
printf("请输入要修改的电话号码:");
scanf("%s", a[i].tel);
//strcpy(p[i].name,ChangeName);
//strcpy(p[i].name,ChangeTel);
save(a, n);
printf("信息修改完毕!\n");
printf("%s\t%s\t%s\n", a[i].name, a[i].num, a[i].tel);
flag = 1;
break;
}
}
if (flag == 0)
printf("未找到该姓名信息!");
printf("\n");
}
int DelListName(TEL a[], int n) //5.按姓名删除通讯录
{
int i, j;
char SearchName[25];
printf("按姓名删除通讯录:\n");
scanf("%s", SearchName);
for (i = 0; i < n; i++)
{
if (strcmp(a[i].name, SearchName) == 0)
break;
}
if (i >= n)
{
printf("未查询到该姓名信息!按姓名删除通讯录失败!\n");
return 0;
}
else
{
for (j = i; j < n - 1; j++)
a[j] = a[j + 1];
save(a, n);
printf("按姓名删除通讯录成功!\n");
return 1;
}
}
int DelListNum(TEL a[], int n) //6.按学号删除通讯录
{
int i, j, flag;
char SearchNum[25];
printf("按学号删除通讯录:\n");
scanf("%s", SearchNum);
for (i = 0; i < n; i++)
{
if (strcmp(a[i].num, SearchNum) == 0)
break;
}
if (i >= n)
{
printf("未查询到该学号信息!按学号删除通讯录失败!\n");
return 0;
}
else
{
for (j = i; j < n - 1; j++)
a[j] = a[j + 1];
printf("按学号删除通讯录成功!\n");
save(a, n);
return 1;
}
printf("\n");
}
int AddList(TEL a[], int n) //7.添加学生信息
{
int i, flag;
char AddNum[25];
printf("添加通讯录:\n");
printf("请输入要添加的学号:");
scanf("%s", AddNum);
for (i = 0, flag = 0; i < n; i++)
{
if (strcmp(AddNum, a[i].num) == 0)
{
flag = 1;
printf("学号重复!\n");
return 0;
}
}
if (flag == 0)
{
//n++;
printf("学号不重复!请添加信息:\n");
printf("请输入要添加的名字:");
scanf("%s", a[n].name);
strcpy(a[n].num, AddNum);
printf("请输入要添加的电话号码:");
scanf("%s", a[n].tel);
//save(a,n);
printf("添加信息成功!\n");
return 1;
//printf("%d",n);
}
printf("\n");
}