当前位置:网站首页>通讯录(二)
通讯录(二)
2022-07-25 18:26:00 【菜菜小蒙】
该通讯录使用的是动态内存,并有文件保存功能。
(一)函数声明
#pragma once
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include <stdlib.h>
#define INT_SZ 2
#define MAX_NAME 20
#define MAX_SEX 5
#define MAX_TELE 15
#define MAX_ADDR 10
typedef struct People
{
char name[MAX_NAME];
int age;
char sex[MAX_SEX];
char tele[MAX_TELE];
char addr[MAX_ADDR];
} Peo;
typedef struct Contact
{
Peo* data;
int count;
int capacity;
}Contact;
//菜单
void menu();
//初始化
void Init(Contact* p);
//增加联系人
void Add(Contact* p);
//删除联系人
void Delete(Contact* p);
//查找联系人
void Search(Contact* p);
//修改联系人
void Modify(Contact* p);
//输出通讯录
void Show(Contact* p);
//排序通讯录
void Sort(Contact* p);
//销毁通讯录
void Destroy(Contact* p);
//读取通讯录
void ReadFile(Contact* p);
//保存通讯录
void WriteFile(Contact* p);
(二)函数实现
#include "Contact.h"
void scan(Peo arr[], int n)
{
printf("请输入联系人的信息\n");
printf("请输入姓名:");
scanf("%s", arr[n].name);
printf("请输入年龄:");
scanf("%d", &arr[n].age);
printf("请输入性别:");
scanf("%s", arr[n].sex);
printf("请输入电话:");
scanf("%s", arr[n].tele);
printf("请输入地址:");
scanf("%s", arr[n].addr);
}
//比较
int compare(const void* e1, const void* e2)
{
return(strcmp(((Peo*)e1)->name, ((Peo*)e2)->name));
}
//菜单
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");
}
//初始化
void Init(Contact* p)
{
assert(p);
Peo* ptr = (Peo*)malloc(INT_SZ * sizeof(Peo));
if (p->data == NULL)
{
printf("Init:%s\n", strerror(errno));
return;
}
else
{
p->data = ptr;
p->count = 0;
p->capacity = 0;
}
ReadFile(p);
}
//扩容
void CheckCapacity(Contact* p)
{
if (p->count == p->capacity)
{
Peo* ptr = realloc(p->data, (p->capacity + INT_SZ) * sizeof(Peo));
if (ptr == NULL)
{
printf("ADD:%s", strerror(errno));
return;
}
else
{
p->capacity += INT_SZ;
p->data = ptr;
}
}
}
//增加联系人
void Add(Contact* p)
{
assert(p);
//判断通讯录是否已满
CheckCapacity(p);
//输入
scan(p->data, p->count);
printf("添加联系人成功\n");
p->count++;
}
//删除联系人
void Delete(Contact* p)
{
assert(p);
char name[20] = { 0 };
if (p->count == 0)
{
printf("通讯录为空\n");
return;
}
printf("请输入联系人的姓名:");
scanf("%s", name);
for (int i = 0; i < p->count; i++)
{
if (strcmp(name, p->data[i].name) == 0)
{
for (int j = i; j < p->count - 1; j++)
{
p->data[j] = p->data[j + 1];
}
printf("删除成功!\n");
p->count--;
return;
}
}
printf("不存在该联系人\n");
}
//查找联系人
void Search(Contact* p)
{
assert(p);
char name[20] = { 0 };
printf("请输入查询联系人的姓名:");
scanf("%s", name);
for (int i = 0; i < p->count; i++)
{
if (strcmp(p->data[i].name, name) == 0)
{
printf("%-10s%-15s%-20s%-15s%-8s\n", "姓名", "年龄", "性别", "电话", "住址");
printf("%-10s%-15d%-20s%-15s%-8s\n", p->data[i].name, p->data[i].age, p->data[i].sex, p->data[i].tele, p->data[i].addr);
return;
}
}
printf("不存在该联系人\n");
}
//修改联系人
void Modify(Contact* p)
{
assert(p);
printf("请输入修改联系人的姓名:");
char name[20] = { 0 };
scanf("%s", name);
for (int i = 0; i < p->count; i++)
{
if (strcmp(name, p->data[i].name) == 0)
{
scan(p->data, i);
return;
}
}
printf("不存在该联系人\n");
}
//输出通讯录
void Show(Contact* p)
{
assert(p);
printf("%-10s%-15s%-20s%-15s%-8s\n", "姓名", "年龄", "性别", "电话", "住址");
for (int i = 0; i < p->count; i++)
{
printf("%-10s%-15d%-20s%-15s%-8s\n", p->data[i].name, p->data[i].age, p->data[i].sex, p->data[i].tele, p->data[i].addr);
}
}
//排序通讯录
void Sort(Contact* p)
{
assert(p);
qsort(p->data, p->count, sizeof(p->data[0]), compare);
printf("排序成功\n");
}
//销毁通讯录
void Destroy(Contact* p)
{
assert(p);
free(p->data);
p->count = 0;
p->capacity = 0;
p = NULL;
printf("通讯录已销毁\n");
}
//读取通讯录
void ReadFile(Contact* p)
{
assert(p);
FILE* fp = fopen("test.txt", "rb");
if (fp == NULL);
{
return;
}
Peo tmp = { 0 };
while (fread(&tmp, sizeof(Peo), 1, fp) == 1)
{
CheckCapacity(p);
p->data[p->count] = tmp;
p->count++;
}
fclose(fp);
fp = NULL;
}
//保存通讯录
void WriteFile(Contact* p)
{
assert(p);
FILE* fp = fopen("test.txt", "wb");
if (fp == NULL)
{
perror("WriteFile:");
return;
}
for (int i = 0; i < p->count; i++)
{
fwrite(p->data + i, sizeof(Peo), 1, fp);
}
printf("通讯录信息已保存\n");
fclose(fp);
fp = NULL;
}
(三)测试实现
#include "Contact.h"
int main()
{
int input = 0;
Contact PC;
Init(&PC);
do
{
menu();
scanf("%d", &input);
switch (input)
{
case 1:
Add(&PC);
break;
case 2:
Delete(&PC);
break;
case 3:
Search(&PC);
break;
case 4:
Modify(&PC);
break;
case 5:
Show(&PC);
break;
case 6:
Sort(&PC);
break;
case 7:
Destroy(&PC);
break;
case 0:
WriteFile(&PC);
Destroy(&PC);
printf("程序已退出\n");
break;
default:
printf("输入错误\n请重新输入\n");
break;
}
} while (input);
return 0;
}边栏推荐
- Number two 2010 real test site
- The milestone progress has been made in the joint development of whole human GPCR antibody drugs by baicalto and liberothera
- Save the image with gaussdb (for redis), and the recommended business can easily reduce the cost by 60%
- Software testing -- common testing tools
- 408 Chapter 2 linear table
- GAN的详细介绍及其应用(全面且完整)
- [Huawei machine test real question] string matching
- PHP memory management mechanism and garbage collection mechanism
- Why is the index in [mysql] database implemented by b+ tree? Is hash table / red black tree /b tree feasible?
- 如何将exe文件添加到开机启动
猜你喜欢

Kendryte K210 在freertos上的lcd屏幕的使用

imx6 RTL8189FTV移植

TypeError: Unrecognized value type: <class ‘str‘> ParserError: Unknown string format

【华为机试真题】字符串匹配

Detailed explanation of super full mavan label

Interview shock: why does TCP need three handshakes?

nodejs 简单例子程序之express

LeetCode 101. 对称二叉树 && 100. 相同的树 && 572. 另一棵树的子树
![Why is the index in [mysql] database implemented by b+ tree? Is hash table / red black tree /b tree feasible?](/img/1f/a2d50ec6bc97d52c1e7566a42e564b.png)
Why is the index in [mysql] database implemented by b+ tree? Is hash table / red black tree /b tree feasible?

pd.melt() vs reshape2::melt()
随机推荐
c语言---25 扫雷游戏
Could not stop Cortex-M device! please check the JTAG cable的解决办法
C语言 整数与字符串的相互转换
Practice of RTC performance automation tool in memory optimization scenario
Talking about Devops monitoring, how does the team choose monitoring tools?
GAN的详细介绍及其应用(全面且完整)
Combined with GHS multi, use Reza E1 simulator to realize the simulation and debugging of Reza rh850 single chip microcomputer
Keil5 "loading PDSC debug description failed for STMicroelectronics stm32hxxxxxxx" solution
国际权威认可!OceanBase入选Forrester Translytical数据平台报告
解决You can change this value on the server by setting the ‘max_allowed_packet‘ variable报错
vim基本操作命令
Stm8s003f3 internal flash debugging
You can change this value on the server by setting the 'Max_ allowed_ Packet 'variable error
NC78 反转链表
C language -- 25 minesweeping game
C语言 libcurl交叉编译
一次备库的坏块的修复过程
There was an error while marking a file for deletion
Flexible current probe selection guide
testng执行顺序的3中控制方法