系统设计-设计思路
定义结构体来保存通讯录中联系人的各项信息
使用主菜单实现对通讯录主要功能的管理,不同功能对应着不同的编号
使用结构体数据来存储多个联系人
采用文件来保存联系人的数据,提高操作效率
系统的数据输入包括输入功能编号以及联系人的各项信息。数据输出主要指显示联系人的信息
系统设计-模块划分
主模块:该模块主要用来完成整个系统的流程,在主模块中调用其他函数,实现整个系统的功能。
新增联系人信息模块:该模块分别根据提示信息输入联系人的姓名、职业、电话、电子邮件、地址,输入结束后,自动将联系人信息保存在磁盘文件中。
该模块根据提示信息输入联系人信息并保存到磁盘文件中。
文件处理函数:fopen、feof、fread、fwrite、fclose。
字符串处理函数:strcmp。
查询联系人信息模块:该模块用于在通讯录中根据姓名查找指定的联系人,并根据系统提示决定是否显示该信息
根据姓名查找指定的联系人,并根据系统提示决定是否显示该信息。
修改联系人信息模块:该模块用来修改通讯录中指定联系人的信息。(根据姓名)
删除联系人信息模块:该模块将指定联系人的信息从通讯录中删除。(根据姓名)
排序模块:该模块将通讯录中的所有联系人按照姓名的字母顺序进行排序。
显示联系人信息模块:该模块用来显示通讯录中所有的联系人信息。
系统设计-数据结构设计
#include"stdio.h"
#include"stdlib.h"
#include"conio.h"
#include"dos.h"
#include"string.h"/*系统化输出*/
#define LEN sizeof(struct addritem)
#define FORMAT "%-10s%-10s%-15s%-25s%-30s%\n"
#define DATA addrinfo[i].name,addrinfo[i].occu,addrinfo[i].tel,addrinfo[i].email,addrinfo[i].address/*定义通讯录条目结构体*/
struct addritem
{char name[30]; /*联系人姓名*/char occu[30]; /*联系人职业*/char tel[15]; /*联系人电话*/char email[30]; /*联系人邮箱*/char address[30]; /*联系人地址*/
};/*结构体数组*/
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\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 record |\n");printf("\t\t| 6.display |\n");printf("\t\t-----------------------------------------------------|\n\n");printf("\t\t\tchoose(0-6):");
}
//增加联系人
void input() {int i, count = 0;char ch[2]; //记录用户输入FILE *fp; //文件指针变量if ((fp = fopen("data.txt", "a+")) == NULL) { //a+ 以附加的方式打开文件printf("can not open!\n");return;}while (!feof(fp)) { //feof 检测流上的结束符 eof 返回零值或非零值if (fread(&addrinfo[count], LEN, 1, fp) == 1)//第一个参数 指定用于接收数据的内存地址 //第二个参数 指定每个数据项的字节数 //第三个参数 指定每次读取数据项的个数//第四个参数 代表输入流count++;}fclose(fp);if (count == 0) {printf("No contact record!\n");}else {system("cls");display();}if ((fp = fopen("data.txt", "wb")) == NULL) { //wb 只写的方式printf("can not open address list\n");return;}for (i = 0; i < count; i++) {fwrite(&addrinfo[i], LEN, 1, fp);}printf("please input(y/n)");scanf("%c", ch);while (strcmp(ch, "Y") == 0 || strcmp(ch, "y") == 0) {printf("name:");scanf("%s", &addrinfo[count].name);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;}}printf("occupation:");scanf("%s", &addrinfo[count].occu);printf("telephone:");scanf("%s", &addrinfo[count].tel);printf("email:");scanf("%s", &addrinfo[count].email);printf("address:");scanf("%s", &addrinfo[count].address);if (fwrite(&addrinfo[count], LEN, 1, fp) != 1) {printf("can not save the record");}else {printf("%s saved!\n", addrinfo[count].name);count++;}fclose(fp);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", "rb")) == NULL) {printf("can not open!\n");return;}//读取联系人信息while (!feof(fp))if (fread(&addrinfo[count], LEN, 1, fp) == 1)count++;fclose(fp);if (count == 0) {print("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) {print("find the contact,display?(y/n)");scanf("%s", ch);if (strcmp(ch, "Y") == 0 || strcmp(ch, "y") == 0) {printf("name occupation telephone email address\t\n");printf(FORMAT, DATA);}break;}if (i == count) {printf("can not find the contact!\n");}}
}//修改通讯录
void update() {FILE *fp;int i, j, count = 0;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++;}}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("occupation:");scanf("%s", &addrinfo[i].occu);printf("telephone:");scanf("%s", &addrinfo[i].tel);printf("email:");scanf("%s", &addrinfo[i].email);printf("address:");scanf("%s", &addrinfo[i].address);}if (fp = fopen("data.txt", "wb") == NULL) {printf("can not ioen\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("can not find the contact!\n");
}//删除联系人
void del() {FILE *fp;int i, j, count = 0;char ch[15],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 of the contact which you want to delete\n");printf("delete name:");scanf("%s", name);for (i = 0; i < count; i++) {if (strcmp(name, addrinfo[i].name) == 0) {printf("find the contact! del(y/n)!\n");scanf("%s", ch);if (strcmp(ch, "Y") == 0 || strcmp(ch, "y") == 0) {for (j = i; j < count; j++) { //将i之后的纪录前移addrinfo[j] = addrinfo[j + 1];}}count--;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);printf("del sucessfully!\n");return;}printf("not find the contact!\n");}
}void sort() {FILE *fp;struct addritem t;int i = 0, j = 0, 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=0;j<count;j++)if (strcmp(addrinfo[i].name, addrinfo[i].name) > 0){t = addrinfo[i];addrinfo[i] = addrinfo[j];addrinfo[j] = t;}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);printf("save successfully");
}void display() {FILE *fp;int i = 0, 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");if (count == 0) {printf("no record to display!\n");return;}for(i=0;i<count;i++)printf(FORMAT, DATA);
}void 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);}
}