实现一个通讯录;

通讯录能够用来存储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;
}

执行结果:

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvSXJlYW5fTGF1/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="">

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvSXJlYW5fTGF1/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="">

通讯录结构体方法的实现 和VS中存在的一些问题的分析的更多相关文章

  1. C语言中的结构体和C++中的结构体以及C++中类的区别

    c++中结构体可以定义一个函数 C中的结构体和C++中结构体的不同之处:在C中的结构体只能自定义数据类型,结构体中不允许有函数,而C++中的结构体可以加入成员函数. C++中的结构体和类的异同: 一. ...

  2. C语言 结构体数组保存到二进制文件中

    在项目中我定义了一个结构体数组,头文件如下: C/C++ code   ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 ...

  3. FFmpeg 结构体学习(八):FFMPEG中重要结构体之间的关系

    FFMPEG中结构体很多.最关键的结构体可以分成以下几类: 解协议(http,rtsp,rtmp,mms) AVIOContext,URLProtocol,URLContext主要存储视音频使用的协议 ...

  4. struct结构体在c和c++中的差别

    非常多次遇到这个struct的问题,今天在这里简单总结一下我的理解 一.struct在C 中的使用 1.单独使用struct定义结构体类型 struct Student { int id; int n ...

  5. C语言的结构体,枚举类型在程序中的作用

    http://www.xue63.com/xueask-1221-12212854.html 结构和枚举类型从程序实现的角度来说,是用更接近自然语言的方式来表达数据.比如说实现2维空间的点,你可以使用 ...

  6. C语言结构体在内存中的存储情况探究------内存对齐

    条件(先看一下各个基本类型都占几个字节): void size_(){ printf("char类型:%d\n", sizeof(char)); printf("int类 ...

  7. Go语言中结构体的使用-第2部分OOP

    1 概述 结构体的基本语法请参见:Go语言中结构体的使用-第1部分结构体.结构体除了是一个复合数据之外,还用来做面向对象编程.Go 语言使用结构体和结构体成员来描述真实世界的实体和实体对应的各种属性. ...

  8. c++中的结构体:声明 定义 初始化

    什么是结构体? 之前的学习中我们知道了数组是一个容器,而且是存放固定大小数据的容器,而且存放的元素的数据类型必须要一致. 比如数据库中有这样的一条记录学号 性别 年龄 成绩 地址应该怎样存放 结构体: ...

  9. Linux2.6 内核中结构体初始化(转载)

    转自:http://hnniyan123.blog.chinaunix.net/uid-29917301-id-4989879.html 在Linux2.6版本的内核中,我们经常可以看到下面的结构体的 ...

随机推荐

  1. 同步IO与同步非阻塞IO的理解

    本文图片均来自网络 一.同步IO---Blocking IO 在Blocking IO模型中,用户空间的应用程序执行一个系统调用(recvform),这会导致应用程序阻塞,直到数据准备好,并且将数据从 ...

  2. PAT甲级1107. Social Clusters

    PAT甲级1107. Social Clusters 题意: 当在社交网络上注册时,您总是被要求指定您的爱好,以便找到一些具有相同兴趣的潜在朋友.一个"社会群体"是一群拥有一些共同 ...

  3. 关闭Spring Boot的Jsckson的FAIL_ON_EMPTY_BEANS

    说明:Spring Boot中默认使用了Jackson做JSON的解析. 解决方法: 1.通过注入Bean来实现 错误: org.springframework.http.converter.Http ...

  4. JSP中Out和Request对象详解

    内置表示不需要new便可直接使用. 一.基础知识 1.缓冲区:IO最原始是一个一个字节的读取,这就像吃米饭的时候一粒一粒的吃,很没有效率,这时候就有了碗,一碗一碗的吃,岂不痛快. 2.Get提交不能超 ...

  5. 【java失业择业中】失业第四天:准备面试

    1.jQuery基础 学好jquery的一个基础条件是学好css层叠样式,因为很多时候这2个是一块配合使用的. 页面中很多需要jquery实现的效果只是通过jquery的选择器,选中要操作的元素,添加 ...

  6. HttpClient post提交数据,汉字转码

    public static String post(String url, String data) throws ClientProtocolException, IOException { Htt ...

  7. Mysql -- 统计类用法

    累加: update push_online a,(select msg_key, push_countfrom push_online)b set a.push_count=b.push_count ...

  8. Informatica 常用组件Source Qualifier之二 默认查询

    2 默认查询 对于关系源,PowerCenter Server 将在运行会话时为每个源限定符转换生成查询.对于每个在映射中使用的源列,默认查询均为 SELECT 语句.也就是说,PowerCenter ...

  9. Unity 打包发布Android新手教学 (小白都能看懂的教学 ) [转]

    版权声明:本文为Aries原创文章,转载请标明出处.如有不足之处欢迎提出意见或建议,联系QQ531193915 扫码关注微信公众号,获取最新资源 最近在Unity的有些交流群里,发现好多Unity开发 ...

  10. 十个书写Node.js REST API的最佳实践(上)

    收录待用,修改转载已取得腾讯云授权 原文:10 Best Practices for Writing Node.js REST APIs 我们会通过本文介绍下书写Node.js REST API的最佳 ...