C語(yǔ)言實(shí)現(xiàn)通訊錄系統(tǒng)
C語(yǔ)言通訊錄系統(tǒng)實(shí)現(xiàn),供大家參考,具體內(nèi)容如下
需求分析:
利用文件讀,寫(xiě)的方法
實(shí)現(xiàn)增加通訊錄聯(lián)系人信息
實(shí)現(xiàn)刪除通訊錄聯(lián)系人信息
實(shí)現(xiàn)查找通訊錄聯(lián)系人信息
實(shí)現(xiàn)修改通訊錄聯(lián)系人信息
實(shí)現(xiàn)查看現(xiàn)有通訊錄聯(lián)系人信息
代碼實(shí)現(xiàn):
// main.c // C語(yǔ)言通訊錄實(shí)現(xiàn) // // Created by Brisinga on 15/10/14. // Copyright © 2015年 yan. All rights reserved. // #include <stdio.h> #include<string.h> #define LEN 10 #define NAMELEN 22 #define TELLEN 12 //*************結(jié)構(gòu)體變量定義**************** //定義聯(lián)系人的個(gè)數(shù) int contactCount = 0; //定義結(jié)構(gòu)體 typedef struct{ //定義聯(lián)系人姓名 char name[NAMELEN]; //定義聯(lián)系人電話 char tel[TELLEN]; }Person; //初始化聯(lián)系人數(shù)組 Person contact[LEN]; //定義文件名 charchar *path = "a.data"; //接收用戶輸入的編號(hào) int no; //**************函數(shù)聲明******************** //初始化聲明 void init(); //校驗(yàn)聲明 int isValid(int n,int min,int max); //添加聯(lián)系人 void addContact(); //刪除聯(lián)系人 void deleteContact(); //修改聯(lián)系人 void updateContact(); //查看所有聯(lián)系人 void doList(); //搜索聯(lián)系人 void searchContact(); //退出系統(tǒng) void quit(); //聯(lián)系人寫(xiě)入文件 void writeFile(); //*************************************** int main(int argc, const charchar * argv[]) { //通訊錄初始化 init(); printf("通訊錄初始化成功!\n"); while (1) { //定義界面 printf("**********************************\n"); printf("***********歡迎使用通訊錄***********\n"); printf("***********1.添加聯(lián)系人*************\n"); printf("***********2.刪除聯(lián)系人*************\n"); printf("***********3.修改聯(lián)系人*************\n"); printf("***********4.查看所有聯(lián)系人**********\n"); printf("***********5.搜索聯(lián)系人*************\n"); printf("***********6.退出系統(tǒng)***************\n"); printf("**********************************\n"); //提示用戶輸入編號(hào) printf("請(qǐng)輸入要進(jìn)行操作的編號(hào):\n"); scanf("%d",&no); //判斷輸入是否合法 isValid(no, 1, 6); //判斷用戶的操作 switch (no) { case 1: //添加聯(lián)系人 addContact(); break; case 2: //刪除聯(lián)系人 deleteContact(); break; case 3: //更新聯(lián)系人 updateContact(); break; case 4: //查看聯(lián)系人 doList(); break; case 5: //搜索聯(lián)系人 searchContact(); break; case 6: //退出 quit(); return 0; default: break; } } return 0; } //通訊錄初始化 void init(){ //定義文件指針 FILEFILE *fp = fopen(path, "r"); //判斷通訊錄是否存在 if (fp!=NULL) { //如果存在 //1.讀取聯(lián)系人個(gè)數(shù) fread(&contactCount, sizeof(contactCount), 1, fp); //2.讀取每一個(gè)聯(lián)系人nts for (int i=0; i<contactCount; i++) { fread(&contact[i], sizeof(Person), 1, fp); } }else{ //如果不存在 //創(chuàng)建通訊錄 fp = fopen(path, "wb"); //寫(xiě)入當(dāng)前聯(lián)系人的個(gè)數(shù) fwrite(&contactCount, sizeof(contactCount), 1, fp); } fclose(fp); } //校驗(yàn)輸入是否合法 int isValid(int n,int min,int max){ //如果輸入的編號(hào)大于min小于max,則返回0 if (n>=min&&n<=max) { return 0; } //否則返回1 printf("輸入非法!\n"); return 1; } //添加聯(lián)系人 void addContact(){ //提示用戶輸入要添加聯(lián)系人的姓名 printf("請(qǐng)輸入要添加的聯(lián)系人姓名:*注意姓名之間不能有空格\n"); //接收聯(lián)系人姓名 scanf("%s",contact[contactCount].name); //提示用戶輸入要添加聯(lián)系任的電話 printf("請(qǐng)輸入要添加聯(lián)系人的電話: *注意電話號(hào)碼之間不能有空格\n"); //接收聯(lián)系人電話 scanf("%s",contact[contactCount].tel); //詢問(wèn)是否確定添加 printf("確定要添加么?1.確定 0.取消\n"); scanf("%d",&no); if (no) { contactCount++; //寫(xiě)入文件 writeFile(); printf("添加成功!\n"); } } //刪除聯(lián)系人 void deleteContact(){ //顯示聯(lián)系人 doList(); int flag; //提示用戶輸入要?jiǎng)h除聯(lián)系人的編號(hào) printf("請(qǐng)輸入要?jiǎng)h除聯(lián)系人的編號(hào):\n"); //接收用戶輸入的編號(hào) scanf("%d",&no); //判斷編號(hào)是否合法 if(!isValid(no, 1, contactCount)){ //編號(hào)合法 printf("確定要?jiǎng)h除么?刪除后將無(wú)法恢復(fù)!1.確定 0.取消\n"); scanf("%d",&flag); if (flag) { //如果要?jiǎng)h除的聯(lián)系人在末尾 if (no==contactCount) { //則contactCount-1 contactCount--; }else{ //如果要?jiǎng)h除的聯(lián)系人不在末尾 //移動(dòng)數(shù)組元素 for (int i=no-1; i<contactCount-1; i++) { contact[i]=contact[i+1]; } } //contactCount-- contactCount--; //寫(xiě)文件 writeFile(); } }else //編號(hào)不合法,則退出 printf("編號(hào)不合法!\n"); return ; } //修改聯(lián)系人 void updateContact(){ //顯示聯(lián)系人 doList(); //提示用戶輸入要修改聯(lián)系人的編號(hào) printf("請(qǐng)輸入要修改的聯(lián)系人的編號(hào):\n"); //接收用戶輸入的編號(hào) scanf("%d",&no); //判斷編號(hào)是否合法 int flag; if (!isValid(no, 1, contactCount)) { //提示用戶輸入新的的聯(lián)系人的姓名 printf("請(qǐng)輸入新的姓名:*注意:姓名之間不能有空格\n"); //接收用戶輸入的姓名 scanf("%s",contact[no-1].name); //提示用戶輸入新的電話號(hào)碼 printf("請(qǐng)輸入新的電話號(hào)碼:*注意:電話號(hào)碼之間不能有空格\n"); //接收用戶輸入的電話號(hào)碼 scanf("%s",contact[no-1].tel); //詢問(wèn)用戶確定修改么? printf("確定要修改么?1.確定 0.取消\n"); scanf("%d",&flag); if (flag) { //寫(xiě)文件 writeFile(); printf("聯(lián)系人修改成功!\n"); }else{ printf("編號(hào)不合法!!\n"); return ; } } } //查看所有聯(lián)系人 void doList(){ printf("顯示所有聯(lián)系人:\n"); printf("編號(hào)\t姓名\t電話\n"); for (int i=0; i<contactCount; i++) { printf("%d\t%s\t%s\n",i+1,contact[i].name,contact[i].tel); } } //搜索聯(lián)系人 void searchContact(){ int flag = 1; //接收用戶輸入的姓名 char searchName[NAMELEN]; //接收用戶輸入的電話 char searchTel[TELLEN]; //詢問(wèn)用戶搜索方式 printf("請(qǐng)輸入查找的方式: 1.按姓名查找 2.按電話號(hào)碼查找\n"); //接收用戶的搜索方式 scanf("%d",&no); if (no==1) { //請(qǐng)用戶輸入要查找的姓名 printf("請(qǐng)輸入要查找到姓名:\n"); scanf("%s",searchName); for (int i=0; i<contactCount; i++) { if (!strcmp(searchName,contact[i].name)) { printf("您查找的聯(lián)系人為:\n"); printf("%d\t%s\t%s\n",i+1,contact[i].name,contact[i].tel); return ; }else{ flag = 0; } } }else if(no==2){ //請(qǐng)用戶輸入要查找的電話號(hào)碼 printf("請(qǐng)輸入要查找的電話號(hào)碼:\n"); scanf("%s",searchTel); for (int i=0; i<contactCount; i++) { if (!strcmp(searchTel,contact[i].tel)) { printf("您查找的聯(lián)系人為:\n"); printf("%d\t%s\t%s\n",i+1,contact[i].name,contact[i].tel); return ; }else{ flag = 0; } } }else{ printf("非法輸入!\n"); return ; } if (!flag) { printf("很抱歉!小伙伴不在通訊錄中!\n"); } } //退出系統(tǒng) void quit(){ printf("系統(tǒng)正在退出...\n"); printf("系統(tǒng)已經(jīng)退出!\n"); } //聯(lián)系人寫(xiě)入文件 void writeFile(){ //定義文件指針 FILEFILE *fp = fopen(path, "wb"); if (fp!=NULL) { //寫(xiě)入聯(lián)系人個(gè)數(shù) fwrite(&contactCount, sizeof(contactCount), 1, fp); //寫(xiě)入每個(gè)聯(lián)系人 for (int i=0; i<contactCount; i++) { fwrite(&contact[i], sizeof(Person),1, fp); } } fclose(fp); }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
C語(yǔ)言安全編碼之?dāng)?shù)組索引位的合法范圍
這篇文章主要介紹了C語(yǔ)言安全編碼的數(shù)組索引位合法范圍剖析,對(duì)于編碼安全非常重要!需要的朋友可以參考下2014-07-07C語(yǔ)言中斐波那契數(shù)列的三種實(shí)現(xiàn)方式(遞歸、循環(huán)、矩陣)
本文主要介紹了C語(yǔ)言中斐波那契數(shù)列的三種實(shí)現(xiàn)方式(遞歸、循環(huán)、矩陣),文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-01-01opencv3/C++實(shí)現(xiàn)霍夫圓/直線檢測(cè)
今天小編就為大家分享一篇opencv3/C++實(shí)現(xiàn)霍夫圓/直線檢測(cè),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-12-12C++面試基礎(chǔ)之static關(guān)鍵字詳解
這篇文章主要給大家介紹了關(guān)于C++面試基礎(chǔ)之static關(guān)鍵字的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-02-02

C++11生成隨機(jī)數(shù)(random庫(kù))的使用

基于Matlab實(shí)現(xiàn)鯨魚(yú)優(yōu)化算法的示例代碼

C語(yǔ)言實(shí)現(xiàn)簡(jiǎn)易貪吃蛇游戲的示例代碼

C++關(guān)于構(gòu)造函數(shù)可向父類或者本類傳參的講解