标签:
#ifndef __CONTACT_H__ #define __CONTACT_H__ #include<stdio.h> #include<string.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
<span style="font-family: Arial, Helvetica, sans-serif;"> </span>
<span style="font-family: Arial, Helvetica, sans-serif;"> </span>
<span style="font-family: Arial, Helvetica, sans-serif;">#include"contact.h"</span>
<span style="font-family: Arial, Helvetica, sans-serif;"> </span>
int find(char *p, Pcon pcon) { <span style="white-space:pre"> </span>int i; for (i = 0; i < pcon->count; i++) { if (strcmp(p, pcon->dhb[i].name) == 0) return i; else return -1; } } void add_peo(Pcon pcon) { if (pcon->count < MAX_PEO) { printf("请输入姓名:\n"); scanf_s("%s", pcon->dhb[pcon->count].name, NAME_LEN); printf("请输入年龄:\n"); scanf_s("%d", &(pcon->dhb[pcon->count]).age,3); printf("请输入性别:\n"); scanf_s("%s", pcon->dhb[pcon->count].sex, SEX_LEN); printf("请输入电话:\n "); scanf_s("%s", pcon->dhb[pcon->count].tele,TELE_LEN); printf("请输入地址:\n"); scanf_s("%s", pcon->dhb[pcon->count].addr,ADDR_LEN); pcon->count++; printf("已添加成功!\n"); } else { printf("电话本已满!\n"); return ; } } void del_peo(Pcon pcon) { int i, j; char name[20]; printf("请输入要删除的姓名:\n"); scanf_s("%s", name, NAME_LEN); j = find(name, pcon); if (j == -1) { printf("要删除的人不存在!\n"); return; } else { for (i = j; i < pcon->count; i++) { pcon->dhb[i] = pcon->dhb[i + 1]; } } pcon->count--; printf("删除成功!\n"); } void search(Pcon pcon) { int i, j; char name[20]; printf("请输入要查找的姓名:\n"); scanf_s("%s", name, NAME_LEN); for (i = 0; i < pcon->count; i++) { j = find(name, pcon); if (j == -1) { printf("要查找的人不存在!\n"); return; } else { printf("姓名:%s\t", pcon->dhb[i].name); printf("年龄:%d\t", pcon->dhb[i].age); printf("性别:%s\t", pcon->dhb[i].sex); printf("电话:%s\t", pcon->dhb[i].tele); printf("地址:%s\n", pcon->dhb[i].addr); } } } void modify(Pcon pcon) { int j; char name[20]; printf("请输入要修改的姓名:\n"); scanf_s("%s", name, NAME_LEN); j = find(name, pcon); if (j == -1) { printf("要修改的人不存在!\n"); return; } else { printf("请输入姓名:\n"); scanf_s("%s", pcon->dhb[j].name, NAME_LEN); printf("请输入年龄:\n "); scanf_s("%d", &(pcon->dhb[j]).age,3); printf("请输入性别:\n"); scanf_s("%s", pcon->dhb[j].sex,SEX_LEN ); printf("请输入电话:\n"); scanf_s("%s", pcon->dhb[j].tele,TELE_LEN); printf("请输入地址:\n"); scanf_s("%s", pcon->dhb[j].addr,ADDR_LEN); } printf("修改成功!\n"); } void show(Pcon pcon) { int i = 0; for (i = 0; i < pcon->count; i++) { printf("姓名:%s\t ", pcon->dhb[i].name); printf("年龄:%d\t", pcon->dhb[i].age); printf("性别:%s\t", pcon->dhb[i].sex); printf("电话:%s\t", pcon->dhb[i].tele); printf("地址:%s\n", pcon->dhb[i].addr); } } void clear(Pcon pcon) { pcon->count = 0; } <span style="font-family: Arial, Helvetica, sans-serif;">#include"contact.h"</span>
void print_menu() { printf("*****************************\n"); printf("*** 1>add 2>del***\n"); printf("*** 3>show 4>search***\n"); printf("*** 5>modify 6>clear***\n"); printf("*****************************\n"); } int main() { PCON con; int input = 1; con.count = 0; while (input) { print_menu(); printf("请选择>:"); scanf_s("%d", &input); switch (input) { case 1: add_peo(&con); break; case 2: del_peo(&con); break; case 3: show(&con); break; case 4: search(&con); break; case 5: modify(&con); break; case 6: clear(&con); break; default: break; } } return 0; }
标签:
原文地址:http://blog.csdn.net/feng_you000/article/details/46128573