C#编写一个简易通讯录
一、需求分析
设计一个实用的通讯录小程序,由“姓名”、“电话号码”、“邮箱地址”、“QQ号码”组成。具有如下功能:
- 输入联系人信息,增加到通讯录中;
- 逐个显示通讯录中所有联系人的相关信息;
- 根据姓名进行查找,返回此联系人的详细信息;
- 删除指定姓名的联系人记录;
- 修改指定姓名的联系人信息;
- 按照姓名对联系人进行排序;
- 将联系人信息保存到文件中,以及从文件
中读取所有联系人。
二、概要设计
首先,定义各个标准库函数,建立结构体数组,定义所需函数。然后,分别建立七个功能对应的七个函数,这七个函数再通过主函数调用分别得以实现。之后,设置主函数,列出功能清单,供用户做出选择,通过执行多分支选择语句——switch语句,调用其它函数。
1.功能导图:
2.结构体数组为:
typedef struct {char name[20];char phone[20];char email[20];char qq[20];}ElemType;typedef struct {ElemType* elem;int length; //长度int listsize; //容量}SqList;
3.定义所需函数:
void AddContact(SqList &L);void DisplayContacts(SqList &L);void SearchContact(SqList &L);void DeleteContact(SqList &L);void ModifyContact(SqList &L);void SortContacts(SqList &L);void SaveContacts(SqList &L);
4.运用switch语句调用函数:
switch (cmd) {case '1':AddContact(contactList); break;case '2':DisplayContacts(contactList); break;case '3':SearchContact(contactList); break;case '4':DeleteContact(contactList); break;case '5':ModifyContact(contactList); break;case '6':SortContacts(contactList); break;case '7':SaveContacts(contactList); break;}
三、详细代码
#define LIST_INIT_SIZE 100 //初始容量#define LISTINCREMENT 50 //增量typedef struct {char name[20];char phone[20];char email[20];char qq[20];}ElemType;typedef struct {ElemType* elem;int length; //长度int listsize; //容量}SqList;void AddContact(SqList &L);void DisplayContacts(SqList &L);void SearchContact(SqList &L);void DeleteContact(SqList &L);void ModifyContact(SqList &L);void SortContacts(SqList &L);void SaveContacts(SqList &L);void PrintMenu() {printf("\n********************************************\n");printf("* *\n");printf("* 欢迎使用通讯录 v0.9 *\n");printf("* *\n");printf("* xxx制作 *\n");printf("* *\n");printf("* 1:增加联系人 *\n");printf("* 2:打印联系人 *\n");printf("* 3:查找联系人 *\n");printf("* 4:删除联系人 *\n");printf("* 5:修改联系人 *\n");printf("* 6:排序 *\n");printf("* 7:保存 *\n");printf("* 0:退出 *\n");printf("* *\n");printf("********************************************\n");}int main() {//1 + 回车//缓冲区里存2个字符char cmd;SqList contactList;InitList(contactList);do {system("CLS"); //清屏PrintMenu();printf("请输入选项:");scanf(" %c", &cmd); //" %c" %c前加空格,跳过缓冲区里可能的回车符getchar(); //吃掉“输入选项后缓冲区里的回车”switch (cmd) {case '1':AddContact(contactList); break;case '2':DisplayContacts(contactList); break;case '3':SearchContact(contactList); break;case '4':DeleteContact(contactList); break;case '5':ModifyContact(contactList); break;case '6':SortContacts(contactList); break;case '7':SaveContacts(contactList); break;}} while (cmd!='0');}void PrintContact(ElemType& e) {printf("%s %s %s %s\n", e.name, e.phone, e.email, e.qq);}void AddContact(SqList& L) {char cmd;do {ElemType e;printf("请输入联系人姓名:");scanf("%s", e.name);printf("请输入联系人电话号码:");scanf("%s", e.phone);printf("请输入联系人邮箱:");scanf("%s", e.email);printf("请输入联系人QQ:");scanf("%s", e.qq);ListInsert(L, ListLength(L) + 1, e);printf("请问您还要继续增加联系人吗?y|n");scanf(" %c", &cmd);//" %c" %c前加空格,跳过缓冲区里的回车符} while (cmd != 'n' && cmd != 'N');}void DisplayContacts(SqList& L) {ListTraverse(L, PrintContact);printf("按任意键继续...");getchar();}void SearchContact(SqList& L) {ElemType e;char x[20];char cmd;do {printf("请输入您想查找的联系人姓名:");scanf("%s", x);for (int i = 0; i < L.length; i++) {if (strcmp(L.elem[i].name, x) ==0){printf("name:%s phone:%s email:%s qq:%s\n", L.elem[i].name, L.elem[i].phone, L.elem[i].email, L.elem[i].qq);}else {printf("输入有误!\n");}}printf("请问您还要继续查找联系人吗?y|n");scanf(" %c", &cmd);//" %c" %c前加空格,跳过缓冲区里的回车符} while (cmd != 'n' && cmd != 'N');}void DeleteContact(SqList & L) {ElemType e;char x[20];char cmd;do {printf("请输入您想删除的联系人姓名:");scanf("%s", x);for (int i = 0; i < L.length; i++) {if (strcmp(L.elem[i].name , x)==0) {for (int j = i; j < L.length; j++) {strcpy(L.elem[j].name , L.elem[j + 1].name);strcpy(L.elem[j].phone ,L.elem[j + 1].phone);strcpy(L.elem[j].email , L.elem[j+1].email);strcpy(L.elem[j].qq , L.elem[j + 1].qq);--L.length;printf("操作成功\n");}}else {printf("输入有误!\n");}}printf("请问您还要继续删除联系人吗?y|n");scanf(" %c", &cmd);//" %c" %c前加空格,跳过缓冲区里的回车符} while (cmd != 'n' && cmd != 'N');}void ModifyContact(SqList& L){ElemType e;int j;char x[20];char cmd;do {printf("请输入您要修改信息的联系人姓名:");scanf("%s", x);for (int i = 0; i < L.length; i++) {if (strcmp(L.elem[i].name, x) == 0) {printf("联系人原信息为:name:%s phone:%s email:%s qq:%s\n", L.elem[i].name, L.elem[i].phone, L.elem[i].email, L.elem[i].qq);printf("请输入您想修改的选项:");printf("* 1:name *\n");printf("* 2:phone *\n");printf("* 3:email *\n");printf("* 4:qq *\n");scanf("%s", &j);switch (j) {case '1':scanf("%s", L.elem[i].name); break;case '2':scanf("%s", L.elem[i].phone); break;case '3':scanf("%s", L.elem[i].email); break;case '4':scanf("%s", L.elem[i].qq); break;}printf("操作成功\n");}else {printf("输入有误!\n");}}printf("请问您还要继续修改联系人吗?y|n");scanf(" %c", &cmd);//" %c" %c前加空格,跳过缓冲区里的回车符} while (cmd != 'n' && cmd != 'N');}void SortContacts(SqList& L) {ElemType e;char x1[20], x2[20], x3[20], x4[20];char cmd;printf("按联系人姓名进行排序");for (int i = 0; i < L.length; i++) {for (int j = 0; j < L.length - 1; j++) {if (strcmp(L.elem[j].name, L.elem[j + 1].name) > 0){strcpy(x1, L.elem[j].name);strcpy(x2, L.elem[j].phone);strcpy(x3, L.elem[j].email);strcpy(x4, L.elem[j].qq);strcpy(L.elem[j].name, L.elem[j + 1].name);strcpy(L.elem[j].phone, L.elem[j + 1].phone);strcpy(L.elem[j].email, L.elem[j + 1].email);strcpy(L.elem[j].qq, L.elem[j + 1].qq);strcpy(L.elem[j + 1].name, x1);strcpy(L.elem[j + 1].phone, x2);strcpy(L.elem[j + 1].email, x3);strcpy(L.elem[j + 1].qq, x4);}} printf("name:%s phone:%s email:%s qq:%s\n", L.elem[i].name, L.elem[i].phone, L.elem[i].email, L.elem[i].qq); }system("pause");}void SaveContacts(SqList & L) {FILE* fp;if ((fp = fopen("d:\\Users\\mljy\\Desktop\\ContactList\\ContactList.txt", "v")) == NULL){printf("文件打开失败!\n");exit(0);}for (int i = 0; i < L.length; i++) {fprintf(fp,"%s %s %s %s\n", L.elem[i].name, L.elem[i].phone, L.elem[i].email, L.elem[i].qq);printf("保存成功!\n");}if (fclose(fp)) {printf("操作错误!\n");exit(0);}getchar();}
四、调试分析
程序运行如下: