通信录

由于通信录代码模块较复杂,所以进行了分模块处理。总共分了三个模块,分别是头文件模块,函数模块,主函数模块。代码如下:

头文件模块:contact.h


#ifndef __CONTACT_H__

#define __CONTACT_H__


#define MAX_NAME   20

#define MAX_SEX 3

#define MAX_TELE 12

#define MAX_ADDR 20


#define MAX 1000

#include <stdio.h>


enum OP

{

EXIT,

ADD,

DEL,

SEARCH,

MODIFY,

SHOW,

CLR,

SORT

};


typedef struct Peo_Info

{

char name[MAX_NAME];

char sex[MAX_SEX];

int age;

char tele[MAX_TELE];

char addr[MAX_ADDR];

}Peo_Info;


typedef struct Dhb

{

Peo_Info pinfo[MAX];

int count;

}Dhb,*pDhb;


void menu();

void init_dhb(pDhb pdhb);

void add_dhb(pDhb pdhb);

void del_dhb(pDhb pdhb);

void search_dhb(pDhb pdhb);

void modify_dhb(pDhb pdhb);

void show_dhb(pDhb pdhb);

void clear_dhb(pDhb pdhb);

void sort_dhb(pDhb pdhb);


#endif //__CONTACT_H__


函数模块:contact.c


#define _CRT_SECURE_NO_WARNINGS 1


#include "contact.h"



void menu()

{

printf("\n");

printf("                    通讯录                     \n");

printf("********************************************************************\n");

printf("************** 1.add               2.del*********************\n");

printf("************** 3.search             4.modify ******************\n");

printf("************** 5.show              6.clear *******************\n");

printf("************** 7.sort              0.exit ********************\n");

printf("*********************************************************************\n");

}


static int find_entry(pDhb pdhb,const char* name)

{

int i = 0;

for (i = 0; i < pdhb->count; i++)

{

if (0 == strcmp(name, pdhb->pinfo[i].name))

{

return i;

}

}

return -1;

}


void init_dhb(pDhb pdhb)

{

pdhb->count = 0;

}


void add_dhb(pDhb pdhb)

{

if (pdhb->count >= MAX)

{

printf("电话本已满,无法添加\n");

return;

}

printf("名字:>");

scanf("%s", pdhb->pinfo[pdhb->count].name);

printf("性别:>");

scanf("%s", pdhb->pinfo[pdhb->count].sex);

printf("年龄:>");

scanf("%d", &pdhb->pinfo[pdhb->count].age);

printf("电话:>");

scanf("%s", pdhb->pinfo[pdhb->count].tele);

printf("住址:>");

scanf("%s", pdhb->pinfo[pdhb->count].addr);

pdhb->count++;

printf("添加成功\n");

}

void del_dhb(pDhb pdhb)

{

char name[MAX_NAME];

int ret = 0;

printf("请输入要删除的人的名字:>");

scanf("%s", name);

ret = find_entry(pdhb, name);

if (ret == -1)

{

printf("要删除的人不存在\n");

return;

}

else

{

int j = 0;

for (j = ret; j < pdhb->count-1; j++)

{

pdhb->pinfo[j] = pdhb->pinfo[j+1];

}

pdhb->count--;

printf("删除成功\n");

}

}

void search_dhb(pDhb pdhb)

{

char name[MAX_NAME];

int ret = 0;

printf("请输入要查找的人的名字:>");

scanf("%s", name);

ret = find_entry(pdhb, name);

if (ret == -1)

{

printf("要查找的人不存在\n");

return;

}

else

{

printf("%10s\t%5s\t%4s\t%10s\t%10s\n", "name", "sex", "age", "tele", "addr");

printf("%10s\t%5s\t%3d\t%10s\t%10s\n",

pdhb->pinfo[ret].name,

pdhb->pinfo[ret].sex,

pdhb->pinfo[ret].age,

pdhb->pinfo[ret].tele,

pdhb->pinfo[ret].addr);

}

}

void modify_dhb(pDhb pdhb)

{

char name[MAX_NAME];

int ret = 0;

printf("请输入要修改的人的名字:>");

scanf("%s", name);

ret = find_entry(pdhb, name);

if (ret == -1)

{

printf("要修改的人不存在\n");

return;

}

else

{

printf("名字:>");

scanf("%s", pdhb->pinfo[ret].name);

printf("性别:>");

scanf("%s", pdhb->pinfo[ret].sex);

printf("年龄:>");

scanf("%d", &pdhb->pinfo[ret].age);

printf("电话:>");

scanf("%s", pdhb->pinfo[ret].tele);

printf("住址:>");

scanf("%s", pdhb->pinfo[ret].addr);

}

}

void show_dhb(pDhb pdhb)

{

int i = 0;

printf("%10s\t%5s\t%4s\t%10s\t%10s\n", "name", "sex", "age", "tele", "addr");

for (i = 0; i < pdhb->count; i++)

{

printf("%10s\t%5s\t%3d\t%10s\t%10s\n",

pdhb->pinfo[i].name,

pdhb->pinfo[i].sex,

pdhb->pinfo[i].age,

pdhb->pinfo[i].tele,

pdhb->pinfo[i].addr);

}

}

void clear_dhb(pDhb pdhb)

{

pdhb->count = 0;

}

void sort_dhb(pDhb pdhb)

{

int i = 0;

int j = 0;

for (i = 0; i < pdhb->count - 1; i++)

{

for (j = 0; j < pdhb->count - 1 - i; j++)

{

if (strcmp(pdhb->pinfo[j].name, pdhb->pinfo[j + 1].name)>0)

{

Peo_Info tmp = pdhb->pinfo[j];

pdhb->pinfo[j] = pdhb->pinfo[j + 1];

pdhb->pinfo[j + 1] = tmp;

}

}

}

}


主函数模块:test.c


#define _CRT_SECURE_NO_WARNINGS 1

#include <stdlib.h>

#include "contact.h"


int main()

{

system("color d");

Dhb dhb;

int input = 1;

init_dhb(&dhb);

while (input)

{

menu();

printf("请选择>:");

scanf("%d", &input);

switch (input)

{

case ADD:

add_dhb(&dhb);

break;

case DEL:

del_dhb(&dhb);

break;

case SEARCH:

search_dhb(&dhb);

break;

case MODIFY:

modify_dhb(&dhb);

break;

case SHOW:

show_dhb(&dhb);

break;

case CLR:

clear_dhb(&dhb);

break;

case SORT:

sort_dhb(&dhb);

break;

case EXIT:

exit(EXIT_SUCCESS);

break;

}

}

return 0;

}


运行结果如下:


通信录_第1张图片

你可能感兴趣的:(通信,include,count)