C语言——通讯录实现

ops/2024/10/21 13:38:56/

一、介绍

本文只是对于结构体类型的练习。

只是简单的静态通讯录实现,没有具体的UI界面,只有一些简单的功能,同时也比较粗糙,有很多地方没有经过足够的打磨。

二、源码

本项目包含三个文件:

test.c/cpp

#include "contacts_list.h"int main()
{int status = 0;Contacts_list contacts_list;//通讯录结构体变量InitContact(&contacts_list);int res = 0;int num = 0;do{menu();//打印菜单scanf("%d", &status);system("cls");switch (status){case 1:AddContact(&contacts_list);break;case 2:DeleteContact(&contacts_list);break;case 3:char name1[21];printf("please type in the name>:");scanf("%s", name1);res = SearchContact(&contacts_list, name1);if (res != -1){printf("%d\n", res);}else{printf("cannot find\n");}break;case 4:char name2[21];printf("please type in the name>:");scanf("%s", name2);ModifyContact(&contacts_list,name2);break;case 5:DisplayContact(&contacts_list);break;case 6:printf("1.sort by name\n");printf("2.sort by age\n");scanf("%d", &num);SortContact(&contacts_list,num);break;case 0:printf("exit\n");break;default :printf("error\n");break;}} while (status);return 0;
}

contact.c/cpp

#include "contacts_list.h"void menu()
{printf("*************************************\n");printf("*****  1.add            2.del    ****\n");printf("*****  3.search         4.modify ****\n");printf("*****  5.display        6.sort   ****\n");printf("*****  0.exit                    ****\n");printf("*************************************\n");printf("please choose a option>:");
}void InitContact(Contacts_list* list)
{int i = 0;for (i = 0; i < 100; i++){list->num = 0;//将联系人个数初始化为零memset(list->data, 0, sizeof(list->data));//将数据部分初始化为0}
}void AddContact(Contacts_list* list)
{if (list->num == MAXCONTACTS){printf("there is no more space\n");//没有空间return;}printf("please type in name(maximum 20)>:");scanf("%s", list->data[list->num].name);printf("please type in gender(maximum 8)>:");scanf("%s", list->data[list->num].gender);printf("please type in age>:");scanf("%d", &(list->data[list->num].age));list->num++;printf("operating successfully\n");
}void DisplayContact(const Contacts_list* list)
{int i = 0;for (i = 0; i < list->num; i++){printf("name: %s\n", list->data[i].name);printf("gender: %s\n", list->data[i].gender);printf("age: %d\n", list->data[i].age);printf("-------------------------\n");}printf("display successfully\n");
}void DeleteContact(Contacts_list* list)
{char name[21];if (list->num == 0){printf("there is no contact can be deleted\n");//联系人为零,无需删除return;}printf("please type in the name who you want to delete>:");scanf("%s", name);int res = SearchContact(list, name);if (res != -1){int i = 0;for (i = res; i < list->num - 1; i++){list->data[i] = list->data[i + 1];}list->num--;printf("delete successfully\n");}else{printf("cannot find\n");//没找到要删除的联系人return;}
}int SearchContact(Contacts_list* list, const char* string)
{int i = 0;for (i = 0; i < list->num; i++){if (strcmp(list->data[i].name, string) == 0){return i;//查找到返回下标}}return -1;//未查找到返回-1
}void ModifyContact(Contacts_list* list, const char* string)
{int res = SearchContact(list, string);if (res == -1){printf("cannot find\n");return;}printf("please type in name(maximum 20)>:");scanf("%s", list->data[res].name);printf("please type in gender(maximum 8)>:");scanf("%s", list->data[res].gender);printf("please type in age>:");scanf("%d", &(list->data[res].age));printf("modification successfully\n");
}int cmp_by_name(const void* element1, const void* element2)
{PeopleInfo* ele1 = (PeopleInfo*)element1;//强制转换为联系人结构体类型,方便访问name变量PeopleInfo* ele2 = (PeopleInfo*)element2;return strcmp(ele1->name, ele2->name);
}int cmp_by_age(const void* element1, const void* element2)
{int res = ((PeopleInfo*)element1)->age - ((PeopleInfo*)element2)->age;return ((-res < 0) - (res < 0));
}void SortContact(Contacts_list* list, int num)
{switch (num){case 1:qsort(list->data, list->num, sizeof(list->data[0]), cmp_by_name);printf("sort successfully\n");break;case 2:qsort(list->data, list->num, sizeof(list->data[0]), cmp_by_age);printf("sort successfully\n");break;default :printf("error\n");break;}
}

contact.h

#pragma once
#include <stdio.h>
#include <stdlib.h>
#include <string.h>#define MAXCONTACTS 100//打印菜单函数
void menu();//声明联系人信息结构体变量
typedef struct PeopleInfo
{char name[21];char gender[9];int age;
}PeopleInfo;//声明通讯录结构体变量
typedef struct Contacts_list
{PeopleInfo data[MAXCONTACTS];//存放联系人数据int num;//记录通讯录联系人个数
}Contacts_list;//初始化函数
void InitContact(Contacts_list* list);//添加联系人
void AddContact(Contacts_list* list);//显示通讯录
void DisplayContact(const Contacts_list* list);//删除联系人
void DeleteContact(Contacts_list* list);//查找联系人
int SearchContact(Contacts_list* list, const char* string);//修改联系人
void ModifyContact(Contacts_list* list,const char* string);//排序联系人
void SortContact(Contacts_list* list, int num);//按名字排序
int cmp_by_name(const void* element1, const void* element2);//按年龄排序
int cmp_by_age(const void* element1, const void* element2);

如有错误,欢迎指正。


http://www.ppmy.cn/ops/27204.html

相关文章

2024-04-30 区块链-以太坊-相关文档

摘要: 2024-04-30 区块链-以太坊-文档 以太坊-相关文档: https://github.com/ethereum/go-ethereum https://geth.ethereum.org/ https://geth.ethereum.org/docs https://ethereum.org/zh/ 以太坊开发文档 | ethereum.org 以太坊开发文档_w3cschool 以太坊开发文档 基础主题 …

Microsoft Edge浏览器:高效、简洁、个性化的网页浏览体验

Microsoft Edge是微软公司推出的一款网络浏览器&#xff0c;它是基于Chromium开源项目开发的&#xff0c;因此与Google Chrome有很多相似之处。以下是一些使用Microsoft Edge的心得体会&#xff1a; 1. 界面简洁&#xff1a;Microsoft Edge的界面设计非常简洁&#xff0c;用户…

Ansible playbook之循环

1.标准Loops 当我们想安装10个软件包的时候&#xff0c;为了避免写10个task来安装&#xff0c;我们可以直接使用标准的loops简单快速实现10个软件包的安装&#xff0c;下面例子是分别打印了one two这两个值&#xff1a; #1.编写loop.yaml [rootansible01 ansible]# cat loops.…

SpringBoot项目配置SpringDataRedis

1 在SpringBoot中常规的配置 <dependency><!--自带lettcute客户端--><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-redis</artifactId></dependency><dependency><groupId>org.ap…

uniapp + uView动态表单校验

项目需求&#xff1a;动态循环表单&#xff0c;并实现动态表单校验 页面&#xff1a; <u--form label-position"top" :model"tmForm" ref"tmForm" label-width"0px" :rulesrules><div v-for"(element, index) in tmForm…

R语言之如何安装R和RStudio软件

目录 简介下载R安装包安装R软件安装RStudio软件总结 简介 R软件是一种用于统计计算和数据分析的编程语言&#xff0c;它提供了丰富的函数和包来处理和分析各种数据集。R具有广泛的应用领域&#xff0c;包括统计学、生物信息学、金融学等。它是一个开源&#xff0c;免费的软件。…

AIGC | 如何使用AI绘画工具stable diffusion做品牌视觉延展

设计师在从事品牌视觉相关设计的过程中&#xff0c;往往离不开使用品牌视觉符号进行主题化或风格化设计&#xff0c;下面就为大家分享如何使用stable diffusion来辅助进行品牌视觉logo延展设计。 环境搭建 在进行设计之前我们需要先下载Controlnet QR Code Monster模型&#x…

限流的学习

限流算法&#xff1a; 滑动窗口算法 滑动日志算法 漏桶算法 令牌桶算法 redis分布式限流 1、固定窗口限流 固定窗口算法又叫计数器算法&#xff0c;是一种简单方便的限流算法。主要通过一个支持原子操作的计数器来累计 1 秒内的请求次数&#xff0c;当 1 秒内计数达到限流阈值…