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

用C語言實現通訊錄

 更新時間:2022年06月07日 10:47:23   作者:一零 柒  
這篇文章主要為大家詳細介紹了用C語言實現通訊錄,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下

相信大家都見識過通訊錄,通訊錄里面一般有的內容為姓名,聯(lián)系方式,年齡等信息。通訊錄的主要功能有:增加、刪除、查找、排序、修改、展示等操作。

那么具體應該用C語言怎么實現這個呢?接下來我們大家一起來看一下,一起用前一段時間所學習的C語言相關知識來看看!

1. 先定義主函數

Contact.h

里面聲明通訊錄所要實現的功能,第一次的功能可能考慮不全。沒關系,可以在想要某個功能時再添加即可。

#define _CRT_SECURE_NO_WARNINGS 1
#ifndef __CONTACT_H_
#define __CONTACT_H_

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

#define DEFAULT_SZ 3//初始通訊錄大小

enum CHOOSE//選擇功能
{
? ? EXIT,
? ? ADD,
? ? DEL,
? ? SHOW,
? ? FIND,
? ? SORT,
? ? MODIFY,
};
typedef struct CONTACT//結構體:每個成員的信息
{
? ? char name[20];
? ? int age;
? ? char phone[12];
}PeoInfo;

typedef struct Contact//結構體:將成員信息封裝
{
? ? PeoInfo* data; ?//柔性指針
? ? int count; ?//當前個數
? ? int capacity;//最大容量
}Contact, *pContact;

//通訊錄的主要功能
void AddCintact(pContact pc);//增加信息
void InitContact(pContact pc);//初始化通訊錄
void ShowContact(pContact pc);//展示通訊錄
void DelContact(pContact pc);//刪除通訊錄
void FindContact(pContact pc);//查找
void DestroyContact(pContact pc);//銷毀通訊錄
void ModifyContact(pContact pc);//修改通訊錄信息
void SortContact(pContact pc);//排序通訊錄

#endif //CONTACT

2.接下來就是通訊錄的選擇界面

main.c

里面主要是該通訊錄的選擇界面。

#include"Contact.h"
#include"bubble_sort.h"

void menu()
{
? ? int choose = 0;
? ? printf("+-------------------------------+\n");
? ? printf("| ? 1.Add ? ? ? ?2.Del ? ? ? ? ?|\n");
? ? printf("| ? 3.Show ? ? ? 4.Find ? ? ? ? |\n");
? ? printf("| ? 5.Sort ? ? ? 6.Modify ? ? ? |\n");
? ? printf("| ? ? ? ?0.Exit ? ? ? ? ? ? ? ? |\n");
? ? printf("+-------------------------------+\n");
}

int main()
{
? ? int choose = 0;
? ? Contact my_con;
? ? InitContact(&my_con);?
? ? do
? ? {
? ? ? ? menu();
? ? ? ? printf("請選擇要進行的操作:\n");
? ? ? ? scanf_s("%d", &choose);
? ? ? ? switch (choose)
? ? ? ? {
? ? ? ? case ADD:
? ? ? ? ? ? AddContact(&my_con);
? ? ? ? ? ? break;
? ? ? ? case DEL:
? ? ? ? ? ? DelContact(&my_con);
? ? ? ? ? ? break;
? ? ? ? case SHOW:
? ? ? ? ? ? ShowContact(&my_con);
? ? ? ? ? ? break;
? ? ? ? case FIND:
? ? ? ? ? ? FindContact(&my_con);
? ? ? ? ? ? break;
? ? ? ? case SORT:
? ? ? ? ? ? SortContact(&my_con);
? ? ? ? ? ? break;
? ? ? ? case MODIFY:
? ? ? ? ? ? ModifyContact(&my_con);
? ? ? ? ? ? break;
? ? ? ? case EXIT:
? ? ? ? ? ? DestroyContact(&my_con);
? ? ? ? ? ? break;
? ? ? ? default:
? ? ? ? ? ? printf("請重新選擇!\n");
? ? ? ? ? ? break;
? ? ? ? }

? ? } while (choose);

? ? system("pause");
? ? return 0;
}

3.剩下的就是通訊錄主要函數的具體實現

test.c

#include"Contact.h"
#include"bubble_sort.h"

void InitContact(pContact pc)//初始化通訊錄
{
? ? pc->count = 0;
? ? pc->capacity = DEFAULT_SZ;
? ? pc->data =(PeoInfo *) calloc(sizeof(PeoInfo),pc->capacity);
}

void check_capacity(pContact pc)//檢查通訊錄空間
{

? ? PeoInfo* p = (PeoInfo *)realloc(pc->data, (pc->capacity + 2)*sizeof(PeoInfo));
? ? if (p != NULL)
? ? {
? ? ? ? pc->data = p;
? ? ? ? p = NULL;
? ? }
? ? pc->capacity += 2;
? ? printf("增容成功!\n");
}

void AddContact(pContact pc)//增加信息
{
? ? char c = 0;
? ? if (pc->count == pc->capacity)
? ? {
? ? ? ? printf("通訊錄已滿!\n");
? ? ? ? printf("請問是否增容:(Y \\ N)\n");
? ? ? ? getchar();
? ? ? ? scanf("%c", &c);
? ? ? ? if (c == 'Y')
? ? ? ? {
? ? ? ? ? ? check_capacity(pc);
? ? ? ? }
? ? ? ? else
? ? ? ? {
? ? ? ? ? ? return;
? ? ? ? }
? ? }
? ? printf("請輸入名字:");
? ? scanf("%s", pc->data[pc->count].name);
? ? printf("請輸入年齡:");
? ? scanf("%d", &(pc->data[pc->count].age));
? ? printf("請輸入電話:");
? ? scanf("%s", pc->data[pc->count].phone);
? ? pc->count++;
? ? printf("添加成功!\n");
}

void ShowContact(pContact pc)//展示通訊錄
{
? ? int i = 0;
? ? if (pc->count == 0)
? ? {
? ? ? ? printf("通訊錄為空!\n");
? ? ? ? return;
? ? }
? ? printf("%7s %5s %10s\n","姓名","年齡","電話");
? ? for (i = 0; i < pc->count; i++)
? ? {
? ? ? ? printf("%7s %5d %10s\n", pc->data[i].name, pc->data[i].age, pc->data[i].phone);
? ? }
? ? printf("--------------------------\n");
}

int FindEntry(char name[],pContact pc)//查找
{
? ? int i = 0;
? ? for (i = 0; i < pc->count; i++)
? ? {
? ? ? ? if (strcmp(pc->data[i].name, name) == 0)
? ? ? ? {
? ? ? ? ? ? return i;
? ? ? ? }
? ? }
? ? return -1;
}

void DelContact(pContact pc)//刪除通訊錄
{
? ? char name[20] = { 0 };
? ? int pos = 0;
? ? int i = 0;
? ? if (pc->count == 0)
? ? {
? ? ? ? printf("通訊錄為空!\n");
? ? ? ? return ;
? ? }
? ? //1.查找
? ? printf("要查找的人的名字:");
? ? scanf("%s", name);
? ? pos = FindEntry(name,pc);
? ? if (pos == -1)
? ? {
? ? ? ? printf("要刪除的人不存在!\n");
? ? ? ? return ;
? ? }
? ? //2.刪除
? ? for (i = pos; i < pc->count-1;i++)
? ? {
? ? ? ? pc->data[i] = pc->data[i + 1];
? ? }
? ? pc->count--;
? ? printf("刪除成功!\n");
}

void FindContact(pContact pc)//查找
{
? ? int n = 0;
? ? char name[20] = { 0 };
? ? printf("請輸入要查找的人名:\n");
? ? scanf("%s", name);
? ? n = FindEntry(name,pc);
? ? if (n >= 0)
? ? {
? ? ? ? printf("%s ? ? %d ? ? ?%s\n", pc->data[n].name, pc->data[n].age, pc->data[n].phone);
? ? }
? ? else if (n = -1)
? ? {
? ? ? ? printf("查無此人!\n");
? ? }
}

void DestroyContact(pContact pc)//銷毀通訊錄
{
? ? free(pc->data);
? ? pc->data = NULL;
? ? pc->count = 0;
? ? pc->capacity = 0;
? ? printf("成功銷毀!\n");
}

void ModifyContact(pContact pc)//修改通訊錄信息
{
? ? char name[20] = { 0 };
? ? int n = 0;
? ? printf("請輸入要修改的人名:\n");
? ? scanf("%s", name);
? ? if (n >= 0)
? ? {
? ? ? ? n = FindEntry(name, pc);
? ? ? ? printf("請輸入名字:");
? ? ? ? scanf("%s", pc->data[n].name);
? ? ? ? printf("請輸入年齡:");
? ? ? ? scanf("%d", &(pc->data[n].age));
? ? ? ? printf("請輸入電話:");
? ? ? ? scanf("%s", pc->data[n].phone);
? ? }
? ? else
? ? {
? ? ? ? printf("查無此人!\n");
? ? }
? ? printf("修改成功!\n");
}

//通訊錄的排序
void Menu()
{
? ? printf("請選擇按照何種方式排序:\n");
? ? printf("1.姓名 ? ? ?2.年齡 ? ? ?\n");
? ? printf(" ? ? ?0.exit ? ? ? ? ? ?\n");
}
void SortContact(pContact pc)
{
? ? int input = 0;
? ? ? ? Menu();
? ? ? ? scanf("%d", &input);
? ? ? ? switch (input)
? ? ? ? {
? ? ? ? case 1:
? ? ? ? ? ? bubble_sort(pc->data,pc->count, sizeof(PeoInfo),cmp_name);
? ? ? ? ? ? break;
? ? ? ? case 2:
? ? ? ? ? ? bubble_sort(pc->data, pc->count, sizeof(PeoInfo), cmp_age);
? ? ? ? ? ? break;
? ? ? ? case 0:
? ? ? ? ? ? break;
? ? ? ? default:
? ? ? ? ? ? printf("請重新選擇!\n");
? ? ? ? }
? ? ? ? printf("排序成功!\n");
}

4.可以發(fā)現,上面我們的通訊錄的排序函數還沒有具體實現。這里用冒泡排序實現。

bubble_sort.h

#ifndef __BUBBLESORT_H_
#define __BUBBLESORT_H_

#include<stdlib.h>
#include<stdio.h>

void bubble_sort(void * base, int sz, int width, int(*cmp)(const void* e1, const void *e2));
void swap(char *buf1, char ?*buf2, int width);
int cmp_name(const void *e1, const void *e2);
int cmp_age(const void *e1, const void *e2);

#endif //__BUBBLESORT_H__

bubble.c

#include"bubble_sort.h"
#include"Contact.h"

void swap(char *buf1, char ?*buf2, int width)
{
? ? int i = 0;
? ? for (i = 0; i < width; i++)
? ? {
? ? ? ? char tmp = *buf1;
? ? ? ? *buf1 = *buf2;
? ? ? ? *buf2 = tmp;
? ? ? ? buf1++;
? ? ? ? buf2++;
? ? }

}

int cmp_name(const void *e1, const void *e2)
{
? ? return strcmp(((PeoInfo*)e1)->name, ((PeoInfo*)e2)->name);
}

int cmp_age(const void *e1, const void *e2)
{
? ? return ((PeoInfo*)e1)->age - ((PeoInfo*)e2)->age;
}

void bubble_sort(void * base, int sz, int width, int(*cmp)(const void* e1, const void *e2))
{
? ? int i = 0;
? ? int j = 0;
? ? for (i = 0; i < sz - 1; i++)
? ? {
? ? ? ? for (j = 0; j < sz - 1 - i; j++)
? ? ? ? {
? ? ? ? ? ? if (cmp((char*)base+width*j, (char*)base+width*(j+1))>0)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? //交換
? ? ? ? ? ? ? ? swap((char *)base + width*j, (char*)base + width*(j + 1),width);
? ? ? ? ? ? }
? ? ? ? }
? ? }

}

以上就是通訊錄的大概步驟了,希望可以幫到大家!

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

  • Visual?Studio?2022?激活碼(親測可用)

    Visual?Studio?2022?激活碼(親測可用)

    在?Visual?Studio?2019?的基礎上,新版集成開發(fā)壞境提供了非常多的改進,包括對?64?位、.NET?6?的支持,為核心調試器提供更好的性能。本文給大家分享Visual?Studio?2022?激活碼,需要的朋友參考下吧
    2021-12-12
  • C/C++獲取Windows平臺CPU占用率的方法

    C/C++獲取Windows平臺CPU占用率的方法

    最近在做系統(tǒng)信息相關的接口,為了實現跨平臺,故在linux和Windows平臺獲取占用率信息,文章主要介紹Windows下的方法,文中給出了參考代碼,需要的朋友可以參考下
    2023-12-12
  • C++實現簡單走迷宮的代碼

    C++實現簡單走迷宮的代碼

    這篇文章主要為大家詳細介紹了C++實現簡單走迷宮的代碼,利用回溯法求解,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-05-05
  • C語言數據結構之圖書借閱系統(tǒng)

    C語言數據結構之圖書借閱系統(tǒng)

    這篇文章主要為大家詳細介紹了C語言數據結構之圖書借閱系統(tǒng),文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-03-03
  • 一文帶你了解C++中deque的使用

    一文帶你了解C++中deque的使用

    C++中的deque是一種雙端隊列,可以在隊列的前端和后端進行插入元素和刪除操作,同時可以視作一個長度不定的數組,支持高效的插入和刪除操作。本篇文章將深入探討C++中的deque的使用,感興趣的可以了解一下
    2023-05-05
  • C語言自制測色弱找方塊游戲的示例代碼

    C語言自制測色弱找方塊游戲的示例代碼

    這篇文章主要介紹了基于C語言自制測色弱找方塊的游戲。該游戲是仿照最近網上流行的找方塊游戲編寫的,可玩性還是挺高的,感興趣的可以了解一下
    2022-12-12
  • C語言實現頁面置換 先進先出算法(FIFO)

    C語言實現頁面置換 先進先出算法(FIFO)

    這篇文章主要為大家詳細介紹了C語言實現頁面置換,先進先出算法(FIFO),文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-12-12
  • C語言基礎使用IDE快速開發(fā)的方法

    C語言基礎使用IDE快速開發(fā)的方法

    這篇文章主要介紹了C語言基礎使用IDE快速開發(fā)的方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-11-11
  • C語言16進制與ASCII字符相互轉換

    C語言16進制與ASCII字符相互轉換

    大家好,本篇文章主要講的是C語言16進制與ASCII字符相互轉換,感興趣的同學趕快來看一看吧,對你有幫助的話記得收藏一下
    2022-01-01
  • c++11中std::move函數的使用

    c++11中std::move函數的使用

    本文主要介紹了c++11中std::move函數的使用,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-01-01

最新評論