system.h
#include <stdio.h>
#include <string.h>
#define MAX 1000
#define MAX_NAME 20
#define MAX_SEX 5
#define MAX_TELE 12
#define MAX_ADDR 30
enum Option
{
EXIT,
ADD,
DEL,
SEARCH,
MODIFY,
SHOW,
SORT,
};
struct Peoinfo
{
char name[MAX_NAME];
int age;
char sex[MAX_SEX];
char tele[MAX_TELE];
char addr[MAX_ADDR];
};
struct massage
{
struct Peoinfo date[MAX];
int size;
};
void Initsystem(struct massage* ps);
void ADDmassage(struct massage* ps);
void SHOWmassage(const struct massage* ps);
void DELmassage(struct massage* ps);
void SEARCHmassage(struct massage* ps);
void MODIFYmassage(struct massage* ps);
void SORTmassagebyage(struct massage* ps);
int FINDmassagebyname(struct massage* ps,char name[MAX_NAME]);
system.cpp
#include "system.h"
void Initsystem(struct massage*ps)
{
memset(ps->date,0,sizeof(ps->date));
ps->size=0;
}
void ADDmassage(struct massage*ps)
{
if (ps->size==MAX)
printf("信息储存过多,无法增加\n");
else
{
printf("请输入名字:\n");
scanf("%s",ps->date[ps->size].name);
printf("请输入年龄:\n");
scanf("%d",&(ps->date[ps->size].age));
printf("请输入性别:\n");
scanf("%s",ps->date[ps->size].sex);
printf("请输入电话:\n");
scanf("%s",ps->date[ps->size].tele);
printf("请输入地址:\n");
scanf("%s",ps->date[ps->size].addr);
ps->size++;
printf("添加成功\n");
}
}
void SHOWmassage(const struct massage* ps)
{
if(ps->size==0)
{
printf("无信息\n");
}
else
{
int i=0;
printf("%-20s\t%-4s\t%-5s\t%-12s\t%-20s\n","名字","年龄","性别","电话","地址");
for (i=0;i<ps->size;i++)
{
printf("%-20s\t%-4d\t%-5s\t%-12s\t%-20s\n",
ps->date[i].name,
ps->date[i].age,
ps->date[i].sex,
ps->date[i].tele,
ps->date[i].addr);
}
}
}
int FINDmassagebyname(struct massage* ps,char name[MAX_NAME])
{
int i=0;
for (i=0;i<ps->size;i++)
{
if(0==strcmp(ps->date[i].name,name))
{
return i;
}
}
return -1;
}
void DELmassage(struct massage* ps)
{
char name[MAX_NAME];
printf("请输入要删除人的名字:\n");
scanf("%s",name);
int i=0;
int pos=FINDmassagebyname(ps,name);
if(pos==-1)
{
printf("要删除的人不存在\n");
}
else
{
int j=0;
for (j=i;j<ps->size-1;i++)
{
ps->date[j]=ps->date[j+1];
}
ps->size--;
printf("删除成功\n");
}
}
void SEARCHmassage(struct massage* ps)
{
char name[MAX_NAME];
printf("请输入要查找人的名字\n");
scanf("%s",name);
int pos=FINDmassagebyname(ps,name);
if(pos==-1)
{
printf("要查找的人不存在");
}
else
{
printf("找到了\n");
printf("%-20s\t%-4s\t%-5s\t%-12s\t%-20s\n","名字","年龄","性别","电话","地址");
printf("%-20s\t%-4d\t%-5s\t%-12s\t%-20s\n",
ps->date[pos].name,
ps->date[pos].age,
ps->date[pos].sex,
ps->date[pos].tele,
ps->date[pos].addr);
}
}
void MODIFYmassage(struct massage* ps)
{
char name[MAX_NAME];
printf("请输入要修改人的名字\n");
scanf("%s",name);
int pos=FINDmassagebyname(ps,name);
if(pos==-1)
{
printf("要修改人的信息不存在\n");
}
else
{
printf("请输入名字:\n");
scanf("%s",ps->date[pos].name);
printf("请输入年龄:\n");
scanf("%d",&(ps->date[pos].age));
printf("请输入性别:\n");
scanf("%s",ps->date[pos].sex);
printf("请输入电话:\n");
scanf("%s",ps->date[pos].tele);
printf("请输入地址:\n");
scanf("%s",ps->date[pos].addr);
printf("修改成功\n");
}
}
void SORTmassagebyage(struct massage* ps)
{
int i=0;
int j=0;
int tmp;
for (i=0;i<ps->size-1;i++)
{
for (j=0;j<ps->size-1-i;i++)
{
if(ps->date[j].age>ps->date[j+1].age)
{
tmp=ps->date[j].age;
ps->date[j].age=ps->date[j+1].age;
ps->date[j+1].age=tmp;
}
}
}
}
test.h
#include "system.h"
void menu()
{
printf("*********************************\n");
printf("**** massage system ****\n");
printf("*********************************\n");
printf("****1.add 2.del ****\n");
printf("****3.search 4.modify****\n");
printf("****5.show 6.sort ****\n");
printf("****0.exit ****\n");
printf("*********************************\n");
}
int main()
{
int input;
struct massage con;
Initsystem(&con);
do
{
menu();
printf("请选择:");
scanf("%d",&input);
switch (input)
{
case ADD:
ADDmassage(&con);
break;
case DEL:
DELmassage(&con);
break;
case SEARCH:
SEARCHmassage(&con);
break;
case MODIFY:
MODIFYmassage(&con);
break;
case SHOW:
SHOWmassage(&con);
break;
case SORT:
SORTmassage(&con);
break;
case EXIT:
printf("退出程序\n");
break;
default:
printf("选择错误,重新输入:\n");
break;
}
} while (input);
}