这里是c版本的该类型作业的完整代码,c++版本可以看我的前一篇文章。
以下:
#include "stdio.h"
#include "stdlib.h"
#define my_max 1000
void Main_menu()
{
printf("****************************\n");
printf("****** 1、添加职工 ******\n" );
printf("****** 2、查看职工 ******\n" );
printf("****** 3、修改职工 ******\n" );
printf("****** 4、查找职工 ******\n" );
printf("****** 5、删除职工 ******\n" );
printf("****** 0、退出通讯录******\n" );
printf("****************************\n" );
}
typedef struct
{
char name[20];
char gender[20];
char age[20];
char phone_number[20];
char address[20];
char education[20];
}Contact_person;
typedef struct
{
Contact_person peopleArray[my_max];
int people_number ;
} Communicator;
Communicator num;
/*--------------------------------------------------功能1------------------------------------------------*/
void addperson(Communicator* num)
{
int sex = 0;
if (num->people_number >= 1000)
{
printf( "人数已满" );
}
else
{
char name[20];
char gender[20];
char age[20];
char phone[20];
char adds[20];
char education[20];
//1、输入姓名
printf( "请输入职工姓名\n" );
gets(name);
gets(name);
for(int i = 0; i < 20; i++)
{
num->peopleArray[num->people_number].name[i] = name[i];
}
//2、输入性别
printf("请输入联系人性别\n" );
gets(gender);
for (int i = 0; i < 20; i++)
{
num->peopleArray[num->people_number].gender[i] = gender[i];
}
//3、输入年龄
printf( "请输入职工年龄\n" );
gets(age);
for (int i = 0; i < 20; i++)
{
num->peopleArray[num->people_number].age[i] = age[i];
}
//4、输入号码
printf("请输入职工电话号码\n" );
gets(phone);
for (int i = 0; i < 20; i++)
{
num->peopleArray[num->people_number].phone_number[i] = phone[i];
}
//5、输入地址
printf("请输入职工住址\n" );
gets(adds);
for (int i = 0; i < 20; i++)
{
num->peopleArray[num->people_number].address[i] = adds[i];
}
//6、输入学历
printf("请输入学历\n");
gets(education);
for (int i = 0; i < 20; i++)
{
num->peopleArray[num->people_number].education[i] = education[i];
}
//记录人数
num->people_number++;
printf("添加成功\n");
}
system("pause");
system("cls");
}
/*----------------------------------------------功能2---------------------------------------------*/
void showpeople(Communicator* num)
{
if (!num->people_number)
{
printf( "暂无此员工\n");
}
else
{
for (int i = 0; i < num->people_number; i++)
{
printf("姓名是: %s\t", &num->peopleArray[i].name);
printf("性别是: %s\t", &num->peopleArray[i].gender);
printf("年龄是: %s\t", &num->peopleArray[i].age);
printf("号码是: %s\t", &num->peopleArray[i].phone_number);
printf("学历是: %s\t", num->peopleArray[i].education);
printf("住址是: %s\t\n", &num->peopleArray[i].address);
}
}
system("pause");
system("cls");
}
/*------------------------------------------------功能3----------------------------------------------*/
int findpeople(Communicator* num, char name[]) //检索输入姓名
{
int s = 0;
for (int i = 0; i < num->people_number; i++)
{
for (int a = 0; a < 20; a++)
{
if (num->peopleArray[i].name[a] != name[a])
{
break;
}
else
s = 1;
}
if(s == 1)
{
return i;
}
}
return -1;
}
void deleteperson(Communicator* abs)
{
char name[20];
if (abs->people_number == 0)
{
printf("暂无人可删");
}
else
{
printf("请输入要删除的联系人姓名\n");
gets(name);
gets(name);
int a = findpeople(abs, name);
if (a == -1)
{
printf("查无此人\n" );
}
else
{
for (int i = a; i < abs->people_number; i++)
{
abs->peopleArray[i] = abs->peopleArray[i + 1];
}
printf("删除成功\n");
abs->people_number--;
}
}
system("pause");
system("cls");
}
/*---------------------------------------------------功能4-------------------------------------------------*/
void changepeople(Communicator* num)
{
char name[20];
if (num->people_number == 0)
{
printf("暂无人可修改\n");
}
else
{
printf("输入要修改的联系人姓名\n");
gets(name);
gets(name);
int res = 0;
char sex[20];
char age[20];
char phone[20];
char adds[20];
char edu[20];
res = findpeople(num, name);
if (res == -1)
{
printf("查无此人");
}
else
{
printf("姓名 \n");
gets(name);
for (int i = 0; i < 20; i++)
{
num->peopleArray[res].name[i] = name[i];
}
printf("性别 \n");
gets(sex);
for (int i = 0; i < 20; i++)
{
num->peopleArray[res].gender[i] = sex[i];
}
printf("年龄\n");
gets(age);
for (int i = 0; i < 20; i++)
{
num->peopleArray[res].age[i] = age[i];
}
printf("号码\n");
gets(phone);
for (int i = 0; i < 20; i++)
{
num->peopleArray[res].phone_number[i] = phone[i];
}
printf("学历\n");
gets(edu);
for (int i = 0; i < 20; i++)
{
num->peopleArray[res].education[i] = edu[i];
}
printf("地址\n" );
gets(adds);
for (int i = 0; i < 20; i++)
{
num->peopleArray[res].address[i] = adds[i];
}
printf("修改完成\n");
}
}
system("pause");
system("cls");
}
/*---------------------------------------------功能6--------------------------------------------------*/
void queryperson(Communicator* num)
{
char name[20];
if (num->people_number == 0)
{
printf("暂无人可查\n");
}
else {
printf("请输入要查询的联系人的姓名\n");
gets(name);
gets(name);
int res = 0;
res = findpeople(num, name);
if (res == -1)
{
printf("查无此人\n");
}
else
{
printf("姓名 %s\n", num->peopleArray[res].name );
printf("性别 %s\n", num->peopleArray[res].gender);
printf("年龄 %s\n", num->peopleArray[res].age);
printf("号码 %s\n", num->peopleArray[res].phone_number);
printf("学历 %s\n", num->peopleArray[res].education);
printf("住址 %s\n", num->peopleArray[res].address);
}
}
system("pause");
system("cls");
}
int main()
{
int choose = 0;
num.people_number = 0; //初始化人数
while (1)
{
Main_menu();
scanf_s("%d", &choose);
switch (choose)
{
case 1:
addperson(&num);
break;
case 2:
showpeople(&num);
break;
case 3:
changepeople(&num);
break;
case 4:
queryperson(&num);
break;
case 5:
deleteperson(&num);
break;
case 0:
printf("欢迎下次使用\n");
return 0;
break;
default:
printf("输入有误\n");
system("pause");
system("cls");
break;
}
}
system("pause");
return 0;
}
谢谢观看,如有问题欢迎讨论哦。