实现一个通讯录;
通讯录可以用来存储1000个人的信息,每个人的信息包括:
姓名、性别、年龄、电话、住址
提供方法:
1. 添加联系人信息
2. 删除指定联系人信息
3. 查找指定联系人信息
4. 修改指定联系人信息
5. 显示所有联系人信息
6. 清空所有联系人
7. 以名字排序所有联系人
经过多次调试,代码初成:
#ifndef CONTACT_H
#define CONTACT_H
#endif
#include<stdio.h>
#include<windows.h>
#include<stdlib.h>
#pragma warning(disable:4996)
#define NAME_MUN 20
#define SEX_NUM 5
#define TEL_NUM 11
#define ADDR_NUM 30
#define MAX 1000
struct tellphone
{
char name[NAME_MUN];
char sex[SEX_NUM];
int age;
char tel[TEL_NUM];
char addr[ADDR_NUM];
};
typedef struct contact
{
struct tellphone people[MAX];
int count;
}ps;
void add_contact(ps* pcon);
void del_contact(ps* pcon);
int search_contact(ps* pcon);
void change_contact(ps* pcon);
void show_contact(ps* pcon);
void inti_contact(ps* pcon);
void sort_contact(ps* pcon);
#include"contact.h"
void add_contact(ps* pcon)//增加
{
if (pcon->count == MAX)
{
printf("通讯录满了\n");
return ;
}
else
{
printf("输入姓名:");
scanf("%s", pcon->people[pcon->count].name);
printf("输入性别:");
scanf("%s", pcon->people[pcon->count].sex);
printf("输入年龄:");
scanf("%d", &pcon->people[pcon->count].age);
printf("输入电话:");
scanf("%s", pcon->people[pcon->count].tel);
printf("输入地址:");
scanf("%s", pcon->people[pcon->count].addr);
pcon->count++;
}
return 0;
}
void del_contact(ps* pcon)//删除
{
int i;
int ret = search_contact(pcon);
if (ret != -1)
{
for (i = ret;i < pcon->count-1;i++)
{
pcon->people[i] = pcon->people[i + 1];
}
pcon->count--;
}
else
{
printf("没有");
return -1;
}
}
int search_contact(ps* pcon)//寻找
{
int i;
char name[20];
printf("请输入名字:");
scanf("%s", name);
for (i = 0;i < pcon->count;i++)
{
if (strcmp(pcon->people[i].name, name) == 0)
{
printf("name sex age tel addr\n");
printf("%s ", pcon->people[i].name);
printf("%d ", pcon->people[i].age);
printf("%s ", pcon->people[i].tel);
printf("%s ", pcon->people[i].addr);
printf("\n");
return i;
}
}
printf("没有");
return -1;
}
void change_contact(ps* pcon)//改名
{
int ret = search_contact(pcon);
if (ret != -1)
{
printf("输入姓名:");
scanf("%s", pcon->people[ret].name);
printf("输入性别:");
scanf("%s", pcon->people[ret].sex);
printf("输入年龄:");
scanf("%d", &pcon->people[ret].age);
printf("输入电话:");
scanf("%s", pcon->people[ret].tel);
printf("输入地址:");
scanf("%s", pcon->people[ret].addr);
return;
}
printf("输入错误");
return -1;
}void show_contact(ps* pcon)
//显示
{
int i;
printf("name sex age tel addr\n");
for (i = 0;i < pcon->count;i++)
{
printf("%s ", pcon->people[i].name);
printf("%s ", pcon->people[i].sex);
printf("%d ", pcon->people[i].age);
printf("%s ", pcon->people[i].tel);
printf("%s ", pcon->people[i].addr);
printf("\n");
}
return;
}
void inti_contact(ps* pcon)//初始化
{
pcon->count = 0;
}
void sort_contact(ps* pcon)//排序
{
int i;
int j;
for (i = 0;i < pcon->count - 1;i++)
{
for (j = 0;j < pcon->count - 1 - i;j++)
{
if (strcmp(pcon->people[j].name, pcon->people[j + 1].name) > 0)
{
char temp[20];
strcpy(temp, pcon->people[j].name);
strcpy(pcon->people[j].name, pcon->people[j+1].name);
strcpy(pcon->people[j+1].name,temp);
}
}
}
show_contact(pcon);
}
#include"contact.h"
void menu()
{
printf("#######################\n");
printf("##1.add 2.del##\n");
printf("##3.search 4.change##\n");
printf("##5.show 6.inti##\n");
printf("##7.sort 8.exit##\n");
printf("#######################\n");
}
int main()
{
int n;
struct contact people;
people.count = 0;
while (1)
{
menu();
printf("please input one number:");
scanf("%d", &n);
switch (n)
{
case 1:
add_contact(&people);
break;
case 2:
del_contact(&people);
break;
case 3:
search_contact(&people);
break;
case 4:
change_contact(&people);
break;
case 5:
show_contact(&people);
break;
case 6:
inti_contact(&people);
break;
case 7:
sort_contact(&people);
break;
case 8:
exit(0);
default:
printf("input error\n");
break;
}
}
system("pause");
return 0;
}
结果如下:
菜鸟一枚,能力有限,反复演示,已证无误!
上图只为大家演示了添加和排序过程,演示过程不够清晰,还请大家谅解!不说了,不说了,舍友叫我打游戏呢!