網(wǎng)站首頁 編程語言 正文
本文實(shí)例為大家分享了C語言實(shí)現(xiàn)通訊錄系統(tǒng)的具體代碼,供大家參考,具體內(nèi)容如下
設(shè)計(jì)要求
1.單位、個人信息查詢
2.打開、寫入保存這些信息的文件
完整代碼
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct Userinfo // 定義結(jié)構(gòu)體類型;封裝個人的信息
{
? ? char name[20]; // 用戶姓名
? ? char sex[2]; // 性別
? ? char cname[20]; // 單位
? ? char mobileNumber[11]; // 手機(jī)
} Userinfo;
typedef struct Companyinfo // 定義結(jié)構(gòu)體類型;封裝個人的信息
{
? ? char companyname[20]; // 姓名
? ? char companyaddress[200]; // 單位地址
? ? char telphone[8]; // 電話
} Companyinfo;
? ? int count = 0; //系統(tǒng)中的現(xiàn)有人數(shù)
? ? int countu = 0; //系統(tǒng)中的現(xiàn)有人數(shù)
void insertuserinfo(Userinfo *userinfo, int *countu)
/*添加聯(lián)系人信息*/
{
?? ?printf("請輸入要添加人的姓名:> ");
?? ?scanf("%s", (userinfo + (*countu))->name);
//flag:
?? ?printf("請輸入要添加人的性別(男/女):> ");
?? ?scanf("%s", (userinfo + (*countu))->sex);
?? ?printf("請輸入要添加人的工作單位:> ");
?? ?scanf("%s", &(userinfo + (*countu))->cname);
?? ?printf("請輸入要添加的電話:> ");
?? ?scanf("%s", (userinfo + (*countu))->mobileNumber);
?? ?printf("添加成功!\n");
?? ?(*countu)++;/*已有人數(shù)加1*/
}
void insertcompanyinfo(Companyinfo *companyinfo, int *count)
/*添加單位信息*/
{
?? ?printf("請輸入要添加單位的名稱:> ");
?? ?scanf("%s", (companyinfo + (*count))->companyname);
?? ?printf("請輸入要添加單位的地址:> ");
?? ?scanf("%s", &(companyinfo + (*count))->companyaddress);
?? ?printf("請輸入要添加單位的電話:> ");
?? ?scanf("%s", (companyinfo + (*count))->telphone);
?? ?printf("添加成功!\n");
?? ?(*count)++;/*已有人數(shù)加1*/
}
void DeleteUserinfo(Userinfo *userinfo, int *countu)
/*刪除指定聯(lián)系人信息*/
{
?? ?char _name[20];
?? ?if ((*countu) <= 0)
?? ?{
?? ??? ?printf("此系統(tǒng)中還沒有人員信息!\n");
?? ??? ?return;
?? ?}
?? ?printf("請輸入您要刪除人員的姓名:> ");
?? ?scanf("%s", _name);
?? ?for (int i = 0; i < (*countu); i++)
?? ?{
?? ??? ?if (strcmp((userinfo + i)->name, _name) == 0)
?? ??? ?{
?? ??? ??? ?for (int j = i; j < (*countu) - 1; j++)
?? ??? ??? ?{
?? ??? ??? ??? ?strcpy((userinfo + j)->name, (userinfo + j + 1)->name);
?? ??? ??? ??? ?strcpy((userinfo + j)->sex, (userinfo + j + 1)->sex);
?? ??? ??? ??? ?strcpy((userinfo + j)->cname ,(userinfo + j + 1)->cname);
?? ??? ??? ??? ?strcpy((userinfo + j)->mobileNumber, (userinfo + j + 1)->mobileNumber);
?? ??? ??? ?}
?? ??? ??? ?(*countu)--;
?? ??? ??? ?printf("刪除成功!\n");
?? ??? ??? ?return;
?? ??? ?}/*if*/
?? ?}/*for*/
?? ?printf("當(dāng)前系統(tǒng)中沒有此人!\n");
}
void DeleteCompanyinfo(Companyinfo *companyinfo, int *count)
/*刪除指定單位信息*/
{
?? ?char _name[20];
?? ?if ((*count) <= 0)
?? ?{
?? ??? ?printf("此系統(tǒng)中還沒有單位信息!\n");
?? ??? ?return;
?? ?}
?? ?printf("請輸入您要刪除單位名稱:> ");
?? ?scanf("%s", _name);
?? ?for (int i = 0; i < (*count); i++)
?? ?{
?? ??? ?if (strcmp((companyinfo + i)->companyname, _name) == 0)
?? ??? ?{
?? ??? ??? ?for (int j = i; j < (*count) - 1; j++)
?? ??? ??? ?{
?? ??? ??? ??? ?strcpy((companyinfo + j)->companyname, (companyinfo + j + 1)->companyname);
?? ??? ??? ??? ?strcpy((companyinfo + j)->companyaddress, (companyinfo + j + 1)->companyaddress);
?? ??? ??? ??? ?strcpy((companyinfo + j)->telphone, (companyinfo + j + 1)->telphone);
?? ??? ??? ?}
?? ??? ??? ?(*count)--;
?? ??? ??? ?printf("刪除成功!\n");
?? ??? ??? ?return;
?? ??? ?}/*if*/
?? ?}/*for*/
?? ?printf("當(dāng)前系統(tǒng)中沒有此單位!\n");
}
void Search(const Companyinfo *companyinfo, const int count)
/*查找指定單位信息*/
{
?? ?char _name[20];
?? ?printf("請輸入您要查找的單位名稱:> ");
?? ?scanf("%s", _name);
?? ?for (int i = 0; i < count; i++)
?? ?{
?? ??? ?if (strcmp((companyinfo + i)->companyname, _name) == 0)
?? ??? ?{
?? ??? ??? ?printf("*********=======您查單位信息為=======*********\n");
?? ??? ??? ?printf(" ? ?********* ? 單位名稱:> %s\n", (companyinfo + i)->companyname);
?? ??? ??? ?printf(" ? ?********* ? 單位地址:> %s\n", (companyinfo + i)->companyaddress);
?? ??? ??? ?printf(" ? ?********* ? 單位電話:> %s\n", (companyinfo + i)->telphone);
?? ??? ??? ?return;
?? ??? ?}
?? ?}/*for*/
?? ?printf("沒有找到您要查找的單位!\n");
}
void SearchUser(const Userinfo *userinfo, const int countu)
/*查找指定聯(lián)系人信息*/
{
?? ?char _name[20];
?? ?printf("請輸入您要查找人的信息:> ");
?? ?scanf("%s", _name);
?? ?for (int i = 0; i < countu; i++)
?? ?{
?? ??? ?if (strcmp((userinfo + i)->name, _name) == 0)
?? ??? ?{
?? ??? ??? ?printf("*********=======您查個人信息為=======*********\n");
?? ??? ??? ?printf(" ? ?********* ? 姓名:> %s\n", (userinfo + i)->name);
?? ??? ??? ?printf(" ? ?********* ? 性別:> %s\n", (userinfo + i)->sex);
?? ??? ??? ?printf(" ? ?********* ? 單位:> %d\n", (userinfo + i)->cname);
?? ??? ??? ?printf(" ? ?********* ? 電話:> %d\n", (userinfo + i)->mobileNumber);
?? ??? ??? ?return;
?? ??? ?}
?? ?}/*for*/
?? ?printf("沒有找到您要查找的人員!\n");
}
void Alter(Companyinfo *companyinfo, const int count)
/*修改指定單位信息*/
{
?? ?char _name[20];
?? ?printf("請輸入您要修改的單位的名稱:> ");
?? ?scanf("%s", _name);
?? ?for (int i = 0; i < count; i++)
?? ?{
?? ??? ?if (strcmp((companyinfo + i)->companyname, _name) == 0)
?? ??? ?{
?? ??? ??? ?printf("請輸入修改后的單位名稱:> ");
?? ??? ??? ?scanf("%s", (companyinfo + i)->companyname);
?? ??? ??? ?printf("請輸入修改后的單位地址:> ");
?? ??? ??? ?scanf("%s", (companyinfo + i)->companyaddress);
?? ??? ??? ?printf("請輸入修改后的單位電話:> ");
?? ??? ??? ?scanf("%s", (companyinfo + i)->telphone);
?? ??? ??? ?printf("修改成功!\n");
?? ??? ??? ?return;
?? ??? ?}
?? ?}/*for*/
?? ?printf("沒有找到您要查找的單位!\n");
}
void Show(const Companyinfo *companyinfo, const int count)
/*顯示所有單位信息*/
{
?? ?if (count == 0)
?? ?{
?? ??? ?printf("沒有找到您要查找的單位!\n");
?? ?}
?? ?else
?? ?{
?? ??? ?for (int i = 0; i < count; i++)
?? ??? ?{
?? ??? ??? ?printf("%5s ? ?|%13s ?|%s\n", (companyinfo + i)->companyname, (companyinfo + i)->telphone, (companyinfo + i)->companyaddress);
?? ??? ?}
?? ?}
}
void OpenFile()
{
? ?FILE *fp = NULL;
? ?char buff[255];
? ?fp = fopen("/Teldict.txt", "r");
? ?printf("打開文件名:Teldict.text \n");
? ?printf("內(nèi)容如下:\n");
? ?fgets(buff, 255, (FILE*)fp);
? ?printf("1: %s\n", buff );
? ?fclose(fp);
}
void WriteFile()
{
? char s[100];
? ?FILE *fp = NULL;
? ?fp = fopen("/Teldict.txt", "w+");
? ? ? ? ? ? printf("請輸入寫入文件的內(nèi)容: ");
? ? ? ? ? ? scanf("%s",&s);
? ?fprintf(fp,s);
? ?fputs(s, fp);
?// ?fputs("This is testing for fputs...\n", fp);
? ?fclose(fp);
}
int StcCmp(const void*num1, const void *num2)
/*快排的比較函數(shù)*/
{
?? ?return (strcmp(((Companyinfo *)num1)->companyname, ((Companyinfo *)num2)->companyname) > 0) ? 1 : -1;
}
int switchuserinfo(Userinfo *userinfo)
{
? ? int b;
? ? ? ? ? ? printf("\n");
? ? ? ? ? ? printf("1)新建個人信息\n");
? ? ? ? ? ? printf("2)修改個人信息\n");
? ? ? ? ? ? printf("3)刪除個人信息\n");
? ? ? ? ? ? printf("4)返回上一菜單\n");
? ? ? ? ? ? printf("請選擇上面序號進(jìn)行相應(yīng)的操作: ");
? ? ? ? ? ? scanf("%d",&b);
? ? ? ? ? ? switch(b)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? case 1:
? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? insertuserinfo(userinfo,&countu);
? ? ? ? ? ? ? ? ? ? ? break;
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? case 2:
? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? void Alter(Companyinfo *companyinfo, const int count);
? ? ? ? ? ? ? ? ? ? ? break;
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? case 3:
? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? void Alter(Companyinfo *companyinfo, const int count);
? ? ? ? ? ? ? ? ? ? ? break;
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? case 4:break;
? ? ? ? ? ? }
? ?return 0;
}
int switchcompanyinfo(Companyinfo *companyinfo)
{
? ? ? int b;
? ? ? ? ? ? printf("\n");
? ? ? ? ? ? printf("1)新建單位信息\n");
? ? ? ? ? ? printf("2)修改單位信息\n");
? ? ? ? ? ? printf("3)刪除單位信息\n");
? ? ? ? ? ? printf("4)查詢單位信息\n");
? ? ? ? ? ? printf("5)顯示單位信息\n");
? ? ? ? ? ? printf("6)返回上一菜單\n");
? ? ? ? ? ? printf("請選擇上面序號進(jìn)行相應(yīng)的操作: ");
? ? ? ? ? ? scanf("%d",&b);
? ? ? ? ? ? switch(b)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? case 1:
? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? insertcompanyinfo(companyinfo,&count);
? ? ? ? ? ? ? ? ? ? ? break;
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? case 2:
? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? Alter(companyinfo,count);
? ? ? ? ? ? ? ? ? ? ? break;
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? case 3:
? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? DeleteCompanyinfo(companyinfo,&count);
? ? ? ? ? ? ? ? ? ? ? break;
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? case 4:
? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? Search(companyinfo,count);
? ? ? ? ? ? ? ? ? ? ? break;
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? case 5:
? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? Show(companyinfo,count);
? ? ? ? ? ? ? ? ? ? ? break;
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? case 6:break;
? ? ? ? ? ? }
?return 0;
};
int main()
{
Userinfo userinfo[10];
Companyinfo companyinfo[10];
int inputkey;
printf("歡迎使用電話薄查詢系統(tǒng)!\n\n");
qq:
? ?printf("請輸入登錄賬號:");
? ?scanf("%d",&inputkey);
? int input = 1;
if (inputkey==1001)
?{
?? ?while (input)
?? ?{
?? ? ?printf("\n");
? ? ?printf("請選擇對應(yīng)序號:(1) 單位信息管理; (2)個人信息管理 ?(3)重新登錄 ?(4)退出 :");
? ? ?scanf("%d",&input);
?? ??? ?switch (input)
?? ??? ?{
?? ? ? ? ?case 1: ? ? ?//單位信息管理
?? ??? ??? ? switchcompanyinfo(companyinfo);
?? ??? ??? ? break;
?? ? ? ? ?case 2: ? ? ?//個人信息管理
?? ??? ??? ? switchuserinfo(userinfo);
?? ??? ??? ? break;
?? ? ? ? ?case 3: ? ? ?//
?? ??? ??? ? goto qq;
?? ??? ??? ? break;
?? ??? ? ?case 4:
?? ??? ??? ?printf("感謝您試用本服務(wù)系統(tǒng),歡迎您的下次使用!\n");
?? ??? ??? ?system("pause");
?? ??? ? ? ?return 0;
? ? ?? ?};//while
? ? ?}
?}
? else
? ? {
? ?? ?while (input)
?? ?{
?? ? ? ?printf("請選擇操作序號 :\n\n");
?? ? ? ?printf("1)單位查詢\n");
?? ? ? ?printf("2)個人信息查詢\n");
?? ? ? ?printf("3)打開文件\n");
?? ? ? ?printf("4)寫入文件\n");
?? ? ? ?printf("5)重新登錄\n");
?? ? ? ?printf("6)退出\n");
? ? ?scanf("%d",&input);
?? ??? ?switch (input)
?? ??? ?{
?? ? ? ? ?case 1: ? ? ?//單位查詢
?? ??? ??? ? Search(companyinfo, count);
?? ??? ??? ? break;
?? ? ? ? ?case 2: ? ? ?//個人信息查詢
?? ??? ??? ? SearchUser(userinfo, countu);
?? ??? ??? ? break;
?? ? ? ? ?case 3: ? ? ?//打開文件
?? ??? ??? ? OpenFile();
?? ??? ??? ? break;
?? ? ? ? ?case 4: ? ? ?//寫入文件
?? ??? ??? ? WriteFile();
?? ??? ??? ? break;
? ? ? ? ? case 5 :
? ? ? ? ? ? goto qq; break;
?? ??? ? ?case 6:
?? ??? ??? ?printf("感謝您試用本服務(wù)系統(tǒng),歡迎您的下次使用!\n");
?? ??? ??? ?system("pause");
?? ??? ? ? ?return 0;
? ? ?? ?};//while
?? ?}
? ? }
? printf("\n");
?? ?system("pause");
? return 0;
}
運(yùn)行結(jié)果
本代碼設(shè)置的登錄賬號是1,當(dāng)然你也可以進(jìn)行修改。
原文鏈接:https://blog.csdn.net/weixin_55764157/article/details/122239350
相關(guān)推薦
- 2024-07-15 SpringBoot使用Apache Poi導(dǎo)出word文檔
- 2022-05-11 Python實(shí)現(xiàn)圖書管理系統(tǒng)設(shè)計(jì)_python
- 2022-03-30 pytorch中的dataset用法詳解_python
- 2022-03-24 .Net?Core服務(wù)治理Consul使用服務(wù)發(fā)現(xiàn)_自學(xué)過程
- 2022-10-21 K8s解決主機(jī)重啟后kubelet無法自動啟動問題(推薦)_云其它
- 2022-07-12 Linux中xargs命令的用法
- 2022-05-13 ffmpeg+pyqt5簡單實(shí)現(xiàn)一個抽幀可視化小工具
- 2022-06-08 FreeRTOS實(shí)時操作系統(tǒng)在Cortex-M3上的移植過程_操作系統(tǒng)
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支