C語言實現(xiàn)通訊錄的方法(包括靜態(tài)版本和動態(tài)版本)
1.靜態(tài)通訊錄的實現(xiàn)
實現(xiàn)的方法:
我們采用的方法就是工程形勢,實現(xiàn)將功能和定義以及測試分成三個文件,其中定義放在.h文件,實現(xiàn)和測試放在.c文件當(dāng)中。
(1)contact.h文件的基本實現(xiàn):
#pragma once//防止頭文件重復(fù)定義 #define NAME_MAX 20 #define SEX_MAX 5 #define TELE_MAX 12 #define ADDR_MAX 30 #define MAX 1000 //靜態(tài)通訊錄,最大為1000 #include<stdio.h> #include<string.h> struct PeoInfo { char name[NAME_MAX]; int age; char sex[SEX_MAX]; char tele[TELE_MAX]; char addr[ADDR_MAX]; }; //靜態(tài)的版本 struct Contact { struct PeoInfo data[MAX];//容量為1000的通訊錄 int sz; //已經(jīng)使用了多少個通訊錄 }; //初始化通訊錄 void InitContact(struct Contact* pc); //通訊錄的增加 void PushContact(struct Contact* pc); //通訊錄的刪除: void PopContact(struct Contact* pc); //通訊錄的打印 void ShowContact(const struct Contact* pc); //通過名字來查找,找到返回下標(biāo),找不到返回-1。 int FindContactByName(const struct Contact* pc, const char* name); //通訊的查找并打印 void SearchContact(const struct Contact* pc); //修改聯(lián)系人的信息 void ModContact(struct Contact* pc); //以名字排序 void SortByNameContact(struct Contact* pc); //清空所有聯(lián)系人 void DestroyContact(struct Contact* pc);
(2)contact.c文件的基本實現(xiàn)
#include"contact.h" //通訊錄的初始化 void InitContact(struct Contact* pc) { pc->sz = 0; memset(pc->data, 0, sizeof(struct PeoInfo) * MAX); } //通訊錄增加 void PushContact(struct Contact* pc) { if (pc->sz >= MAX) { printf("This contact is full!\n"); } printf("please input name:>"); scanf("%s", &pc->data[pc->sz].name); printf("please input age:>"); scanf("%d", &pc->data[pc->sz].age); printf("please input sex:>"); scanf("%s", pc->data[pc->sz].sex); printf("please input tele:>"); scanf("%s", pc->data[pc->sz].tele); printf("please input addr:>"); scanf("%s", pc->data[pc->sz].addr); printf("Add successful\n"); pc->sz++; } //通過名字來查找,找到返回下標(biāo),找不到返回-1。 int FindContactByName(const struct Contact* pc, const char* ThisName) { int i = 0; for (i = 0; i < pc->sz; i++) { if (strcmp(ThisName, pc->data[i].name) == 0) { return i; } } return -1; } //通訊錄的打印 void ShowContact(const struct Contact* pc) { printf("%15s\t%5s\t%8s\t%15s\t%20s\n", "name", "age","sex","tele","addr"); int i = 0; for (i = 0; i < pc->sz; i++) { printf("%15s\t%5d\t%8s\t%15s\t%20s\n", pc->data[i].name, pc->data[i].age, pc->data[i].sex, pc->data[i].tele, pc->data[i].addr); } } //通訊錄的刪除: void PopContact(struct Contact* pc) { if (pc->sz == 0) { printf("Address book is empty. Can Not be deleted\n"); return; } char name[NAME_MAX] = { 0 }; printf("Please enter the name of the person you want to delete:>"); scanf("%s", name); //1.查找 int pos = FindContactByName(pc, name); if ( pos == -1) { printf("There's no one by that name!\n"); } else { //2.刪除 int j = 0; for (j = pos; j < pc->sz - 1; j++) { pc->data[j] = pc->data[j + 1]; } printf("Delection successful\n"); } pc->sz--; } //通訊的查找 void SearchContact(const struct Contact* pc) { char name[NAME_MAX] = { 0 }; printf("please input name of you want to search:>"); scanf("%s", name); int pos = FindContactByName(pc, name); if (pos == -1) { printf("No this one!\n"); } else { printf("%15s\t%5s\t%8s\t%15s\t%20s\n", "name", "age", "sex", "tele", "addr"); printf("%15s\t%5d\t%8s\t%15s\t%20s\n", pc->data[pos].name, pc->data[pos].age, pc->data[pos].sex, pc->data[pos].tele, pc->data[pos].addr); } } //修改聯(lián)系人的信息 void ModContact(struct Contact* pc) { char name[NAME_MAX] = { 0 }; printf(" Please enter the name of the person you want to modify : > "); scanf("%s", name); int pos = FindContactByName(pc, name); if (pos == -1) { printf("No this one\n"); } else { printf("please input new name:>"); scanf("%s", &pc->data[pos].name); printf("please input new age:>"); scanf("%d", &pc->data[pos].age); printf("please input new sex:>"); scanf("%s", pc->data[pos].sex); printf("please input new tele:>"); scanf("%s", pc->data[pos].tele); printf("please input new addr:>"); scanf("%s", pc->data[pos].addr); printf("Modify successful\n"); } } //以名字排序 void SortByNameContact(struct Contact* pc) { if (pc->sz == 0 || pc->sz == 1) { printf("Too few contacts to sort\n"); } int i, j; for (i = 0; i < pc->sz ; i++) { int flag = 0; for (j = 0; j < pc->sz - 1 - i; j++) { if (strcmp(pc->data[j].name, pc->data[j + 1].name) > 0) { struct PeoInfo tmp = pc->data[j]; pc->data[j] = pc->data[j + 1]; pc->data[j + 1] = tmp; flag = 1; } } if (flag == 0) { break; } } printf("Sort successful\n"); } //清空所有聯(lián)系人 void DestroyContact(struct Contact* pc) { pc->sz = 0; printf("Destroy successful\n"); }
(3)test.c文件的實現(xiàn)
#define _CRT_SECURE_NO_WARNINGS 1 #include"contact.h" void menu() { printf("************************************\n"); printf("****** 1.add 2.del *******\n"); printf("****** 3.search 4.modify *******\n"); printf("****** 5.show 6.sort *******\n"); printf("****** 7.destroy 0.exit *******\n"); printf("************************************\n"); } enum Option//使用枚舉,增加代碼的可讀性 { EXIT, ADD, DEL, SEARCH, MODIFY, SHOW, SORT, DESTROY, }; int main() { int input; struct Contact con; //初始化通訊錄 InitContact(&con); do { menu(); printf("please select:>\n"); scanf("%d", &input); switch (input) { case EXIT: printf("exit succseeful!\n"); break; case ADD: PushContact(&con); break; case DEL: PopContact(&con); break; case SEARCH: SearchContact(&con); break; case MODIFY: ModContact(&con); break; case SHOW: ShowContact(&con); break; case SORT: SortByNameContact(&con); break; case DESTROY: DestroyContact(&con); break; default: printf("Select error, please select again!\n"); } } while (input); return 0; }
2.動態(tài)通訊錄的實現(xiàn)
實現(xiàn)的方法:
和靜態(tài)實現(xiàn)細節(jié)都差不多,只是靜態(tài)使用的數(shù)組,所以固定了通訊錄的大小,不能超出通訊錄大小的限制,且沒有使用完還會造成空間的浪費。所以使用動態(tài)內(nèi)存分配來實現(xiàn)動態(tài)的通訊錄。會節(jié)省空間并且是通訊錄的大小變得靈活。
(1)contact.h文件的基本實現(xiàn):
#pragma once//防止頭文件重復(fù)定義 #define NAME_MAX 20 #define SEX_MAX 5 #define TELE_MAX 12 #define ADDR_MAX 30 #define DEFINE_SZ 3 #include<stdio.h> #include<string.h> #include<stdlib.h> struct PeoInfo { char name[NAME_MAX]; int age; char sex[SEX_MAX]; char tele[TELE_MAX]; char addr[ADDR_MAX]; }; //動態(tài)的版本 struct Contact { struct PeoInfo* data; int sz;//通訊錄已經(jīng)使用元素的個數(shù) int capacity; //當(dāng)前的最大容量 }; //初始化通訊錄 void InitContact(struct Contact* pc); //通訊錄的增加 void PushContact(struct Contact* pc); //通訊錄的刪除: void PopContact(struct Contact* pc); //通訊錄的打印 void ShowContact(const struct Contact* pc); //通過名字來查找,找到返回下標(biāo),找不到返回-1。 int FindContactByName(const struct Contact* pc, const char* name); //通訊的查找并打印 void SearchContact(const struct Contact* pc); //修改聯(lián)系人的信息 void ModContact(struct Contact* pc); //以名字排序 void SortByNameContact(struct Contact* pc); //清空所有聯(lián)系人 void DestroyContact(struct Contact* pc);
(2)contact.c文件的基本實現(xiàn)
#include"contact.h" //通訊錄的初始化 void InitContact(struct Contact* pc) { pc->sz = 0; pc->data = (struct PeoInfo*)malloc(sizeof(struct PeoInfo) * DEFINE_SZ); if (pc->data == NULL) { printf("malloc fail\n"); return; } pc->capacity = DEFINE_SZ; } //通訊錄增加 void PushContact(struct Contact* pc) { if (pc->sz == pc->capacity) { //增容: struct PeoInfo* ptr = (struct PeoInfo*)realloc(pc->data, sizeof(struct PeoInfo) * (pc->capacity + 2)); if (ptr == NULL) { printf("realloc fail\n"); return; } else { pc->data = ptr; pc->capacity += 2; printf("Compatibilization successful\n"); } } //錄入新增成員信息: printf("please input name:>"); scanf("%s", &pc->data[pc->sz].name); printf("please input age:>"); scanf("%d", &pc->data[pc->sz].age); printf("please input sex:>"); scanf("%s", pc->data[pc->sz].sex); printf("please input tele:>"); scanf("%s", pc->data[pc->sz].tele); printf("please input addr:>"); scanf("%s", pc->data[pc->sz].addr); printf("Add successful\n"); pc->sz++; } //通過名字來查找,找到返回下標(biāo),找不到返回-1。 int FindContactByName(const struct Contact* pc, const char* ThisName) { int i = 0; for (i = 0; i < pc->sz; i++) { if (strcmp(ThisName, pc->data[i].name) == 0) { return i; } } return -1; } //通訊錄的打印 void ShowContact(const struct Contact* pc) { printf("%15s\t%5s\t%8s\t%15s\t%20s\n", "name", "age", "sex", "tele", "addr"); int i = 0; for (i = 0; i < pc->sz; i++) { printf("%15s\t%5d\t%8s\t%15s\t%20s\n", pc->data[i].name, pc->data[i].age, pc->data[i].sex, pc->data[i].tele, pc->data[i].addr); } } //通訊錄的刪除: void PopContact(struct Contact* pc) { if (pc->sz == 0) { printf("Address book is empty. Can Not be deleted\n"); return; } char name[NAME_MAX] = { 0 }; printf("Please enter the name of the person you want to delete:>"); scanf("%s", name); //1.查找 int pos = FindContactByName(pc, name); if (pos == -1) { printf("There's no one by that name!\n"); } else { //2.刪除 int j = 0; for (j = pos; j < pc->sz - 1; j++) { pc->data[j] = pc->data[j + 1]; } printf("Delection successful\n"); } pc->sz--; } //通訊的查找 void SearchContact(const struct Contact* pc) { char name[NAME_MAX] = { 0 }; printf("please input name of you want to search:>"); scanf("%s", name); int pos = FindContactByName(pc, name); if (pos == -1) { printf("No this one!\n"); } else { printf("%15s\t%5s\t%8s\t%15s\t%20s\n", "name", "age", "sex", "tele", "addr"); printf("%15s\t%5d\t%8s\t%15s\t%20s\n", pc->data[pos].name, pc->data[pos].age, pc->data[pos].sex, pc->data[pos].tele, pc->data[pos].addr); } } //修改聯(lián)系人的信息 void ModContact(struct Contact* pc) { char name[NAME_MAX] = { 0 }; printf(" Please enter the name of the person you want to modify : > "); scanf("%s", name); int pos = FindContactByName(pc, name); if (pos == -1) { printf("No this one\n"); } else { printf("please input new name:>"); scanf("%s", &pc->data[pos].name); printf("please input new age:>"); scanf("%d", &pc->data[pos].age); printf("please input new sex:>"); scanf("%s", pc->data[pos].sex); printf("please input new tele:>"); scanf("%s", pc->data[pos].tele); printf("please input new addr:>"); scanf("%s", pc->data[pos].addr); printf("Modify successful\n"); } } //以名字排序 void SortByNameContact(struct Contact* pc) { if (pc->sz == 0 || pc->sz == 1) { printf("Too few contacts to sort\n"); } int i, j; for (i = 0; i < pc->sz; i++) { int flag = 0; for (j = 0; j < pc->sz - 1 - i; j++) { if (strcmp(pc->data[j].name, pc->data[j + 1].name) > 0) { struct PeoInfo tmp = pc->data[j]; pc->data[j] = pc->data[j + 1]; pc->data[j + 1] = tmp; flag = 1; } } if (flag == 0) { break; } } printf("Sort successful\n"); } //清空所有聯(lián)系人 void DestroyContact(struct Contact* pc) { pc->sz = 0; pc->capacity = 0; free(pc->data); pc->data = NULL; printf("Destroy successful\n"); }
(3)test.c文件的實現(xiàn)
#include"contact.h" void menu() { printf("************************************\n"); printf("****** 1.add 2.del *******\n"); printf("****** 3.search 4.modify *******\n"); printf("****** 5.show 6.sort *******\n"); printf("****** 7.destroy 0.exit *******\n"); printf("************************************\n"); } enum Option { EXIT, ADD, DEL, SEARCH, MODIFY, SHOW, SORT, DESTROY, }; int main() { int input; struct Contact con; //初始化通訊錄 InitContact(&con); do { menu(); printf("please select:>\n"); scanf("%d", &input); switch (input) { case EXIT: DestroyContact(&con); printf("exit succseeful!\n"); break; case ADD: PushContact(&con); break; case DEL: PopContact(&con); break; case SEARCH: SearchContact(&con); break; case MODIFY: ModContact(&con); break; case SHOW: ShowContact(&con); break; case SORT: SortByNameContact(&con); break; case DESTROY: DestroyContact(&con); break; default: printf("Select error, please select again!\n"); } } while (input); return 0; }
3.總結(jié)
其實通訊錄的實現(xiàn)就是數(shù)據(jù)結(jié)構(gòu)的一種體現(xiàn),我們需要學(xué)的東西還有很多,請大家一起跟我努力吧??!
再就是有問題請大家及時指正?。?!謝謝大家。
到此這篇關(guān)于C語言實現(xiàn)通訊錄的方法(包括靜態(tài)版本和動態(tài)版本)的文章就介紹到這了,更多相關(guān)C語言實現(xiàn)通訊錄內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
一篇文章教你用C語言模擬實現(xiàn)字符串函數(shù)
這篇文章主要介紹了C語言模擬實現(xiàn)字符串函數(shù),開發(fā)程序的時候經(jīng)常使用到一些字符串函數(shù),例如求字符串長度,拷貝字符串……,需要的朋友可以參考下2021-09-09C++中調(diào)用復(fù)制(拷貝)函數(shù)的三種情況總結(jié)
這篇文章主要介紹了C++中調(diào)用復(fù)制(拷貝)函數(shù)的三種情況總結(jié),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-11-11C語言實現(xiàn)學(xué)籍信息管理系統(tǒng)
這篇文章主要為大家詳細介紹了C語言實現(xiàn)學(xué)籍信息管理系統(tǒng),文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-07-07