问题描述:
6. 清空所有联系人信息
头文件:"contacts.h"
#ifndef __CONTACT_H__//防止内容的重复 #define __CONTACT_H__ //工程中用到的函数库 #include<stdio.h> #include<string.h> #include<stdlib.h> //关于字符串的各种宏替换 #define MAX 1000 #define NAME_SIZE 20 #define SEX_SIZE 10 #define TELE_SIZE 11 #define ADDR_SIZE 20 //描述通讯录信息的结构体 struct personinfor { char name[NAME_SIZE]; char sex[SEX_SIZE]; int age; char tele[TELE_SIZE]; char addr[ADDR_SIZE]; }; typedef struct personinfor person; struct contacts { person person[MAX]; //指向信息结构体的指针 int contact_count; //统计通讯录中的元素个数 }; typedef struct contacts * pcontacts; int add_contact(pcontacts pcon); //添加联系人 int delete_contact(pcontacts pcon);//删除指定联系人 int find_contact(pcontacts pcon); //查找指定联系人 int modify_contact(pcontacts pcon);//修改指定联系人 int clear_contact(pcontacts pcon); //清空所有联系人 void show_contact(pcontacts pcon); //显示所有联系人 #endif<span style="color:#ff0000;"> </span>
#include"contacts.h" /* 寻找指定联系人的位置: 如果找到:返回该联系人的在数组中的下标 没有找到:返回-1; */ int find_entry(pcontacts pcon) { char name[NAME_SIZE]; int i = 0; if(pcon->contact_count == 0) { printf("the contacts is empty!\n"); return -1; } printf("please input a name:"); scanf("%s",name); for(i = 0;i<pcon->contact_count; ++i) { if(strcmp(name,pcon->person[i].name) == 0) { return i; } } return -1; } //增加联系人 int add_contact(pcontacts pcon) { int input = 1; if(pcon->contact_count == MAX) { printf("the contacts is full!\n"); return -1; } printf("please input a person's information according the rompt:\n"); printf("name:"); scanf("%s",pcon->person[pcon->contact_count].name); printf("sex:"); scanf("%s",pcon->person[pcon->contact_count].sex); printf("age:"); scanf("%d",&pcon->person[pcon->contact_count].age); printf("tele:"); scanf("%s",pcon->person[pcon->contact_count].tele); printf("addr:"); scanf("%s",pcon->person[pcon->contact_count].addr); pcon->contact_count++; // printf("do you want to continue ? 1:yes 0:no"); // scanf("%d",&input); // } } int delete_contact(pcontacts pcon) { int ret = find_entry(pcon); int i = 0; if(pcon->contact_count == 0) { printf("the contacts is empty!\n"); return -1; } if(ret == -1) { printf("the contact is not exsit!\n"); } for(i=ret;i<pcon->contact_count-1;++i)//注意i < (pcon->contact_count-1)而不是(pcon->contact_count) { pcon->person[i]=pcon->person[i+1];//否则越界 } pcon->contact_count--; return 1; } int find_contact(pcontacts pcon) { int ret = find_entry(pcon); if(ret!= -1) { printf("%s\t%s\t%d\t%s\t%s\n",pcon->person[ret].name,pcon->person[ret].sex,pcon->person[ret].age,pcon->person[ret].tele,pcon->person[ret].addr); return 1; } else { printf("not exsit\n"); return 0; } } //修改指定联系人 int modify_contact(pcontacts pcon) { int ret = find_entry(pcon); if(ret == -1) { printf("the contact is not exsit!\n"); return 0; } printf("please input the new infor according the rompt:\n"); printf("name:"); scanf("%s",pcon->person[ret].name); printf("sex:"); scanf("%s",pcon->person[ret].sex); printf("age:"); scanf("%d",&pcon->person[ret].age); printf("tele:"); scanf("%s",pcon->person[ret].tele); printf("addr:"); scanf("%s",pcon->person[ret].addr); return 1; } //清空联系人 int clear_contact(pcontacts pcon) { pcon->contact_count = 0; return 1; } //显示所有联系人 void show_contact(pcontacts pcon) { int i = 0; if(pcon->contact_count == 0) { printf("the contacts is empty!\n"); return -1; } printf("the contacts' infor are as following:\n "); for(i=0;i<pcon->contact_count;++i) { printf("%10s%5s%10d%10s%10s\n",pcon->person[i].name,pcon->person[i].sex,pcon->person[i].age,pcon->person[i].tele,pcon->person[i].addr); } } //菜单函数:使上述各个功能能够多次调用 int meau() { int n; printf("*******欢迎使用通讯录管理系统********\n"); printf(" 实现者:zyh_helen\n"); printf("1.添加联系人\n"); printf("2.删除指定联系人\n"); printf("3.查找指定联系人\n"); printf("4.修改指定联系人\n"); printf("5.显示所有联系人\n"); printf("6.清空联系人\n"); printf("7.退出\n"); printf("选择您所需要的功能序号1~7:\n"); scanf("%d",&n); return n; <span style="color:#ff0000;"> }</span>
#include"contacts.h" int main() { struct contacts cont; pcontacts p=&cont; int n = 0; p->contact_count = 0; while(1) { n=meau(); switch(n) { case 7: exit(0); case 1: add_contact(p); break; case 2: delete_contact(p); break; case 3: find_contact(p); break; case 4: modify_contact(p); break; case 5: show_contact(p); break; case 6: clear_contact(p); break; default: break; } } return 0; }