国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C > 正文

C語言通訊錄實例分享

2020-01-26 13:45:59
字體:
供稿:網(wǎng)友

本文實例為大家分享了C語言通訊錄實例的具體代碼,供大家參考,具體內(nèi)容如下

main.c文件:

// // main.c // c語言通訊錄 // // Created by stevenchang on 9/6/15. // Copyright (c) 2015 cz. All rights reserved. //  /**  1.添加聯(lián)系人  2.刪除聯(lián)系人  3.更新聯(lián)系人  4.顯示所有聯(lián)系人  5.查找聯(lián)系人  6.退出系統(tǒng)  */ #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.退出系統(tǒng)***************/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("系統(tǒng)正在退出!/n");     printf("成功退出系統(tǒng)!/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"  //**************函數(shù)的實現(xiàn)**********************//  /**  * 添加聯(lián)系人  */ void addContact() {  int flag = 0;  printf("您選擇的是添加聯(lián)系人,請按操作進行!/n");  printf("請輸入用戶姓名(*注意聯(lián)系人姓名中間不能有空格):/n");  scanf("%s",contacts[totalContactsCount].name);  printf("請輸入電話(*注意聯(lián)系人電話中間不能有空格): /n");  scanf("%s",contacts[totalContactsCount].tel);  printf("是否確認添加!1:是 0:否/n");  scanf("%d",&flag);  if (flag) {   //聯(lián)系人個數(shù)加1   totalContactsCount++;      //將聯(lián)系人信息保存到文件中   writeFile();  }  printf("已經(jīng)放棄添加!/n");  return ; }  /**  * 刪除聯(lián)系人  */ void deleteContact() {  int no;  printf("您選擇的是刪除聯(lián)系人,請按操作進行!/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(); }  /**  * 更新聯(lián)系人  */ void updateContact() {  int no;  char newName[NAMELENGTH];  char newTel[TELLENGTH];  printf("您選擇的是更新聯(lián)系人,請按操作進行!/n");  printf("請輸入要修改的聯(lián)系人編號:/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(); }  /**  * 顯示所有聯(lián)系人  */ void listContact() {  printf("您選擇的是顯示所有聯(lián)系人,聯(lián)系人如下!/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("聯(lián)系人為空,請?zhí)砑勇?lián)系人!/n");   return ;  } }  /**  * 查找聯(lián)系人  */ void searchContact() {  printf("您選擇的是查找聯(lián)系人,請按操作進行!/n");  char searchName[NAMELENGTH];  printf("請輸入要查找的聯(lián)系人姓名:/n");  scanf("%s",searchName);  for (int i = 0 ; i < totalContactsCount; i++) {   if (strcmp(searchName, contacts[i].name)==0) { //說明相同    printf("聯(lián)系人姓名為:%s,電話號碼為:%s/n",contacts[i].name,contacts[i].tel);    return ;   }   if (i == totalContactsCount-1) {    printf("此聯(lián)系人不存在!/n");   }  } }  /**  * 通訊錄的初始化  */ void initContacts() {  printf("通訊錄正在初始化!/n");  FILE *fp = fopen(filePath, "r");  if (fp!=NULL) { //讀取文件成功   //讀取聯(lián)系人的個數(shù)   fread(&totalContactsCount, sizeof(totalContactsCount), 1, fp);   //讀取每個聯(lián)系人   for (int i = 0; i < totalContactsCount; i++) {    //讀取聯(lián)系人數(shù)據(jù),到聯(lián)系人數(shù)組中    fread(&contacts[i], sizeof(Person), 1, fp);   }  } else {   //讀取文件失敗   //創(chuàng)建文件   fp = fopen(filePath, "wb");   //寫入聯(lián)系人的個數(shù)   fwrite(&totalContactsCount, sizeof(totalContactsCount), 1, fp);      printf("通訊錄文件創(chuàng)建成功!/n");  }    //關(guā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; }  /**  *將聯(lián)系人寫入文件  */ void writeFile() {  //以二進制文件打開文件  FILE *fp = fopen(filePath, "wb");    if (fp != NULL) {   //寫入聯(lián)系人個數(shù)   fwrite(&totalContactsCount, sizeof(totalContactsCount), 1, fp);   //寫入每個聯(lián)系人個數(shù)   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  //宏定義一個電話號碼的長度  //********************函數(shù)的聲明*********************// void addContact(); //添加聯(lián)系人 void deleteContact(); //刪除聯(lián)系人 void updateContact(); //更新聯(lián)系人 void listContact(); //顯示所有聯(lián)系人 void searchContact(); //查找聯(lián)系人 void initContacts();  //通訊錄的初始化 int validateNum(int num, int min, int max); //判斷功能編號是否合法 1:合法 0:非法 void writeFile(); //將聯(lián)系人寫入文件  typedef struct Person { //定義一個結(jié)構(gòu)體  char name[NAMELENGTH];  //定義姓名數(shù)組  char tel[TELLENGTH];  //定義結(jié)構(gòu)體數(shù)組 } Person;  //定義文件路徑 char *filePath = "telData.data"; int totalContactsCount = 0; Person contacts[N]; //定義Person結(jié)構(gòu)體數(shù)組  #endif /* defined(__c_______MyFile__) */ 

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持武林網(wǎng)。

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表

圖片精選

主站蜘蛛池模板: 沙坪坝区| 温州市| 彭水| 鄂托克前旗| 临武县| 华宁县| 隆化县| 京山县| 河南省| 苏尼特左旗| 福清市| 胶南市| 河曲县| 海兴县| 杨浦区| 承德县| 濉溪县| 甘洛县| 页游| 吉林市| 江西省| 永寿县| 日土县| 南开区| 东乡族自治县| 余干县| 松溪县| 昔阳县| 安塞县| 琼海市| 石狮市| 滦平县| 怀远县| 克拉玛依市| 柞水县| 财经| 泰来县| 屏南县| 吴桥县| 文成县| 礼泉县|