学习了结构体之后,我用结构体实现了一个简易版通讯录。
题目:
实现一个通讯录;
通讯录可以用来存储1000个人的信息,每个人的信息包括:
姓名、性别、年龄、电话、住址
可以实现的功能有:增删改查以及排序。
下面是通讯录的源程序:
头文件中是一些函数的声明以及宏定义
#ifndef __TELE_H__
#define __TELE_H__
#include "tele.h"
#define MAX_NAME 20
#define MAX_SEX 5
#define MAX_TEL 12
#define MAX_ADD 50
#define MAX 1000
//通讯录可以用来存储1000个人的信息,每个人的信息包括:
//姓名、性别、年龄、电话、住址
enum CHIOCE
{
EXIT,
ADD,
DEL,
SEARCH,
MODIFY,
SHOW,
SORT
};
typedef struct Peoinfo
{
char name[MAX_NAME];
char sex[MAX_SEX];
int age;
char tel[MAX_TEL];
char address[MAX_ADD];
}Peoinfo;
typedef struct Contact
{
Peoinfo peop[MAX];
int count;
}Contact,*pContact;//pContact是指针类型名
void AddCon(pContact pc);
void InitCont(pContact pc);
void ShowCon(pContact pc);
void Search(pContact pc);
void DelCon(pContact pc);
void Modify(pContact pc);
void Sort(pContact pc);
#endif
接下来是测试函数test.c
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<stdlib.h>
#include "tele.h"
void menu()
{
printf("************************************\n");
printf("****** 0.exit ******\n");
printf("****** 1.add 2.del ******\n");
printf("****** 3.search 4.modify ******\n");
printf("****** 5.show 6.sort ******\n");
printf("************************************\n");
}
int main()
{
//Peoinfo contact[MAX] = { 0 };//联系人最大
//int count = 0;//联系人个数,放入结构体
//姓名、性别、年龄、电话、住址
Contact my_con;
InitCont(&my_con);
int input = 0;
do
{
menu();
printf("请输入操作> ");
scanf("%d", &input);
switch (input)
{
case ADD:
AddCon(&my_con);
break;
case DEL:
DelCon(&my_con);
break;
case SEARCH:
Search(&my_con);
break;
case MODIFY:
Modify(&my_con);
break;
case SHOW:
ShowCon(&my_con);
break;
case SORT:
Sort(&my_con);
break;
default:
printf("输入有误,请重新输入\n");
break;
}
} while (input);
system("pause");
return 0;
}
最后是源程序tele.c实现功能的函数:
#define _CRT_SECURE_NO_WARNINGS 1
#include "tele.h"
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
void InitCont(pContact pc)//初始化信息
{
pc->count = 0;
memset(pc->peop, 0, sizeof(pc->peop));
}
void AddCon(pContact pc)//添加联系人函数
{
if (pc->count == MAX)
{
printf("通讯录已满\n");
return;
}
printf("请输入名字:");
scanf("%s", pc->peop[pc->count].name);
printf("请输入性别:");
scanf("%s", pc->peop[pc->count].sex);
printf("请输入年龄:");
scanf("%d", &(pc->peop[pc->count].age));
printf("请输入电话:");
scanf("%s", pc->peop[pc->count].tel);
printf("请输入住址:");
scanf("%s", pc->peop[pc->count].address);
(pc->count)++;
}
int find(pContact pc)
{
int i = 0;
char name[MAX_NAME];
assert(pc);
printf("请输入要操作的人的姓名:");
scanf("%s", name);
for (i = 0; i < pc->count; i++)
{
if (strcmp(pc->peop[i].name ,name) == 0)
return i;
}
return -1;
}
void DelCon(pContact pc)
{
int i = 0;// 1 /2 3
int ret = find(pc);
if (ret == -1)
{
printf("此人不在通讯录中\n");
}
else
{
for (i = ret; i < pc->count; i++)
{
pc->peop[i] = pc->peop[i + 1];
}
pc->count--;
printf("删除成功\n");
}
}
void ShowCon(pContact pc)
{
int i = 0;
if (pc->count == 0)
{
printf("通讯录为空\n");
return;
}
for (i = 0; i < pc->count; i++)
{
printf("%s %s %d %s %s\n", pc->peop[i].name, pc->peop[i].sex, pc->peop[i].age,
pc->peop[i].tel, pc->peop[i].address);
}
printf("\n");
}
void Search(pContact pc)
{
int i = 0;
if (pc->count == 0)
{
printf("通讯录为空\n");
return;
}
int ret = find(pc);
if (ret == -1)
{
printf("查无此人\n");
return;
}
else
{
printf("找到了\n");
printf("%s %s %d %s %s\n", pc->peop[ret].name, pc->peop[ret].sex, pc->peop[ret].age,
pc->peop[ret].tel, pc->peop[ret].address);
}
}
void Modify(pContact pc)//修改联系人函数
{
int i = 0;
int ret = find(pc);
if (ret != -1)
{
printf("请重新输入%s的信息:姓名 性别 年龄 电话 地址\n", pc->peop[ret].name);
scanf("%s%s%d%s%s", pc->peop[ret].name, pc->peop[ret].sex, &(pc->peop[ret].age),
pc->peop[ret].tel, pc->peop[ret].address);
printf("修改成功\n");
}
else
{
printf("查无此人\n");
}
}
void sort_menu()
{
printf("请输入排序方式\n");
printf("- - - - - - 1.姓名- - - - - - \n");
printf("- - - - - - 2.性别- - - - - - \n");
printf("- - - - - - 3.年龄- - - - - - \n");
}
void sort_name(pContact pc)
{
int i = 0;
int j = 0;
if (pc->count == 0)
{
printf("通讯录为空\n");
return;
}
for (i = 0; i < pc->count - 1; i++)//0 1 2
{
for (j = 0; j < pc->count - i - 1; j++)
{
if (strcmp(pc->peop[j].name, pc->peop[j + 1].name)>0)//按升序排序
{
Peoinfo tmp = pc->peop[j];
pc->peop[j] = pc->peop[j + 1];
pc->peop[j + 1] = tmp;
}
}
}
for (i = 0; i < pc->count; i++)
{
printf("%s %s %d %s %s\n", pc->peop[i].name, pc->peop[i].sex, pc->peop[i].age,
pc->peop[i].tel, pc->peop[i].address);
}
}
void sort_sex(pContact pc)
{
int i = 0;
for (i = 0; i < pc->count; i++)
{
char sex[] = "male";
if (strcmp(pc->peop[i].sex, sex) != 0)
{
if (strcmp(pc->peop[i + 1].sex, sex) == 0)//按升序排序
{
Peoinfo tmp = pc->peop[i];
pc->peop[i] = pc->peop[i + 1];
pc->peop[i + 1] = tmp;
}
}
}
for (i = 0; i < pc->count; i++)
{
printf("%s %s %d %s %s\n", pc->peop[i].name, pc->peop[i].sex, pc->peop[i].age,
pc->peop[i].tel, pc->peop[i].address);
}
}
void sort_age(pContact pc)
{
int i = 0;
int j = 0;
if (pc->count == 0)
{
printf("通讯录为空\n");
return;
}
for (i = 0; i < pc->count - 1; i++)//0 1 2
{
for (j = 0; j < pc->count - i - 1; j++)
{
if (pc->peop[j].age > pc->peop[j + 1].age)//按升序排序
{
Peoinfo tmp = pc->peop[j];
pc->peop[j] = pc->peop[j + 1];
pc->peop[j + 1] = tmp;
}
}
}
for (i = 0; i < pc->count; i++)
{
printf("%s %s %d %s %s\n", pc->peop[i].name, pc->peop[i].sex, pc->peop[i].age,
pc->peop[i].tel, pc->peop[i].address);
}
}
void Sort(pContact pc)
{
int num = 0;
sort_menu();
printf("请选择>");
scanf("%d", &num);
switch(num)
{
case 1:
sort_name(pc);
break;
case 2:
sort_sex(pc);
break;
case 3:
sort_age(pc);
break;
default:
printf("无效输入\n");
break;
}
}
这个简易版的通讯录有一个缺点,那就是最多可以存放1000个人的信息,当通讯录满了的时候,就没法再往里存放了,而当人数较少时又会浪费空间,这时就出现问题了。怎么解决呢?
可以想一下动态开辟存放信息的空间,下边是实现过程:
首先是头文件声明:
#ifndef __TELE__
#define __TELE__
#include "tele.h"
#define MAX_NAME 20
#define MAX_SEX 5
#define MAX_TEL 12
#define MAX_ADD 50
#define MAX 1000
#define DEFAULT_SZ 2
//通讯录可以用来存储1000个人的信息,每个人的信息包括:
//姓名、性别、年龄、电话、住址
enum CHIOCE
{
EXIT,
ADD,
DEL,
SEARCH,
MODIFY,
SHOW,
SORT
};
typedef struct Peoinfo
{
char name[MAX_NAME];
char sex[MAX_SEX];
int age;
char tel[MAX_TEL];
char address[MAX_ADD];
}Peoinfo;
typedef struct Contact
{
Peoinfo *peop;
int count;
int capacity;
}Contact, *pContact;//pContact是指针类型名
void InitCont(pContact pc);
void AddCon(pContact pc);
void ShowCon(pContact pc);
void Search(pContact pc);
void DelCon(pContact pc);
void Modify(pContact pc);
void Sort(pContact pc);
void Destory(pContact pc);
#endif
测试部分test.c
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<stdlib.h>
#include "tele.h"
void menu()
{
printf("************************************\n");
printf("****** 0.exit ******\n");
printf("****** 1.add 2.del ******\n");
printf("****** 3.search 4.modify ******\n");
printf("****** 5.show 6.sort ******\n");
printf("************************************\n");
}
int main()
{
//Peoinfo contact[MAX] = { 0 };//联系人最大
//int count = 0;//联系人个数,放入结构体
//姓名、性别、年龄、电话、住址
Contact my_con;
InitCont(&my_con);
int input = 0;
do
{
menu();
printf("请输入操作> ");
scanf("%d", &input);
switch (input)
{
case ADD:
AddCon(&my_con);
break;
case DEL:
DelCon(&my_con);
break;
case SEARCH:
Search(&my_con);
break;
case MODIFY:
Modify(&my_con);
break;
case SHOW:
ShowCon(&my_con);
break;
case SORT:
Sort(&my_con);
break;
case EXIT:
Destory(&my_con);
break;
default:
printf("输入有误,请重新输入\n");
break;
}
} while (input);
system("pause");
return 0;
}
最后是函数实现:
#define _CRT_SECURE_NO_WARNINGS 1
#include "tele.h"
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <windows.h>
void InitCont(pContact pc)//初始化信息
{
pc->count = 0;
pc->capacity = DEFAULT_SZ;
pc->peop = (Peoinfo*)calloc(sizeof(Peoinfo), pc->capacity);
}
void check_capc(pContact pc)
{
Peoinfo *p=NULL;
if (pc->count == pc->capacity)
{
printf("容量不够,增容中......\n");
p = (Peoinfo*)realloc(pc->peop, ((pc->capacity + 2)*sizeof(Peoinfo)));
pc->capacity += 2;
Sleep(3);
printf("增加容量成功\n");
}
if (p != NULL)
{
pc->peop = p;
p = NULL;
}
}
void AddCon(pContact pc)
{
check_capc(pc);
printf("请输入名字:");
scanf("%s", pc->peop[pc->count].name);
printf("请输入性别:");
scanf("%s", pc->peop[pc->count].sex);
printf("请输入年龄:");
scanf("%d", &(pc->peop[pc->count].age));
printf("请输入电话:");
scanf("%s", pc->peop[pc->count].tel);
printf("请输入住址:");
scanf("%s", pc->peop[pc->count].address);
pc->count++;
}
int find(pContact pc)
{
int i = 0;
char name[MAX_NAME];
assert(pc);
printf("请输入要操作的人的姓名:");
scanf("%s", name);
for (i = 0; i < pc->count; i++)
{
if (strcmp(pc->peop[i].name, name) == 0)
return i;
}
return -1;
}
void DelCon(pContact pc)
{
int i = 0;// 1 /2 3
int ret = find(pc);
if (ret == -1)
{
printf("此人不在通讯录中\n");
}
else
{
for (i = ret; i < pc->count; i++)
{
pc->peop[i] = pc->peop[i + 1];
}
pc->count--;
printf("删除成功\n");
}
}
void Modify(pContact pc)//修改联系人函数
{
int i = 0;
int ret = find(pc);
if (ret != -1)
{
printf("请重新输入 %s 的信息:\n姓名 性别 年龄 电话 地址\n", pc->peop[ret].name);
scanf("%s%s%d%s%s", pc->peop[ret].name, pc->peop[ret].sex, &(pc->peop[ret].age),
pc->peop[ret].tel, pc->peop[ret].address);
printf("修改成功\n");
}
else
{
printf("查无此人\n");
}
}
void Search(pContact pc)
{
int i = 0;
if (pc->count == 0)
{
printf("通讯录为空\n");
return;
}
int ret = find(pc);
if (ret == -1)
{
printf("查无此人\n");
return;
}
else
{
printf("找到了\n");
printf("%-10s %6s %4d %15s %10s\n", pc->peop[ret].name, pc->peop[ret].sex, pc->peop[ret].age,
pc->peop[ret].tel, pc->peop[ret].address);
}
}
void ShowCon(pContact pc)
{
int i = 0;
if (pc->count == 0)
{
printf("通讯录为空\n");
return;
}
else
{
printf("%-10s %6s %4s %10s %10s\n", "姓名", "性别", "年龄", "电话", "地址");
for (i = 0; i < pc->count; i++)
{
printf("%-10s %6s %4d %10s %10s\n", pc->peop[i].name, pc->peop[i].sex, pc->peop[i].age,
pc->peop[i].tel, pc->peop[i].address);
}
}
printf("\n");
}
void sort_menu()
{
printf("请输入排序方式\n");
printf("- - - - - - 1.姓名- - - - - - \n");
printf("- - - - - - 2.性别- - - - - - \n");
printf("- - - - - - 3.年龄- - - - - - \n");
}
void sort_name(pContact pc)
{
int i = 0;
int j = 0;
if (pc->count == 0)
{
printf("通讯录为空\n");
return;
}
for (i = 0; i < pc->count - 1; i++)//0 1 2
{
for (j = 0; j < pc->count - i - 1; j++)
{
if (strcmp(pc->peop[j].name, pc->peop[j + 1].name)>0)//按升序排序
{
Peoinfo tmp = pc->peop[j];
pc->peop[j] = pc->peop[j + 1];
pc->peop[j + 1] = tmp;
}
}
}
for (i = 0; i < pc->count; i++)
{
printf("%s %s %d %s %s\n", pc->peop[i].name, pc->peop[i].sex, pc->peop[i].age,
pc->peop[i].tel, pc->peop[i].address);
}
}
void sort_sex(pContact pc)
{
int i = 0;
for (i = 0; i < pc->count; i++)
{
char sex[] = "male";
if (strcmp(pc->peop[i].sex, sex) != 0)
{
if (strcmp(pc->peop[i + 1].sex, sex) == 0)//按升序排序
{
Peoinfo tmp = pc->peop[i];
pc->peop[i] = pc->peop[i + 1];
pc->peop[i + 1] = tmp;
}
}
}
for (i = 0; i < pc->count; i++)
{
printf("%s %s %d %s %s\n", pc->peop[i].name, pc->peop[i].sex, pc->peop[i].age,
pc->peop[i].tel, pc->peop[i].address);
}
}
void sort_age(pContact pc)
{
int i = 0;
int j = 0;
if (pc->count == 0)
{
printf("通讯录为空\n");
return;
}
for (i = 0; i < pc->count - 1; i++)//0 1 2
{
for (j = 0; j < pc->count - i - 1; j++)
{
if (pc->peop[j].age > pc->peop[j + 1].age)//按升序排序
{
Peoinfo tmp = pc->peop[j];
pc->peop[j] = pc->peop[j + 1];
pc->peop[j + 1] = tmp;
}
}
}
for (i = 0; i < pc->count; i++)
{
printf("%s %s %d %s %s\n", pc->peop[i].name, pc->peop[i].sex, pc->peop[i].age,
pc->peop[i].tel, pc->peop[i].address);
}
}
void Sort(pContact pc)
{
int num = 0;
sort_menu();
printf("请选择>");
scanf("%d", &num);
switch (num)
{
case 1:
sort_name(pc);
break;
case 2:
sort_sex(pc);
break;
case 3:
sort_age(pc);
break;
default:
printf("无效输入\n");
break;
}
}
void Destory(pContact pc)
{
free(pc->peop);
pc->peop = NULL;
pc->count = 0;
pc->capacity = 0;
printf("销毁成功\n");
}
这是最最简易版的通讯录,之后会有更加简洁的方法用链表实现增删改查。