本文實例為大家分享了C語言通訊錄實例的具體代碼,供大家參考,具體內容如下
main.c文件:
// // main.c // c語言通訊錄 // // Created by stevenchang on 9/6/15. // Copyright (c) 2015 cz. All rights reserved. // /** 1.添加聯系人 2.刪除聯系人 3.更新聯系人 4.顯示所有聯系人 5.查找聯系人 6.退出系統 */ #include <stdio.h> int main(int argc, const char * argv[]) { //程序的初始化 initContacts(); while(1) { int flag = 0; printf("*************C語言通訊錄*************/n"); printf("************1.添加用戶***************/n"); printf("************2.刪除用戶***************/n"); printf("************3.更新用戶***************/n"); printf("************4.查找用戶***************/n"); printf("************5.顯示所有用戶************/n"); printf("************6.退出系統***************/n"); printf("************************************/n"); printf("請輸入1-6的功能編號 :/n"); scanf("%d",&flag); //判斷編號是否合法 validateNum(flag, 1, 6); switch (flag) { case 1: addContact(); //添加用戶 break; case 2: deleteContact(); //刪除用戶 break; case 3: updateContact(); //更新用戶 break; case 4: searchContact(); //查找用戶 break; case 5: listContact(); //顯示所有用戶 break; case 6: printf("系統正在退出!/n"); printf("成功退出系統!/n"); return 0; break; default: break; } } return 0; }
MyFile.c文件:
// // MyFile.c // c語言通訊錄 // // Created by stevenchang on 9/6/15. // Copyright (c) 2015 cz. All rights reserved. // #include "MyFile.h" //**************函數的實現**********************// /** * 添加聯系人 */ void addContact() { int flag = 0; printf("您選擇的是添加聯系人,請按操作進行!/n"); printf("請輸入用戶姓名(*注意聯系人姓名中間不能有空格):/n"); scanf("%s",contacts[totalContactsCount].name); printf("請輸入電話(*注意聯系人電話中間不能有空格): /n"); scanf("%s",contacts[totalContactsCount].tel); printf("是否確認添加!1:是 0:否/n"); scanf("%d",&flag); if (flag) { //聯系人個數加1 totalContactsCount++; //將聯系人信息保存到文件中 writeFile(); } printf("已經放棄添加!/n"); return ; } /** * 刪除聯系人 */ void deleteContact() { int no; printf("您選擇的是刪除聯系人,請按操作進行!/n"); printf("請輸入要刪除的編號:/n"); scanf("%d",&no); //判斷輸入的編號是否合法 if (!validateNum(no, 1, totalContactsCount)) { printf("您輸入的編號不合法!/n"); return ; } //合法 //如果刪除的是最后一個元素 if (no == totalContactsCount) { totalContactsCount--; } else { //如果刪除的不是最后一個元素 for (int i = no; i < totalContactsCount; i++) { contacts[no-1] = contacts[no]; //元素的移動和覆蓋 } totalContactsCount--; } //同步文件 writeFile(); } /** * 更新聯系人 */ void updateContact() { int no; char newName[NAMELENGTH]; char newTel[TELLENGTH]; printf("您選擇的是更新聯系人,請按操作進行!/n"); printf("請輸入要修改的聯系人編號:/n"); scanf("%d",&no); //判斷編號是否合法 if (!validateNum(no, 1, totalContactsCount)) { return ; } //合法 printf("請重新輸入用戶名:/n"); scanf("%s",newName); printf("請重新輸入電話號碼/n"); scanf("%s",newTel); strcpy(contacts[no-1].name, newName); strcpy(contacts[no-1].tel, newTel); //寫入文件 writeFile(); } /** * 顯示所有聯系人 */ void listContact() { printf("您選擇的是顯示所有聯系人,聯系人如下!/n"); if (totalContactsCount > 0) { printf("序號/t姓名/t電話/n"); for (int i = 0 ; i < totalContactsCount; i++) { printf("%d/t%s/t%s/n",i+1,contacts[i].name, contacts[i].tel); } } else { printf("聯系人為空,請添加聯系人!/n"); return ; } } /** * 查找聯系人 */ void searchContact() { printf("您選擇的是查找聯系人,請按操作進行!/n"); char searchName[NAMELENGTH]; printf("請輸入要查找的聯系人姓名:/n"); scanf("%s",searchName); for (int i = 0 ; i < totalContactsCount; i++) { if (strcmp(searchName, contacts[i].name)==0) { //說明相同 printf("聯系人姓名為:%s,電話號碼為:%s/n",contacts[i].name,contacts[i].tel); return ; } if (i == totalContactsCount-1) { printf("此聯系人不存在!/n"); } } } /** * 通訊錄的初始化 */ void initContacts() { printf("通訊錄正在初始化!/n"); FILE *fp = fopen(filePath, "r"); if (fp!=NULL) { //讀取文件成功 //讀取聯系人的個數 fread(&totalContactsCount, sizeof(totalContactsCount), 1, fp); //讀取每個聯系人 for (int i = 0; i < totalContactsCount; i++) { //讀取聯系人數據,到聯系人數組中 fread(&contacts[i], sizeof(Person), 1, fp); } } else { //讀取文件失敗 //創建文件 fp = fopen(filePath, "wb"); //寫入聯系人的個數 fwrite(&totalContactsCount, sizeof(totalContactsCount), 1, fp); printf("通訊錄文件創建成功!/n"); } //關閉文件指針 fclose(fp); printf("通訊錄初始化成功!/n"); } /** * 判斷功能編號是否合法 1:合法 0:非法 */ int validateNum(int num, int min, int max) { if (num < min || num > max) { printf("輸入的功能編號不正確,請重新輸入!/n"); return 0; } return 1; } /** *將聯系人寫入文件 */ void writeFile() { //以二進制文件打開文件 FILE *fp = fopen(filePath, "wb"); if (fp != NULL) { //寫入聯系人個數 fwrite(&totalContactsCount, sizeof(totalContactsCount), 1, fp); //寫入每個聯系人個數 for (int i = 0; i < totalContactsCount; i++) { fwrite(&contacts[i], sizeof(Person), 1, fp); } } fclose(fp); printf("文件更新成功/n"); }
MyFile.h文件:
// // MyFile.h // c語言通訊錄 // // Created by stevenchang on 9/6/15. // Copyright (c) 2015 cz. All rights reserved. // #ifndef __c_______MyFile__ #define __c_______MyFile__ #include <stdio.h> #include <string.h> #define N 100 //宏定義一個通訊錄的容量 #define NAMELENGTH 22 //宏定義一個名字的長度 #define TELLENGTH 12 //宏定義一個電話號碼的長度 //********************函數的聲明*********************// void addContact(); //添加聯系人 void deleteContact(); //刪除聯系人 void updateContact(); //更新聯系人 void listContact(); //顯示所有聯系人 void searchContact(); //查找聯系人 void initContacts(); //通訊錄的初始化 int validateNum(int num, int min, int max); //判斷功能編號是否合法 1:合法 0:非法 void writeFile(); //將聯系人寫入文件 typedef struct Person { //定義一個結構體 char name[NAMELENGTH]; //定義姓名數組 char tel[TELLENGTH]; //定義結構體數組 } Person; //定義文件路徑 char *filePath = "telData.data"; int totalContactsCount = 0; Person contacts[N]; //定義Person結構體數組 #endif /* defined(__c_______MyFile__) */
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持VEVB武林網。
新聞熱點
疑難解答