C語言通訊錄實例分享
更新時間:2018年02月15日 09:42:02 作者:StevenLoveMaggie
這篇文章主要為大家分享了C語言通訊錄實例,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了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é)習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
探究在C++程序并發(fā)時保護共享數(shù)據(jù)的問題
這篇文章主要介紹了探究在C++程序并發(fā)時保護共享數(shù)據(jù)的問題,也有利于大家更好地理解C++多線程的一些機制,需要的朋友可以參考下2015-07-07

