最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

C語(yǔ)言簡(jiǎn)易通訊錄的實(shí)現(xiàn)代碼

 更新時(shí)間:2020年11月17日 17:19:03   作者:咭咭熊  
這篇文章主要為大家詳細(xì)介紹了C語(yǔ)言簡(jiǎn)易通訊錄的實(shí)現(xiàn)代碼,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了C語(yǔ)言簡(jiǎn)易通訊錄的具體代碼,供大家參考,具體內(nèi)容如下

通訊錄的實(shí)現(xiàn):

一.介紹:

運(yùn)用C語(yǔ)言的數(shù)組、指針、結(jié)構(gòu)體等相關(guān)知識(shí):實(shí)現(xiàn)一個(gè)簡(jiǎn)易的通訊錄:

此通訊錄的大概內(nèi)容為:
通訊錄可存儲(chǔ)1000個(gè)人的信息:

每個(gè)人的基本信息有:名字+ 年齡+性別 +  電話+ QQ + 地址
此通訊錄所具有的功能:增加+ 刪除 +  查詢 + 修改 +  排序 + 退出

此通訊錄在設(shè)計(jì)時(shí):總共設(shè)計(jì)了三個(gè)文件:

contact.h: 類型的聲明和函數(shù)的聲明

contact.c:函數(shù)功能的具體實(shí)現(xiàn)

test.c: 測(cè)試文件

二.具體實(shí)現(xiàn):

1. contact.h:

此文件中定義該通訊錄中所設(shè)計(jì)的功能的函數(shù)聲明以及兩個(gè)結(jié)構(gòu)體:

        PeoInfo:存儲(chǔ)通訊錄中每個(gè)人的基本信息

       Contact:存儲(chǔ)通訊錄中的數(shù)據(jù)以及通訊錄中的已存儲(chǔ)的人數(shù)

具體代碼實(shí)現(xiàn)如下:

//
//類型的聲明+函數(shù)的聲明
//
 
#include <stdio.h>
#include <string.h>
 
#define MAX 1000
 
#define MAX_NAME 20
#define MAX_TELE 12
#define MAX_ADDR 100
#define MAX_QQ 12
#define MAX_SEX 5
 
typedef struct PeoInfo
{
 char name[MAX_NAME];
 char tele[MAX_TELE];
 char addr[MAX_ADDR];
 char qq[MAX_QQ];
 char sex[MAX_SEX];
 short age;
}PeoInfo;
 
//通訊錄
typedef struct Contact
{
 PeoInfo data[MAX];//數(shù)據(jù)
 int sz;//有效個(gè)數(shù)
}Contact;
 
 
//添加一個(gè)人的信息
void add_contact(Contact* pc);
 
//顯示通訊錄中的信息
void show_contact(Contact* pc);
 
//刪除指定的聯(lián)系人
void del_contact(Contact* pc);
 
//查找指定聯(lián)系人
void search_contact(Contact* pc);
 
//修改指定聯(lián)系人
void modify_contact(Contact* pc);
 
//排序通訊錄的數(shù)據(jù)
void sort_contact(Contact* pc); 

2. contact.c:

此文件是該通訊錄所設(shè)計(jì)的功能的具體實(shí)現(xiàn):具體包括:

(1) 添加一個(gè)人的信息   void add_contact(Contact* pc);

(2)顯示通訊錄中的信息   void show_contact(Contact* pc);

(3)刪除指定的聯(lián)系人  void del_contact(Contact* pc);

(4)查找指定聯(lián)系人  void search_contact(Contact* pc);

(5)修改指定聯(lián)系人  void modify_contact(Contact* pc);

(6)排序通訊錄的數(shù)據(jù)  void sort_contact(Contact* pc);

具體代碼實(shí)現(xiàn)如下:

#define _CRT_SECURE_NO_WARNINGS 1
 
#include "contact.h"
 
void add_contact(Contact* pc)
{
 if (pc->sz == MAX)
 {
 printf("通訊錄已滿\n");
 }
 else
 {
 printf("請(qǐng)輸入名字:>");
 scanf("%s", pc->data[pc->sz].name);
 printf("請(qǐng)輸入電話:>");
 scanf("%s", pc->data[pc->sz].tele);
 printf("請(qǐng)輸入地址:>");
 scanf("%s", pc->data[pc->sz].addr);
 printf("請(qǐng)輸入QQ:>");
 scanf("%s", pc->data[pc->sz].qq);
 printf("請(qǐng)輸入性別:>");
 scanf("%s", pc->data[pc->sz].sex);
 printf("請(qǐng)輸入年齡:>");
 scanf("%d", &(pc->data[pc->sz].age));
 
 pc->sz++;
 printf("添加成功\n");
 }
}
 
void show_contact(Contact* pc)
{
 int i = 0;
 printf("%10s %12s %20s %5s %12s %5s\n", "名字", "電話", "地址", "年齡", "QQ", "性別");
 for (i = 0; i < pc->sz; i++)
 {
 printf("%10s %12s %20s %5d %12s %5s\n", pc->data[i].name,
 pc->data[i].tele,
 pc->data[i].addr,
 pc->data[i].age,
 pc->data[i].qq,
 pc->data[i].sex);
 }
}
 
static int find_peo_by_name(Contact* pc, char name[])
{
 int i = 0;
 for (i = 0; i < pc->sz; i++)
 {
 if (strcmp(name, pc->data[i].name) == 0)
 {
 return i;//找到了,返回下標(biāo)
 }
 }
 return -1;//找不到
}
 
void del_contact(Contact* pc)
{
 
 if (pc->sz == 0)
 {
 printf("抱歉,通訊錄為空\(chéng)n");
 }
 else
 {
 char name[MAX_NAME] = { 0 };
 printf("請(qǐng)輸入要?jiǎng)h除人的名字:>");
 scanf("%s", name);
 //1. 找到指定的聯(lián)系人的位置
 int pos = find_peo_by_name(pc, name);
 if (pos == -1)
 {
 printf("很遺憾,刪除的人不存在\n");
 }
 else
 {
 //2. 刪除
 int j = 0;
 for (j = pos; j < pc->sz - 1; j++)
 {
 pc->data[j] = pc->data[j + 1];
 }
 pc->sz--;
 printf("刪除成功\n");
 }
 }
}
 
 
void search_contact(Contact* pc)
{
 char name[MAX_NAME] = { 0 };
 printf("請(qǐng)輸入要查找人的名字:>");
 scanf("%s", name);
 int pos = find_peo_by_name(pc, name);
 if (pos == -1)
 {
 printf("查無(wú)此人\n");
 }
 else
 {
 printf("%10s %12s %20s %5s %12s %5s\n",
 "名字", "電話", "地址", "年齡", "QQ", "性別");
 printf("%10s %12s %20s %5d %12s %5s\n", pc->data[pos].name,
 pc->data[pos].tele,
 pc->data[pos].addr,
 pc->data[pos].age,
 pc->data[pos].qq,
 pc->data[pos].sex);
 }
}
 
 
void modify_contact(Contact* pc)
{
 char name[MAX_NAME] = { 0 };
 printf("請(qǐng)輸入要修改人的名字:>");
 scanf("%s", name);
 int pos = find_peo_by_name(pc, name);
 if (pos == -1)
 {
 printf("查無(wú)此人\n");
 }
 else
 {
 printf("請(qǐng)輸入新的名字:>");
 scanf("%s", pc->data[pos].name);
 printf("請(qǐng)輸入新的電話:>");
 scanf("%s", pc->data[pos].tele);
 printf("請(qǐng)輸入新的地址:>");
 scanf("%s", pc->data[pos].addr);
 printf("請(qǐng)輸入新的QQ:>");
 scanf("%s", pc->data[pos].qq);
 printf("請(qǐng)輸入新的性別:>");
 scanf("%s", pc->data[pos].sex);
 printf("請(qǐng)輸入新的年齡:>");
 scanf("%d", &(pc->data[pos].age));
 }
}
 
void sort_contact(Contact* pc)
{
 int i = 0;
 int j = 0;
 for (i = 0; i < pc->sz - 1; i++)
 {
 int flag = 1;//假設(shè)已經(jīng)有序
 for (j = 0; j < pc->sz - 1 - i; j++)
 {
 if (strcmp(pc->data[j].name, pc->data[j + 1].name) > 0)
 {
 PeoInfo tmp = pc->data[j];
 pc->data[j] = pc->data[j + 1];
 pc->data[j + 1] = tmp;
 flag = 0;
 }
 }
 if (1 == flag)
 {
 break;
 }
 }
}

3.test.c

此文件是對(duì)該通訊錄的功能實(shí)現(xiàn)的測(cè)試文件:包括菜單的打印,根據(jù)用戶的輸入,輸出相關(guān)信息以及該通訊錄功能的完整流程的實(shí)現(xiàn)等。

具體代碼實(shí)現(xiàn)如下:

#define _CRT_SECURE_NO_WARNINGS 1
#include "contact.h"
 
//測(cè)試文件
//通訊錄1000個(gè)人的信息:名字+ 年齡+ 電話 + 地址+ QQ + 性別 
//增 刪 查 改 排序 退出
#define _CRT_SECURE_NO_WARNINGS 1
 
 
void menu()
{
 printf("*******************************\n");
 printf("****** 1. add  2. del * ***\n");
 printf("****** 3. search 4. modify ***\n");
 printf("****** 5. sort 6. show ***\n");
 printf("******  0. exit  ***\n");
 printf("*******************************\n");
}
 
enum Option
{
 EXIT,
 ADD,
 DEL,
 SEARCH,
 MODIFY,
 SORT,
 SHOW
};
 
//first_name
//FirstName
 
void test()
{
 //創(chuàng)建的通訊錄
 Contact con = { 0 };
 int input = 0;
 do
 {
 menu();
 printf("請(qǐng)選擇:>");
 scanf("%d", &input);
 switch (input)
 {
 case ADD:
 add_contact(&con);
 break;
 case DEL:
 del_contact(&con);
 break;
 case SORT:
 sort_contact(&con);
 break;
 case SHOW:
 show_contact(&con);
 break;
 case SEARCH:
 search_contact(&con);
 break;
 case MODIFY:
 modify_contact(&con);
 break;
 case EXIT:
 printf("退出通訊錄\n");
 break;
 default:
 printf("選擇錯(cuò)誤\n");
 break;
 }
 } while (input);
}
int main()
{
 test();//
 return 0;
}

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論

巴林右旗| 百色市| 遂昌县| 大城县| 湘阴县| 德州市| 岢岚县| 靖宇县| 怀远县| 勐海县| 本溪市| 荣昌县| 蕉岭县| 称多县| 周宁县| 澄江县| 阜平县| 哈尔滨市| 绥芬河市| 甘南县| 获嘉县| 弥渡县| 桃江县| 施秉县| 林芝县| 奈曼旗| 贵德县| 南投县| 伊通| 铜梁县| 商城县| 浦东新区| 南乐县| 天祝| 化州市| 洪湖市| 航空| 桂平市| 南靖县| 吴江市| 灌阳县|