您的位置:首页 > 其它

通讯录结构体方法的实现 和VS中存在的一些问题的分析

2015-03-17 19:35 459 查看
实现一个通讯录;

通讯录可以用来存储1000个人的信息,每个人的信息包括:

姓名、性别、年龄、电话、住址

功能如下:

1. 添加联系人信息

2. 删除指定联系人信息

3. 查找指定联系人信息

4. 修改指定联系人信息

5. 显示所有联系人信息

6. 清空所有联系人

模块化设计:

头文件 结构体和相应函数的定义,声明

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <memory.h>

#define MAX 1000
#define NAME_LENGTH 20
#define SEX_LENGTH 5
#define AGE_LENGTH 3
#define TELE_LENGTH 20
#define ADDR_LENGTH 30

/*
结构体 用于储存通讯录人员信息
*/
struct ContactsUser
{
char name[NAME_LENGTH];
char sex[SEX_LENGTH];
/*
VS编译器下scanf_s对于长度有安全保护 因此采用字符数组保存年龄
*/
char age[AGE_LENGTH];
char tele[TELE_LENGTH];
char addr[ADDR_LENGTH];
};

/*
结构体 将上一个结构体装起来 同时创建变量记录人数
*/
struct Contacts
{
struct ContactsUser person[MAX];
int user_count;
};

typedef struct Contacts *pContacts;

int add_contacts(pContacts pcon);//添加函数
int dele_contacts(pContacts pcon);//删除函数
int clear_contacts(pContacts pcon);//清空函数
int find_contacts(pContacts pcon);//查找函数
int modify_contacts(pContacts pcon);//修改函数
void show_contacts(pContacts pcon);//显示函数
void menu();//主菜单</span>


#include "contacts.h"

/*
各个功能函数
*/

/*
菜单
*/
void menu()
{
printf("         Contacts              \n");
printf("\n");
printf("1.  Add the users_info \n");
printf("2.  Delete the users_info \n");
printf("3.  Clean all the users_info	\n");
printf("4.  Find the users_info \n");
printf("5.  Modify the users_info\n");
printf("6.  Show all the users_info\n");
printf("7.  exit\n");
printf("\n");

}

/*
查询实体函数  用于将输入的用户特征和储存进行比较(strcmp)
方便其他功能函数的调用
*/
int find_entry(pContacts pcon)
{
int i = 0;
char name[NAME_LENGTH];
printf("please input name:");
scanf_s("%s", name,NAME_LENGTH);
for (i = 0; i < pcon->user_count; i++)
{
if (strcmp(pcon->person[i].name, name) == 0) //输入和存储进行比较
{
return i;
}
}
return -1;
}

/*
增添函数
*/
int add_contacts(pContacts pcon)
{
if (pcon->user_count == MAX)
{
printf("Telephone book is full!\n");
return -1;
}
else
{
printf("Please input name:");
/*
scanf_s安全函数  应该添加控制长度的参数
*/
scanf_s("%s", pcon->person[pcon->user_count].name, NAME_LENGTH);
/*
数组从下标为0到下标为user_count-1 ,则在user_count处操作
*/
printf("Please input sex:");
scanf_s("%s", pcon->person[pcon->user_count].sex, SEX_LENGTH);
printf("Please input age:");
scanf_s("%s", pcon->person[pcon->user_count].age, AGE_LENGTH);
printf("Please input tele:");
scanf_s("%s", pcon->person[pcon->user_count].tele,TELE_LENGTH);
printf("Please input addr:");
scanf_s("%s", pcon->person[pcon->user_count].addr, ADDR_LENGTH);

pcon->user_count++;//添加结束 人员数目增加1
return 1;
}

}

/*
删除函数
*/
int dele_contacts(pContacts pcon)
{
int i = 0;
int ret = find_entry(pcon);//定义ret 接收find_entry的返回位置

if (ret != -1)
{
for (i = ret; i < pcon->user_count - 1; i++)
{
pcon->person[i] = pcon->person[i + 1];
}
pcon->user_count--;
return 1;
}
else
{
printf("not exist!\n");
return -1;
}
}

/*
清空函数
*/
int clear_contacts(pContacts pcon)
{
memset(pcon->person,0,MAX);
/*
memset函数 在一段内存中填充给定的值
是对较大结构体或数组清零的最快方法
*/
pcon->user_count = 0; //count 赋值0  进行清零

return 1;
}

/*
查找函数
*/
int find_contacts(pContacts pcon)
{
int ret = find_entry(pcon);//定义ret 接收find_entry的返回位置
if (ret != -1)
{
printf("name:%-5s", pcon->person[ret].name, NAME_LENGTH);
printf("sex:%-5s", pcon->person[ret].sex, SEX_LENGTH);
printf("age:%-5s", pcon->person[ret].age, AGE_LENGTH);
printf("tele:%-5s", pcon->person[ret].tele, TELE_LENGTH);
printf("addr:%-5s", pcon->person[ret].addr, ADDR_LENGTH);
return 1;
}
else
{
printf("not exist!\n");
return -1;
}
}

/*
修改函数
*/
int modify_contacts(pContacts pcon)
{
int ret = find_entry(pcon);//定义ret 接收find_entry的返回位置
if (ret != -1)
{
printf("Please input name:");
scanf_s("%s", pcon->person[ret].name, NAME_LENGTH);
printf("Please input sex:");
scanf_s("%s", pcon->person[ret].sex, SEX_LENGTH);
printf("Please input age:");
scanf_s("%s", pcon->person[ret].age, AGE_LENGTH);
printf("Please input tele:");
scanf_s("%s", pcon->person[ret].tele, TELE_LENGTH);
printf("Please input addr:");
scanf_s("%s", pcon->person[ret].addr, ADDR_LENGTH);
return 1;
}
else
{
printf("not exist!\n");
return -1;
}
}

/*
显示函数
*/
void show_contacts(pContacts pcon)
{
int i = 0;
printf("\tname\tsex\t\tage\t\ttele\t\t\taddr\n");
for (i = 0; i < pcon->user_count; i++)
{
printf("%10s\t", pcon->person[i].name);
printf("%5s\t", pcon->person[i].sex);
printf("%10s\t", pcon->person[i].age);
printf("%15s\t", pcon->person[i].tele);
printf("%20s\t", pcon->person[i].addr);
}
printf("\n");
}</span>


#include "contacts.h"

/*
主函数
*/
int main()
{

int input = 1;   //定义一个输入  初始化
struct Contacts user;
user.user_count = 0;//为user_count进行初始化

menu();

while (input)
{
printf("\n  enter you choice(0-7):\n");
scanf_s("%d", &input);
switch (input)  //switch-case 使用不同的功能函数
{
case 1:
add_contacts(&user);
break;
case 2:
dele_contacts(&user);
break;
case 3:
clear_contacts(&user);
break;
case 4:
find_contacts(&user);
break;
case 5:
modify_contacts(&user);
break;
case 6:
show_contacts(&user);
break;
case 7:
printf("Thanks for use!\n");
break;
default:
printf("input error!\n");
break;
}
}

return 0;
}


运行结果:



内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐