contact.h
#ifndef __CONTACT_H__
#define __CONTACT_H__
#define NAME_LEN 20
#define SEX_LEN 5
#define TELE_LEN 12
#define ADDR_LEN 20
#define MAX_PEO 1000
typedef struct Peo
{
char name[NAME_LEN];
int age;
char sex[SEX_LEN];
char tele[TELE_LEN];
char addr[ADDR_LEN];
}PEO,*PPEO;
typedef struct Contact
{
PEO dhb[MAX_PEO];//存储人的信息
int count;//记录有效个数
}PCON,*Pcon;
void add_peo(Pcon pcon);
void del_peo(Pcon pcon);
void search(Pcon pcon);
void modify(Pcon pcon);
void show(Pcon pcon);
void clear(Pcon pcon);
#endif
test.c
#include <stdio.h>
#include "contact.h"
void print_menu()
{
printf("*************************************\n");
printf("*****1:add 2:del******\n");
printf("*****3:search 4:modify***\n");
printf("*****5:show 6:clear****\n");
printf("*************************************\n");
}
int main()
{
PCON con;
int input = 1;
con.count = 0;
while(input)
{
print_menu();
printf("请选择操作:\n");
scanf("%d",&input);
switch(input)
{
case 1:
add_peo(&con);
break;
case 2:
del_peo(&con);
break;
case 3:
search(&con);
break;
case 4:
modify(&con);
break;
case 5:
show(&con);
break;
case 6:
clear(&con);
break;
default:
break;
}
}
return 0;
}
main.c
#include<stdio.h>
#include<string.h>
#include"contact.h"
int find(char *p, Pcon pcon)
{
int i = 0;
for(;i < pcon->count; i++)
{
if(strcmp(pcon->dhb[i].name,p) == 0)
return i;
}
return -1;
}
void add_peo(Pcon pcon)
{
if(pcon->count < MAX_PEO)
{
printf("请输入姓名、年龄、性别、电话、住址:\n");
scanf("%s",pcon->dhb [pcon->count].name);
scanf("%d",&pcon->dhb [pcon->count].age);
scanf("%s",pcon->dhb [pcon->count].sex);
scanf("%s",pcon->dhb [pcon->count].tele);
scanf("%s",pcon->dhb [pcon->count].addr);
}
pcon->count ++;
printf("存储成功\n");
}
void del_peo(Pcon pcon)
{
int i = 0;
int ret = 0;
char name[10];
printf("请输入要删除的名字:\n");
scanf("%s",name);
ret = find(name,pcon);
if(ret == -1)
{
printf("无此联系人\n");
}
else
for(i = ret; i < pcon->count-1; i++)
{
pcon->dhb[i] = pcon->dhb[i + 1];
}
pcon->count--;
printf("删除成功\n");
}
void search(Pcon pcon)
{
int i = 0;
int ret = 0;
char name[10];
printf("请输入要查找人的名字:\n");
scanf("%s",name);
ret = find(name,pcon);
if(ret == -1)
{
printf("无此联系人\n");
}
else
{
printf("姓名:%10s\t",pcon->dhb[i].name);
printf("年龄:%10d\t",pcon->dhb[i].age);
printf("性别:%10s\t",pcon->dhb[i].sex);
printf("电话:%10s\t",pcon->dhb[i].tele);
printf("住址:%10s\t",pcon->dhb[i].addr);
printf("\n");
}
}
void modify(Pcon pcon)
{
int i = 0;
int ret = 0;
char name[10];
printf("请输入要查找人的名字:\n");
scanf("%s",name);
ret = find(name,pcon);
printf("请输入姓名、年龄、性别、电话、住址:\n");
scanf("%s",pcon->dhb [i].name);
scanf("%d",&pcon->dhb [i].age);
scanf("%s",pcon->dhb [i].sex);
scanf("%s",pcon->dhb [i].tele);
scanf("%s",pcon->dhb [i].addr);
printf("修改成功\n");
}
void show(Pcon pcon)
{
int i = 0;
for(; i <pcon->count; i++)
{
printf("姓名:%10s\t",pcon->dhb[i].name);
printf("年龄:%10d\t",pcon->dhb[i].age);
printf("性别:%10s\t",pcon->dhb[i].sex);
printf("电话:%10s\t",pcon->dhb[i].tele);
printf("住址:%10s\t",pcon->dhb[i].addr);
printf("\n");
}
}
void clear(Pcon pcon)
{
pcon->count = 0;
}
时间: 2024-10-14 16:56:16