C語言實現(xiàn)簡易通訊錄(靜態(tài)版本)的代碼分享
一、通訊錄
1.演示效果
2.完整代碼
#define _CRT_SECURE_NO_WARNINGS 1 //#include "ConTacts.h" #include <assert.h> #include <stdio.h> #define MAX 30 #define NAME 20 #define TEL 20 #define ADR 30 struct PopInfo {//聲明一個結構體用來保存?zhèn)€人信息 char name[NAME];//姓名 int age;//年齡 char tel[TEL];//手機號 char adr[ADR];//住址 }; struct Contact { struct PopInfo data[MAX];//用結構體數(shù)組來存儲多個人的信息 int sz;//用來記錄存儲幾個人的信息 }; void InitContact(struct Contact* con)//初始化聯(lián)系人信息 { assert(con); memset(con->data, 0, MAX * sizeof(struct PopInfo)); con->sz = 0; } void AddContact(struct Contact* con)//添加聯(lián)系人信息 { assert(con); printf("請輸入姓名:"); scanf("%s", con->data[con->sz].name); printf("請輸入年齡:"); scanf("%d", &(con->data[con->sz].age)); printf("請輸入手機號:"); scanf("%s", con->data[con->sz].tel); printf("請輸入住址:"); scanf("%s", con->data[con->sz].adr); con->sz++; printf("成功添加聯(lián)系人!\n"); } void ShowContact(struct Contact* con)//顯示所有聯(lián)系人信息 { int i = 0; printf("%-20s\t%-20s\t%-20s\t%-20s\t\n", "姓名", "年齡", "手機號", "住址"); for (i = 0; i < con->sz; i++) { printf("%-20s\t%-20d\t%-20s\t%-20s\t\n", con->data[i].name, con->data[i].age, con->data[i].tel, con->data[i].adr); } } int Find(struct Contact* con)//根據(jù)姓名查找存放聯(lián)系人信息的數(shù)組下標 { char name[20]; scanf("%s", name); int i = 0; for (i = 0; i < con->sz; i++) { if (strcmp(con->data[i].name, name) == 0) { return i; break; } } return -1; } void DelContact(struct Contact* con)//刪除指定聯(lián)系人信息函數(shù) { assert(con); printf("請輸入你要刪除的聯(lián)系人姓名:"); /*char name[20]; printf("請輸入你要刪除的聯(lián)系人姓名:"); scanf("%s", name); int i = 0; for (i = 0; i < con->sz; i++) { if (strcmp(con->data[i].name, name) == 0) { printf("刪除成功!\n"); con->sz--; break; } }*/ int i = Find(con); if (i == -1) { printf("未找到你要刪除的聯(lián)系人\n"); } else { int j = 0; for (j = i; j <= con->sz; j++) { con->data[j] = con->data[j + 1]; } con->sz--; printf("刪除成功!\n"); } } void ModContact(struct Contact* con)//修改聯(lián)系人信息 { assert(con); printf("請輸入你要修改的聯(lián)系人姓名:"); /*char name[20]; printf("請輸入你要修改的聯(lián)系人姓名:"); scanf("%s", name); int i = 0; for (i = 0; i < con->sz; i++) { if (strcmp(con->data[i].name, name) == 0) { printf("找到要修改的聯(lián)系人!\n"); break; } }*/ int i = Find(con); if (i == -1) { printf("未找到你要刪除的聯(lián)系人\n"); } else { printf("請輸入姓名:"); scanf("%s", con->data[i].name); printf("請輸入年齡:"); scanf("%d", &(con->data[i].age)); printf("請輸入手機號:"); scanf("%s", con->data[i].tel); printf("請輸入住址:"); scanf("%s", con->data[i].adr); printf("成功修改聯(lián)系人信息!\n"); } } void SearchContact(struct Contact* con)//查找指定聯(lián)系人 { assert(con);//斷言,con不能指向NULL printf("請輸入你要查找的聯(lián)系人姓名:"); /*char name[20]; printf("請輸入你要查找的聯(lián)系人姓名:"); scanf("%s", name); int i = 0; for (i = 0; i < con->sz; i++) { if (strcmp(con->data[i].name, name) == 0) { break; } }*/ int i = Find(con); if (i == -1) { printf("沒有找到你要查找的聯(lián)系人!\n"); } else { printf("%-20s\t%-20s\t%-20s\t%-20s\t\n", "姓名", "年齡", "手機號", "住址"); printf("%-20s\t%-20d\t%-20s\t%-20s\t\n", con->data[i].name, con->data[i].age, con->data[i].tel, con->data[i].adr); } } int cmp_name(const void* e1, const void* e2)//qsort中的比較函數(shù) { return strcmp(((struct PopInfo*)e1)->name, ((struct PopInfo*)e2)->name);//只能對字符及字符串排序,如果是數(shù)字,會出錯 } void Sort_name_Contact(struct Contact* con)//qsort函數(shù)排序 { qsort(con->data, con->sz, sizeof(struct PopInfo), cmp_name); printf("排序成功\n");//名字按照字母的ASCII碼值從小到大排序 } void ClearContact(struct Contact* con)//清空所有聯(lián)系人信息 { char str[10]; while (1) { printf("確定清空通訊錄嗎?(yes/no):"); scanf("%s", str); if (strcmp(str, "yes") == 0) { con->sz = 0; printf("已清空通訊錄!\n"); break; } else if (strcmp(str, "no") == 0) { printf("已取消!\n"); break; } else printf("輸入錯誤,請重新輸入!\n"); } } enum um {//用枚舉類型表示input的值 Exit,//Exit是常量值0,退出程序 Add,//Add是常量值1,添加聯(lián)系人信息 Del,//Del是常量值2,刪除指定聯(lián)系人信息 Mod,//3,修改聯(lián)系人信息 Search,//4,查找聯(lián)系人信息 Sort_name,//5,按照姓氏對聯(lián)系人信息排序 Show,//6,顯示聯(lián)系人信息 Clear//7,清空所有聯(lián)系人信息 }; void menu() { printf("*******************************\n"); printf("*** 1.添加聯(lián)系人信息 **\n"); printf("*** 2.刪除指定聯(lián)系人信息 **\n"); printf("*** 3.修改聯(lián)系人信息 **\n"); printf("*** 4.查找聯(lián)系人信息 **\n"); printf("*** 5.按照姓氏對聯(lián)系人排序 **\n"); printf("*** 6.顯示聯(lián)系人信息 **\n"); printf("*** 7.清空所有聯(lián)系人信息 **\n"); printf("*** 0.退出程序 **\n"); } int main() { int input = 0; struct Contact con; InitContact(&con);//初始化聯(lián)系人信息 do { menu(); printf("請選擇:"); scanf("%d", &input); switch (input) { case Add: AddContact(&con);//添加聯(lián)系人信息 break;//break僅退出switch語句,不會退出do-while循環(huán) case Del: DelContact(&con);//刪除指定聯(lián)系人信息 break; case Mod: ModContact(&con);//修改聯(lián)系人信息 break; case Search: SearchContact(&con);//查找指定聯(lián)系人 break; case Sort_name: Sort_name_Contact(&con);//按照姓氏對聯(lián)系人信息排序 break; case Show: ShowContact(&con);//顯示所有聯(lián)系人信息 break; case Clear: ClearContact(&con);//清空所有聯(lián)系人信息 break; case Exit: printf("退出程序\n"); break; default: printf("輸入錯誤,請重新選擇\n"); break; } } while (input); return 0; }
二、代碼解析
1.宏定義及結構體聲明
#include <assert.h> #include <stdio.h> #define MAX 30 #define NAME 20 #define TEL 20 #define ADR 30 struct PopInfo {//聲明一個結構體用來保存?zhèn)€人信息 char name[NAME];//姓名 int age;//年齡 char tel[TEL];//手機號 char adr[ADR];//住址 }; struct Contact { struct PopInfo data[MAX];//用結構體數(shù)組來存儲多個人的信息 int sz;//用來記錄存儲幾個人的信息 };
定義struct PopInfo結構體用來保存聯(lián)系人的信息,再定義struct Contact,用來保存多個聯(lián)系人的信息以及記錄所存儲聯(lián)系人個數(shù)。
2.主菜單函數(shù)
void menu() { printf("*******************************\n"); printf("*** 1.添加聯(lián)系人信息 **\n"); printf("*** 2.刪除指定聯(lián)系人信息 **\n"); printf("*** 3.修改聯(lián)系人信息 **\n"); printf("*** 4.查找聯(lián)系人信息 **\n"); printf("*** 5.按照姓氏對聯(lián)系人排序 **\n"); printf("*** 6.顯示聯(lián)系人信息 **\n"); printf("*** 7.清空所有聯(lián)系人信息 **\n"); printf("*** 0.退出程序 **\n"); }
3.主函數(shù)
int main() { enum um {//用枚舉類型表示input的值 Exit,//Exit是常量值0,退出程序 Add,//Add是常量值1,添加聯(lián)系人信息 Del,//Del是常量值2,刪除指定聯(lián)系人信息 Mod,//3,修改聯(lián)系人信息 Search,//4,查找聯(lián)系人信息 Sort_name,//5,按照姓氏對聯(lián)系人信息排序 Show,//6,顯示聯(lián)系人信息 Clear//7,清空所有聯(lián)系人信息 }; int input = 0; struct Contact con; InitContact(&con);//初始化聯(lián)系人信息 do { menu(); printf("請選擇:"); scanf("%d", &input); switch (input) { case Add: AddContact(&con);//添加聯(lián)系人信息 break;//break僅退出switch語句,不會退出do-while循環(huán) case Del: DelContact(&con);//刪除指定聯(lián)系人信息函數(shù) break; case Mod: ModContact(&con);//修改聯(lián)系人信息 break; case Search: SearchContact(&con);//查找指定聯(lián)系人 break; case Sort_name: Sort_name_Contact(&con);//按照姓氏對聯(lián)系人信息排序 break; case Show: ShowContact(&con);//顯示所有聯(lián)系人信息 break; case Clear: ClearContact(&con); break; case Exit: printf("退出程序\n"); break; default: printf("輸入錯誤,請重新選擇\n"); break; } } while (input); return 0; }
4.查找函數(shù)
int Find(struct Contact* con)//根據(jù)姓名查找存放聯(lián)系人信息的數(shù)組下標 { char name[20]; scanf("%s", name); int i = 0; for (i = 0; i < con->sz; i++) { if (strcmp(con->data[i].name, name) == 0) { return i; break; } } return -1; }
根據(jù)姓名查找存放聯(lián)系人信息的數(shù)組下標,用于后來查找、刪除、修改聯(lián)系人信息時使用。
5.初始化聯(lián)系人信息
void InitContact(struct Contact* con)//初始化聯(lián)系人信息 { assert(con); memset(con->data, 0, MAX * sizeof(struct PopInfo)); con->sz = 0; }
assert(con);是判斷指針con是否為空,判斷memset(con->data, 0, MAX * sizeof(struct PopInfo))是將結構體struct Contact中struct PopInfo類型的數(shù)組data置為0。con->sz = 0;是將所記錄的聯(lián)系人個數(shù)置為0。
6.添加聯(lián)系人信息
void AddContact(struct Contact* con)//添加聯(lián)系人信息 { assert(con); printf("請輸入姓名:"); scanf("%s", con->data[con->sz].name); printf("請輸入年齡:"); scanf("%d", &(con->data[con->sz].age)); printf("請輸入手機號:"); scanf("%s", con->data[con->sz].tel); printf("請輸入住址:"); scanf("%s", con->data[con->sz].adr); con->sz++; printf("成功添加聯(lián)系人!\n"); }
注意在輸入年齡的時候因為年齡是int類型,要用&符號取出地址,再進行賦值。
7.顯示所有聯(lián)系人信息
void ShowContact(struct Contact* con)//顯示所有聯(lián)系人信息 { int i = 0; printf("%-20s\t%-20s\t%-20s\t%-20s\t\n", "姓名", "年齡", "手機號", "住址"); for (i = 0; i < con->sz; i++) { printf("%-20s\t%-20d\t%-20s\t%-20s\t\n", con->data[i].name, con->data[i].age, con->data[i].tel, con->data[i].adr); } }
printf("%-20s\t%-20s\t%-20s\t%-20s\t\n", "姓名", "年齡", "手機號", "住址")中的%-20s\t%-20s\t%-20s\t%-20s\t\n是為了讓打印聯(lián)系人信息的時候更加直觀,%-20s\t%-20s\t%-20s\t%-20s\t\n中的-是為了讓其左對齊,把-去掉可以是右對齊。
8.刪除指定聯(lián)系人信息
void DelContact(struct Contact* con)//刪除指定聯(lián)系人信息函數(shù) { assert(con); printf("請輸入你要刪除的聯(lián)系人姓名:"); /*char name[20]; printf("請輸入你要刪除的聯(lián)系人姓名:"); scanf("%s", name); int i = 0; for (i = 0; i < con->sz; i++) { if (strcmp(con->data[i].name, name) == 0) { printf("刪除成功!\n"); con->sz--; break; } }*/ int i = Find(con); if (i == -1) { printf("未找到你要刪除的聯(lián)系人\n"); } else { int j = 0; for (j = i; j <= con->sz; j++) { con->data[j] = con->data[j + 1]; } con->sz--; printf("刪除成功!\n"); } }
代碼中注釋部分可以在不調(diào)用 Find函數(shù)時刪除聯(lián)系人信息,在成功刪除聯(lián)系人信息后,要將存放聯(lián)系人個數(shù)的變量進行修改。
9.修改聯(lián)系人信息
void ModContact(struct Contact* con)//修改聯(lián)系人信息 { assert(con); printf("請輸入你要修改的聯(lián)系人姓名:"); /*char name[20]; printf("請輸入你要修改的聯(lián)系人姓名:"); scanf("%s", name); int i = 0; for (i = 0; i < con->sz; i++) { if (strcmp(con->data[i].name, name) == 0) { printf("找到要修改的聯(lián)系人!\n"); break; } }*/ int i = Find(con); if (i == -1) { printf("未找到你要刪除的聯(lián)系人\n"); } else { printf("請輸入姓名:"); scanf("%s", con->data[i].name); printf("請輸入年齡:"); scanf("%d", &(con->data[i].age)); printf("請輸入手機號:"); scanf("%s", con->data[i].tel); printf("請輸入住址:"); scanf("%s", con->data[i].adr); printf("成功修改聯(lián)系人信息!\n"); } }
10.查找指定聯(lián)系人
void SearchContact(struct Contact* con)//查找指定聯(lián)系人 { assert(con);//斷言,con不能指向NULL printf("請輸入你要查找的聯(lián)系人姓名:"); /*char name[20]; printf("請輸入你要查找的聯(lián)系人姓名:"); scanf("%s", name); int i = 0; for (i = 0; i < con->sz; i++) { if (strcmp(con->data[i].name, name) == 0) { break; } }*/ int i = Find(con); if (i == -1) { printf("沒有找到你要查找的聯(lián)系人!\n"); } else { printf("%-20s\t%-20s\t%-20s\t%-20s\t\n", "姓名", "年齡", "手機號", "住址"); printf("%-20s\t%-20d\t%-20s\t%-20s\t\n", con->data[i].name, con->data[i].age, con->data[i].tel, con->data[i].adr); } }
11.按照姓氏對聯(lián)系人信息排序
int cmp_name(const void* e1, const void* e2)//qsort中的比較函數(shù) { return strcmp(((struct PopInfo*)e1)->name, ((struct PopInfo*)e2)->name);//只能對字符及字符串排序,如果是數(shù)字,會出錯 } void Sort_name_Contact(struct Contact* con)//qsort函數(shù)排序 { qsort(con->data, con->sz, sizeof(struct PopInfo), cmp_name); printf("排序成功\n");//名字按照字母的ASCII碼值從小到大排序 }
使用 qsort 函數(shù)對其進行排序。
12.清空所有聯(lián)系人信息
void ClearContact(struct Contact* con)//清空所有聯(lián)系人信息 { char str[10]; while (1) { printf("確定清空通訊錄嗎?(yes/no):"); scanf("%s", str); if (strcmp(str, "yes") == 0) { con->sz = 0; printf("已清空通訊錄!\n"); break; } else if (strcmp(str, "no") == 0) { printf("已取消!\n"); break; } else printf("輸入錯誤,請重新輸入!\n"); } }
到此這篇關于C語言實現(xiàn)簡易通訊錄(靜態(tài)版本)的代碼分享的文章就介紹到這了,更多相關C語言靜態(tài)通訊錄內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
C生萬物C語言宏將整數(shù)二進制位的奇偶數(shù)位交換
這篇文章主要為大家介紹了C生萬物C語言使用宏將整數(shù)二進制位的奇偶數(shù)位交換示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-02-02C++實現(xiàn)十進制數(shù)轉換為二進制數(shù)的數(shù)學算法
這篇文章和大家分享一下我個人對十進制數(shù)轉換為二進制數(shù)的想法,目前暫時更新只整數(shù)十進制的轉換,后續(xù)會更新帶有小數(shù)的進制轉換,代碼使用c++實現(xiàn)2021-09-09