这里是一个简单的通讯录,固定为1000容量,不能动态增长,不能写入文件。初级版本。
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<string.h>
int count = 0;
void menu()
{
printf("**************************************\n");
printf("***********1.添加联系人信息***********\n");
printf("***********2.删除指定联系人信息*******\n");
printf("***********3.查找指定联系人信息*******\n");
printf("***********4.修改指定联系人信息*******\n");
printf("***********5.显示所有联系人信息*******\n");
printf("***********6.清空所有联系人***********\n");
printf("***********7.以名字排序所有联系人*****\n");
printf("***********0.******退出***************\n");
printf("**************************************\n");
}
typedef struct teldir
{
char name[10];
char add[10];
char sex[5];
char num[12];
int age;
}dir;
dir stu[1000] = { "0", "0", "0", "0", 0 };
void input()//添加
{
printf("请输入添加联系人的信息\n");
scanf("%s %s %s %s %d", &stu[count].name, &stu[count].add,
&stu[count].sex, &stu[count].num, &stu[count].age);
count++;
printf("添加成功!");
}
void show_all()//显示全部
{
int i = 0;
printf("姓名*******地址******性别***电话**********年龄**\n");
for (i = 0; i< count+1; i++)
{
if (stu[i].age)
{
printf("%-10s %-10s %-5s %-12s %d\n", stu[i].name, stu[i].add,
stu[i].sex, stu[i].num, stu[i].age);
}
}
}
void deleted()//删除指定对象的信息
{
int flag = 0;
int i = 0;
dir test;
dir test1 = { "0", "0", "0", "0", 0 };
printf("请输入要删除对象的姓名\n");
scanf("%s", &test.name);
for (i = 0; i < count + 1; i++)
{
if (strcmp((stu[i].name), (test.name)) == 0)
{
stu[i] = test1;
printf("删除成功!");
flag = 1;
}
}
if (flag == 0)
{
printf("没有此联系人,请确认\n");
}
}
void lookfor()//查找指定对象的信息
{
int flag = 0;
int i = 0 ;
dir test;
printf("请输入指定对象的姓名\n");
scanf("%s", &test.name);
for (i = 0; i < count + 1; i++)
{
if (strcmp((stu[i].name), (test.name)) == 0)
{
printf("%-10s %-10s %-5s %-12s %d\n", stu[i].name, stu[i].add,
stu[i].sex, stu[i].num, stu[i].age);
flag = 1;
}
else
;
}
if (flag == 0)
{
printf("没有此联系人\n");
}
}
void update()//更新信息
{
int flag = 0;
int i = 0;
dir test;
printf("请输入要更改对象的姓名\n");
scanf("%s", &test.name);
for (i = 0; i < count + 1; i++)
{
if (strcmp((stu[i].name), (test.name)) == 0)
{
printf("请输入更新后的信息\n");
scanf("%s %s %s %s %d", &stu[i].name, &stu[i].add,
&stu[i].sex, &stu[i].num, &stu[i].age);
printf("更新成功!");
flag = 1;
}
}
if (flag == 0)
{
printf("没有此联系人,请确认\n");
}
}
void delete_all()//清空
{
memset(stu, 0, sizeof(stu));
printf("清空完成!");
};
void sort_byname()//按姓名排序
{
//int i = 0;
//dir test;
//for (i = 0; i, count + 1; i++)
//{
// if (strcmp(stu[i].name, stu[i + 1].name) < 0)
// {
// memmove(&test, &stu[i], sizeof(stu));
// memmove( &stu[i],&stu[i+1], sizeof(stu));
// memmove(&stu[i+1], &test, sizeof(stu));
// }
//}
printf("此功能暂时关闭!");
}
int main()
{
int i = 0;
menu();
while (i+1)
{
printf("请选择操作\n");
scanf("%d", &i);
switch (i)
{
case 0:
return 0;
break;
case 1:
input();
break;
case 2:
deleted();
break;
case 3:
lookfor();
break;
case 4:
update();
break;
case 5:
show_all();
break;
case 6:
delete_all();
break;
case 7:
sort_byname();
break;
default:
printf("输入错误\n");
break;
}
}
}