實(shí)現(xiàn)一個(gè)通訊錄; 通訊錄可以用來存儲(chǔ)1000個(gè)人的信息,每個(gè)人的信息包括: 姓名、性別、年齡、電話、住址 提供方法: 1.添加聯(lián)系人信息 2.刪除指定聯(lián)系人信息 3.查找指定聯(lián)系人信息 4.修改指定聯(lián)系人信息 5.顯示所有聯(lián)系人信息 6.清空所有聯(lián)系人 7.以名字排序所有聯(lián)系人 以下提供第一種方法: 1.靜態(tài)實(shí)現(xiàn): #define _CRT_SECURE_NO_WARNINGS 1 #ifndef __CONTACT_H__ #define __CONTACT_H__ #include#include enum OP { EXIT, ADD, DEL, MODIFY, SEARCH, DISPLAY, SORT, CLEAR }; #define NAME_MAX 20 #define SEX_MAX 5 #define TELE_MAX 12 #define ADDR_MAX 25 #define MAX 1000 typedef struct PerInfo { char name[NAME_MAX]; int age; char sex[SEX_MAX]; char tele[TELE_MAX]; char addr[ADDR_MAX]; }PeoInfo; typedef struct Contact { PeoInfo data[MAX]; int size; }Contact,*Pcon; void init_contact(Pcon pcon); void _add_contact(Pcon pcon); void _display_contact(Pcon pcon); void _del_contact(Pcon pcon); void _search_contact(Pcon pcon); void _modify_contact(Pcon pcon); void _sort_contact(Pcon pcon); void _clear_contact(Pcon pcon); #endif //__CONTACT_H__
函數(shù)實(shí)現(xiàn)部分:
創(chuàng)新互聯(lián)建站專注于企業(yè)全網(wǎng)整合營銷推廣、網(wǎng)站重做改版、岳陽縣網(wǎng)站定制設(shè)計(jì)、自適應(yīng)品牌網(wǎng)站建設(shè)、H5場(chǎng)景定制、成都做商城網(wǎng)站、集團(tuán)公司官網(wǎng)建設(shè)、外貿(mào)營銷網(wǎng)站建設(shè)、高端網(wǎng)站制作、響應(yīng)式網(wǎng)頁設(shè)計(jì)等建站業(yè)務(wù),價(jià)格優(yōu)惠性價(jià)比高,為岳陽縣等各大城市提供網(wǎng)站開發(fā)制作服務(wù)。
#define _CRT_SECURE_NO_WARNINGS 1 #include "Contact.h" // //git //svn // void init_contact(Pcon pcon) { memset(pcon->data, 0, MAX*sizeof(PeoInfo)); pcon->size = 0; } void _add_contact(Pcon pcon) { if(pcon->size >= MAX) { printf("電話本滿了\n"); return; } printf("請(qǐng)輸入名字:>"); scanf("%s",pcon->data[pcon->size].name); printf("請(qǐng)輸入年齡:>"); scanf("%d",&(pcon->data[pcon->size].age)); printf("請(qǐng)輸入性別:>"); scanf("%s",pcon->data[pcon->size].sex); printf("請(qǐng)輸入電話:>"); scanf("%s",pcon->data[pcon->size].tele); printf("請(qǐng)輸入地址:>"); scanf("%s",pcon->data[pcon->size].addr); pcon->size++; printf("添加成功\n"); } void _display_contact(Pcon pcon) { int i = 0; printf("%9s\t%3s\t%4s\t%11s\t%10s\n","name","age","sex","tele","addr"); for(i = 0;isize;i++) { printf("%9s\t%3d\t%4s\t%11s\t%10s\n", pcon->data[i].name, pcon->data[i].age, pcon->data[i].sex, pcon->data[i].tele, pcon->data[i].addr); } } static int find_entry(Pcon pcon, char *name) { int i = 0; for(i = 0;i size; i++) { if(strcmp(pcon->data[i].name,name) == 0) { return i; } } return -1; } void _del_contact(Pcon pcon) { int pos = 0; int index = 0; char name[NAME_MAX]; if(pcon->size == 0) { printf("電話本空\n"); return; } printf("請(qǐng)輸入要?jiǎng)h除人的名字:>"); scanf("%s",name); pos = find_entry(pcon, name); if(pos == -1) { printf("找不到要?jiǎng)h除的人\n"); return; } // for(index = pos; index < pcon->size; index++) { pcon->data[index] = pcon->data[index+1]; } pcon->size--; printf("刪除成功\n"); } void _search_contact(Pcon pcon) { char name[NAME_MAX] = {0}; int pos = 0; printf("請(qǐng)輸入要查找人的名字:>"); scanf("%s",name); pos = find_entry(pcon, name); if(pos == -1) { printf("指定聯(lián)系人不存在\n"); return; } else { printf("%9s\t%3s\t%4s\t%11s\t%10s\n","name","age","sex","tele","addr"); printf("%9s\t%3d\t%4s\t%11s\t%10s\n", pcon->data[pos].name, pcon->data[pos].age, pcon->data[pos].sex, pcon->data[pos].tele, pcon->data[pos].addr); } } void _modify_contact(Pcon pcon) { char name[NAME_MAX] = {0}; int pos = 0; printf("請(qǐng)輸入要修改人的名字:>"); scanf("%s",name); pos = find_entry(pcon, name); if(pos == -1) { printf("指定聯(lián)系人不存在\n"); return; } else { printf("請(qǐng)輸入名字:>"); scanf("%s",pcon->data[pos].name); printf("請(qǐng)輸入年齡:>"); scanf("%d",&(pcon->data[pos].age)); printf("請(qǐng)輸入性別:>"); scanf("%s",pcon->data[pos].sex); printf("請(qǐng)輸入電話:>"); scanf("%s",pcon->data[pos].tele); printf("請(qǐng)輸入地址:>"); scanf("%s",pcon->data[pos].addr); } } //volatile void _sort_contact(Pcon pcon) { int i = 0; int j = 0; for(i = 0;i size-1; i++)//控制排序趟數(shù) { for(j = 0; j size-1-i; j++) { if(strcmp(pcon->data[j].name,pcon->data[j+1].name) > 0) { PeoInfo tmp = {0}; tmp = pcon->data[j]; pcon->data[j] = pcon->data[j+1]; pcon->data[j+1] = tmp; } } } } void _clear_contact(Pcon pcon) { pcon->size = 0; }