当前位置:网站首页>通讯录(二)
通讯录(二)
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
- This is a quick look-up table of machine & deep learning code
- LeetCode 101. 对称二叉树 && 100. 相同的树 && 572. 另一棵树的子树
- Detailed introduction and application of GaN (comprehensive and complete)
- 文件基础知识
- Could not stop Cortex-M device! please check the JTAG cable的解决办法
- [web page performance optimization] what about the slow loading speed of the first screen of SPA (single page application)?
- Application of current probe in ECU and electrical system current measurement
- Tkinter GUI address book management system
- C语言 cJSON库的使用
猜你喜欢

超全Mavan标签详解

Thales launches solutions to help SAP customers control cloud data

Tang's little helper

数二2010真题考点

nodejs 简单例子程序之express

Construction of Huffman tree

Keil5 "loading PDSC debug description failed for STMicroelectronics stm32hxxxxxxx" solution

408 Chapter 2 linear table

1---电子实物认知

How to judge the performance of static code quality analysis tools? These five factors must be considered
随机推荐
Cve-2022-33891 Apache spark shell command injection vulnerability recurrence
结合GHS MULTI使用瑞萨E1仿真器实现对瑞萨RH850单片机的仿真调试
解决You can change this value on the server by setting the ‘max_allowed_packet‘ variable报错
Could not stop Cortex-M device! Please check the JTAG cable solution
Linux启动mysql报错
Detailed explanation of super full mavan label
Detailed introduction and application of GaN (comprehensive and complete)
11.1-CM24 最近公共祖先
Leetcode 101. symmetric binary tree & 100. same tree & 572. Subtree of another tree
Taishan Office Technology Lecture: conversion relations of inch, centimeter, pound, pika, Ti, line, word line and pixel
Tensor to img && imge to tensor (pytorch的tensor转换)
TypeError: Unrecognized value type: <class ‘str‘> ParserError: Unknown string format
srec_ Use of common cat parameters
vim基本操作命令
Number two 2010 real test site
Related operations of binary tree
11.2-hj86 find the maximum number of continuous bits
Jz32 print binary tree from top to bottom
这是一张机器&深度学习代码速查表
Jz71 jump step expansion problem