本文實例為大家分享了C語言實現(xiàn)通訊錄功能的具體代碼,供大家參考,具體內(nèi)容如下
創(chuàng)新互聯(lián)專注于綠春企業(yè)網(wǎng)站建設(shè),成都響應(yīng)式網(wǎng)站建設(shè)公司,商城建設(shè)。綠春網(wǎng)站建設(shè)公司,為綠春等地區(qū)提供建站服務(wù)。全流程定制網(wǎng)站開發(fā),專業(yè)設(shè)計,全程項目跟蹤,創(chuàng)新互聯(lián)專業(yè)和態(tài)度為您提供的服務(wù)
先定義頭文件
#ifndef __CONTACT_H__ #define __CONTACT_H__ #define NAME_LEN 10 #define SEX_LEN 6 #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 nb[MAX_PEO]; int count;//記錄有效個數(shù) }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
然后創(chuàng)建contact.c,將函數(shù)寫入
#include#include #include"contact.h" void add_peo(Pcon pcon) { if (pcon->count <= MAX_PEO) { printf("請輸入姓名:"); scanf("%s", pcon->nb[pcon->count].name); printf("請輸入年齡:"); scanf("%d", &pcon->nb[pcon->count].age); printf("請輸入性別:"); scanf("%s", pcon->nb[pcon->count].sex); printf("請輸入電話:"); scanf("%s", pcon->nb[pcon->count].tele); printf("請輸入地址:"); scanf("%s", pcon->nb[pcon->count].addr); pcon->count ++; printf("添加成功!\n"); } else printf("電話本已滿!"); } void show(Pcon pcon) { int i=0; printf("姓名 年齡 性別 電話 地址\n"); for(i=0;i count;i++) { printf("%s%8d%8s%15s%18s\n",pcon->nb[i].name,pcon->nb[i].age,pcon->nb[i].sex,pcon->nb[i].tele,pcon->nb[i].addr); } } int find(char *c,Pcon pcon) { int i=0; for(;i count;i++) { if(strcmp(c,pcon->nb[i].name)==0) return i; } return -1; } void del_peo(Pcon pcon) { char nam[NAME_LEN]; int index=0; printf("請輸入你想刪除的人:"); scanf("%s",nam); index = find(nam,pcon->nb[index].name); for(;index<=pcon->count-1;index++) { pcon->nb[index]=pcon->nb[index+1]; } pcon->count--; printf("刪除成功!\n"); } void search(Pcon pcon) { char nam[NAME_LEN]; int i=0; printf("請輸入你想查找的人:"); scanf("%s",nam); i = find(nam,pcon->nb[i].name); printf("姓名 年齡 性別 電話 地址\n"); printf("%s%8d%8s%15s%20s\n",pcon->nb[i].name, pcon->nb[i].age,pcon->nb[i].sex, pcon->nb[i].tele,pcon->nb[i].addr); } void modify(Pcon pcon) { char nam[NAME_LEN]; int i=0; printf("請輸入你想修改的人:"); scanf("%s",nam); i = find(nam,pcon->nb[i].name); pcon->nb[i] = pcon->nb[-1]; printf("請輸入姓名:"); scanf("%s", pcon->nb[i].name); printf("請輸入年齡:"); scanf("%d", &pcon->nb[i].age); printf("請輸入性別:"); scanf("%s", pcon->nb[i].sex); printf("請輸入電話:"); scanf("%s", pcon->nb[i].tele); printf("請輸入地址:"); scanf("%s", pcon->nb[i].addr); printf("添加成功!\n"); } void clear(Pcon pcon) { pcon->count = 0; }
最后寫入函數(shù)主體
#include#include "contact.h" #define _CRT_SECURE_NO_WARINGS_ 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("%d", &input); // system("cls"); 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; }
下圖為通訊錄運行示例截圖
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。