欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C語言通訊錄實(shí)例分享

 更新時(shí)間:2018年02月15日 09:42:02   作者:StevenLoveMaggie  
這篇文章主要為大家分享了C語言通訊錄實(shí)例,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了C語言通訊錄實(shí)例的具體代碼,供大家參考,具體內(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("請(qǐng)輸入1-6的功能編號(hào) :\n"); 
  scanf("%d",&flag); 
  //判斷編號(hào)是否合法 
  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ù)的實(shí)現(xiàn)**********************// 
 
/** 
 * 添加聯(lián)系人 
 */ 
void addContact() { 
 int flag = 0; 
 printf("您選擇的是添加聯(lián)系人,請(qǐng)按操作進(jìn)行!\n"); 
 printf("請(qǐng)輸入用戶姓名(*注意聯(lián)系人姓名中間不能有空格):\n"); 
 scanf("%s",contacts[totalContactsCount].name); 
 printf("請(qǐng)輸入電話(*注意聯(lián)系人電話中間不能有空格): \n"); 
 scanf("%s",contacts[totalContactsCount].tel); 
 printf("是否確認(rèn)添加!1:是 0:否\n"); 
 scanf("%d",&flag); 
 if (flag) { 
  //聯(lián)系人個(gè)數(shù)加1 
  totalContactsCount++; 
   
  //將聯(lián)系人信息保存到文件中 
  writeFile(); 
 } 
 printf("已經(jīng)放棄添加!\n"); 
 return ; 
} 
 
/** 
 * 刪除聯(lián)系人 
 */ 
void deleteContact() { 
 int no; 
 printf("您選擇的是刪除聯(lián)系人,請(qǐng)按操作進(jìn)行!\n"); 
 printf("請(qǐng)輸入要?jiǎng)h除的編號(hào):\n"); 
 scanf("%d",&no); 
  
 //判斷輸入的編號(hào)是否合法 
 if (!validateNum(no, 1, totalContactsCount)) { 
  printf("您輸入的編號(hào)不合法!\n"); 
  return ; 
 } 
 //合法 
 //如果刪除的是最后一個(gè)元素 
 if (no == totalContactsCount) { 
  totalContactsCount--; 
 } else { 
  //如果刪除的不是最后一個(gè)元素 
  for (int i = no; i < totalContactsCount; i++) { 
   contacts[no-1] = contacts[no]; //元素的移動(dòng)和覆蓋 
  } 
  totalContactsCount--; 
 } 
  
 //同步文件 
 writeFile(); 
} 
 
/** 
 * 更新聯(lián)系人 
 */ 
void updateContact() { 
 int no; 
 char newName[NAMELENGTH]; 
 char newTel[TELLENGTH]; 
 printf("您選擇的是更新聯(lián)系人,請(qǐng)按操作進(jìn)行!\n"); 
 printf("請(qǐng)輸入要修改的聯(lián)系人編號(hào):\n"); 
 scanf("%d",&no); 
  
 //判斷編號(hào)是否合法 
 if (!validateNum(no, 1, totalContactsCount)) { 
  return ; 
 } 
  
 //合法 
 printf("請(qǐng)重新輸入用戶名:\n"); 
 scanf("%s",newName); 
 printf("請(qǐng)重新輸入電話號(hào)碼\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("序號(hào)\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)系人為空,請(qǐng)?zhí)砑勇?lián)系人!\n"); 
  return ; 
 } 
} 
 
/** 
 * 查找聯(lián)系人 
 */ 
void searchContact() { 
 printf("您選擇的是查找聯(lián)系人,請(qǐng)按操作進(jìn)行!\n"); 
 char searchName[NAMELENGTH]; 
 printf("請(qǐng)輸入要查找的聯(lián)系人姓名:\n"); 
 scanf("%s",searchName); 
 for (int i = 0 ; i < totalContactsCount; i++) { 
  if (strcmp(searchName, contacts[i].name)==0) { //說明相同 
   printf("聯(lián)系人姓名為:%s,電話號(hào)碼為:%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)系人的個(gè)數(shù) 
  fread(&totalContactsCount, sizeof(totalContactsCount), 1, fp); 
  //讀取每個(gè)聯(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)系人的個(gè)數(shù) 
  fwrite(&totalContactsCount, sizeof(totalContactsCount), 1, fp); 
   
  printf("通訊錄文件創(chuàng)建成功!\n"); 
 } 
  
 //關(guān)閉文件指針 
 fclose(fp); 
 printf("通訊錄初始化成功!\n"); 
} 
 
/** 
 * 判斷功能編號(hào)是否合法 1:合法 0:非法 
 */ 
int validateNum(int num, int min, int max) { 
 if (num < min || num > max) { 
  printf("輸入的功能編號(hào)不正確,請(qǐng)重新輸入!\n"); 
  return 0; 
 } 
 return 1; 
} 
 
/** 
 *將聯(lián)系人寫入文件 
 */ 
void writeFile() { 
 //以二進(jìn)制文件打開文件 
 FILE *fp = fopen(filePath, "wb"); 
  
 if (fp != NULL) { 
  //寫入聯(lián)系人個(gè)數(shù) 
  fwrite(&totalContactsCount, sizeof(totalContactsCount), 1, fp); 
  //寫入每個(gè)聯(lián)系人個(gè)數(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   //宏定義一個(gè)通訊錄的容量 
#define NAMELENGTH 22 //宏定義一個(gè)名字的長(zhǎng)度 
#define TELLENGTH 12  //宏定義一個(gè)電話號(hào)碼的長(zhǎng)度 
 
//********************函數(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); //判斷功能編號(hào)是否合法 1:合法 0:非法 
void writeFile(); //將聯(lián)系人寫入文件 
 
typedef struct Person { //定義一個(gè)結(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__) */ 

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • C++ typeid 和虛函數(shù)詳解

    C++ typeid 和虛函數(shù)詳解

    這篇文章主要介紹了c++ typeid 和虛函數(shù)的使用,幫助大家更好的理解和使用c++,感興趣的朋友可以了解下,希望能夠給你帶來幫助
    2021-09-09
  • C語言輸出孿生素?cái)?shù)的實(shí)現(xiàn)示例

    C語言輸出孿生素?cái)?shù)的實(shí)現(xiàn)示例

    本文主要介紹了C語言輸出孿生素?cái)?shù)的實(shí)現(xiàn)示例,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-01-01
  • 使用C++實(shí)現(xiàn)插件模式時(shí)的避坑要點(diǎn)(推薦)

    使用C++實(shí)現(xiàn)插件模式時(shí)的避坑要點(diǎn)(推薦)

    這篇文章主要介紹了使用C++實(shí)現(xiàn)插件模式時(shí)的避坑要點(diǎn),本文主要分析實(shí)踐中常見的、因?yàn)閷?duì)原理不清楚而搞出來的產(chǎn)品里的坑,本文給大家介紹的非常詳細(xì),需要的朋友可以參考下
    2022-08-08
  • C語言中g(shù)etchar的用法以及實(shí)例解析

    C語言中g(shù)etchar的用法以及實(shí)例解析

    getchar()是stdio.h中的庫函數(shù),它的作用是從stdin流中讀入一個(gè)字符,下面這篇文章主要給大家介紹了關(guān)于C語言中g(shù)etchar的用法以及實(shí)例的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2022-03-03
  • C++14 新特性之函數(shù)返回值類型推導(dǎo)

    C++14 新特性之函數(shù)返回值類型推導(dǎo)

    本文主要介紹了C++14 新特性之函數(shù)返回值類型推導(dǎo),在模板編程和一些返回類型復(fù)雜或不易直接指明的情況下非常有用,下面就來具體介紹一下,感興趣的可以了解一下
    2024-05-05
  • C/C++ MD5算法的實(shí)現(xiàn)代碼

    C/C++ MD5算法的實(shí)現(xiàn)代碼

    下面就將網(wǎng)上有關(guān)MD5算法一些知識(shí)整理一下,方面自己查閱,需要的朋友可以參考下
    2017-07-07
  • 探究在C++程序并發(fā)時(shí)保護(hù)共享數(shù)據(jù)的問題

    探究在C++程序并發(fā)時(shí)保護(hù)共享數(shù)據(jù)的問題

    這篇文章主要介紹了探究在C++程序并發(fā)時(shí)保護(hù)共享數(shù)據(jù)的問題,也有利于大家更好地理解C++多線程的一些機(jī)制,需要的朋友可以參考下
    2015-07-07
  • C語言全局變量和局部變量的示例代碼

    C語言全局變量和局部變量的示例代碼

    本文主要介紹了C語言全局變量和局部變量的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-05-05
  • C++如何將vector數(shù)字寫入到txt文件中

    C++如何將vector數(shù)字寫入到txt文件中

    這篇文章主要介紹了C++如何將vector數(shù)字寫入到txt文件中問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-11-11
  • C++ assert()函數(shù)用法案例詳解

    C++ assert()函數(shù)用法案例詳解

    這篇文章主要介紹了C++ assert()函數(shù)用法案例詳解,本篇文章通過簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下
    2021-09-09

最新評(píng)論