一个定长版本的通讯录c语言实现

1.头文件contact.h

#ifndef __CONTACT_H__#define __CONTACT_H__#include <stdio.h>#include <string.h>#include <stdlib.h>#include <assert.h>#include <windows.h>#pragma warning(disable:4996)#define  SIZE 128#define TOTAL 1000  //通讯录总容量typedef struct person {char name[SIZE / 16];char sex;// f   or   m int age;char telphone[SIZE / 8];char address[SIZE / 4];}person_t;typedef struct contact {int size;  //当前通讯录有多少人int cap ;  //通讯录容量person_t person[TOTAL];}contact_t;void AddPerson(contact_t* ct);void DeletePerson(contact_t* ct);//index :-1 :show all//index>=0 : show index personvoid ShowContact(contact_t* ct);void SearchPerson(contact_t* ct);void ModPerson(contact_t* ct);void ClsContact(contact_t* ct);void SortContact(contact_t* ct);#endif

2.main.c

主要逻辑框架:

#include "contact.h"static void Menu(){printf("############################\n");printf("# 1. Add           2. Del  #\n");printf("# 3. Search        4. Mod  #\n");printf("# 5. Show          6. Cls  #\n");printf("# 7. Sort          0. Quit #\n");printf("############################\n");printf("Please Select:> ");}int main(){contact_t ct;memset(&ct, 0, sizeof(ct));ct.cap = TOTAL;int quit = 0;int select = 0;int findselect = 0;int select1 = 0;while (!quit) {Menu();scanf("%d", &select);switch (select) {case 1: //Add AddPerson(&ct);ShowContact(&ct, -1);break;case 2:DeletePerson(&ct);break;case 3:SearchPerson(&ct);break;case 4:ModPerson(&ct);//修改信息break;case 5:ShowContact(&ct,-1);break;case 6:ClsContact(&ct);//清空所有联系人信息break;case 7:以名字排序所有联系人SortContact(&ct);break;default:quit = 1;break;}}system("pause");return 0;}

3.contact.c

这部分为各个接口的具体实现:

#include"contact.h"static int IsFull(contact_t* ct) {if (TOTAL == ct->size ) {return 1;}return 0;}static int  Existname(contact_t* ct, char* name){assert(name);person_t* end = ct->person   ct->size;int index = 0;for (person_t* p = ct->person; p < end; p  , index  ) {if (0 == strcmp(p->name, name)) {//return p - ct->persons;return index;}}return -1;}static int  Existtel(contact_t* ct, char* telphone){assert(telphone);person_t* end = ct->person   ct->size;int index = 0;for (person_t* p = ct->person; p < end; p  , index  ) {if (0 == strcmp(p->telphone, telphone)) {//return p - ct->persons;return index;}}return -1;}void AddPerson(contact_t* ct) {assert(ct);if (IsFull(ct)) {printf("contact full!\n");return;}person_t* p = ct->person   ct->size;printf("姓名#  ");scanf(" %s", &p->name);if (Existname(ct, p->name) >= 0) {printf("%s hase exist!\n", ct->person->name);return;}printf("Please input telephole:\n");scanf("%s", &p->telphone);int flagsex = 1;printf("Please input sex (f or m):\n");while (flagsex) {char c = ' ';scanf(" %c", &c);if (c == 'f' || c == 'm' || c == 'F' ||c == 'M') {p->sex = c;flagsex = 0;break;}printf("Please input sex again (f or m):\n");}int flagage = 1;printf("Please input age:\n");while (flagage) {int a = 0;scanf(" %d", &a);if (a>=0 && a<=150) {p->age = a;flagage = 0;break;}printf("Please input truth age again!:\n");}printf("Please input adress:\n");scanf("%s", &p->address);ct->size  = 1;printf(" ...  add success !\n");}static void SearchPersonTelHelper(contact_t* ct){assert(ct);char search_key[SIZE / 4];printf("请输入你要查找人的电话号码# ");scanf("%s", search_key);int index = Existtel(ct, search_key);if (-1 == index) {printf("你要找的人[%s]不存在!\n", search_key);return;}else {ShowContact(ct, index);}}static void SearchPersonName(contact_t* ct){assert(ct);char search_key[SIZE / 4];printf("请输入你要查找人的name# ");scanf("%s", search_key);int index = Existname(ct, search_key);if (-1 == index) {printf("你要找的人[%s]不存在!\n", search_key);return;}else {ShowContact(ct, index);}}void SearchPerson(contact_t* ct){assert(ct);printf("###########################\n");printf("# 1. 按照姓名  2. 按照电话#\n");printf("###########################\n");printf("select:> ");int select = 0;scanf("%d", &select);switch (select) {case 1:SearchPersonName(ct);break;case 2:SearchPersonTelHelper(ct);break;}}static void DelatePersonName(contact_t* ct){char delname[SIZE / 4];printf("输入要删除的联系人姓名\n");scanf(" %s", &delname);int index = Existname(ct, delname);if (-1 == index) {printf("联系人 %s  不存在!\n", delname);return;}else {ct->person[index] = ct->person[(ct->size) - 1];ct->size--;}}static void DelatePersonTel(contact_t* ct) {char deltel[SIZE/8];printf("输入要删除的联系电话\n");scanf(" %s", &deltel);int index = Existtel(ct, deltel);if (-1 == index) {printf("联系人电话 %s  不存在!\n", deltel);return;}else {ct->person[index] = ct->person[(ct->size) - 1];ct->size--;}}void DeletePerson(contact_t* ct) {assert(ct);printf("###########################\n");printf("# 1. 按照姓名  2. 按照电话#\n");printf("###########################\n");printf("select:> ");int select = 0;scanf("%d", &select);switch (select) {case 1:DelatePersonName(ct);printf("delete succsee!");break;case 2:DelatePersonTel(ct);printf("delete succsee!");break;}}void ModPerson(contact_t* ct){char mname[SIZE/16];printf("输入你要修改的联系人姓名:\n");scanf("%s", &mname);int index = Existname(ct, mname);if (index == -1) {printf("用户不能存在\n");return;}else {printf("############################\n");printf("# 1.修改姓名  2.修改性别   #\n");printf("# 3.修改年龄  4.修改地址   #\n");printf("# 5.修改电话  6.退出       #\n");printf("############################\n");printf("Please Select:> ");int select = 0;scanf("%d", &select);person_t* p = ct->person   index;switch (select){case 1:printf("输入修改后的姓名\n");char name[SIZE / 16] = {' '};scanf("%s", &name);strcpy(p->name, name);///**p->name= name;*错误做法/break;case 2:printf("输入修改后的性别(f or m):\n");int flagsex = 1;while (flagsex) {char c = ' ';scanf(" %c", &c);if (c == 'f' || c == 'm' || c == 'F' || c == 'M') {p->sex = c;flagsex = 0;break;}printf("Please input sex again (f or m):\n");}break;case 3:printf("输入修改后的年龄\n");int flagage = 1;while (flagage) {int a = 0;scanf("%d", &a);if (a >= 0 && a <= 150) {p->age = a;flagage = 0;break;}printf("Please input truth age again!:\n");}break;case 4:printf("输入修改后的地址\n");char adress[SIZE/4];scanf("%s", &adress);strcpy(p->address, adress);break;case 5:printf("输入修改后的电话\n");char tel[SIZE/8];scanf("%s", &tel);strcpy(p->telphone, tel);break;case 6:break;default:break;}printf("mod success!\n");}}static int str_cmp(const void* x, const void* y) {person_t* x_p = (person_t*)x;person_t* y_p = (person_t*)y;return strcmp(x_p->name, y_p->name);}void SortContact(contact_t* ct){assert(ct);if (ct->size == 1) {return;}qsort(ct->person, sizeof(ct->size), sizeof(person_t), str_cmp);}void ShowContact(contact_t* ct, int index){assert(ct);if (-1 == index) {person_t* end = ct->person   ct->size;//姓名、性别、年龄、电话、住址printf("统计: %d/%d\n", ct->size, ct->cap);printf("----------------------------------------\n");for (person_t* p = ct->person; p < end; p  ) {printf("|%-8s|%-2c|%-3d|%-11s|%-10s|\n", p->name, p->sex, p->age, p->telphone, p->address);}printf("----------------------------------------\n");}else {person_t* p = ct->person   index;printf("|%-8s|%-2c|%-3d|%-11s|%-10s|\n", p->name, p->sex, p->age, p->telphone, p->address);}}void ClsContact(contact_t* ct){assert(ct);ct->size = 0;}

待改进的地方:1.这个是定长版本的,通讯录总容量有待修改为动态的可变长度的。
2.排序部分有点问题。(其他各部分功能均正常)
3.输入电话号码处应在加入一个判断是威威0-9的数字
这三点问题我将在下一个版本的通讯录中改进。

来源:https://www.icode9.com/content-4-794851.html

(0)

相关推荐