当前位置:网站首页>Address book (II)
Address book (II)
2022-07-25 18:34:00 【Caicaixiaomeng】
The address book uses dynamic memory , It also has the function of file saving .
( One ) Function declaration
#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;
// menu
void menu();
// initialization
void Init(Contact* p);
// Add contacts
void Add(Contact* p);
// Delete Contact
void Delete(Contact* p);
// Find contacts
void Search(Contact* p);
// Modify contact
void Modify(Contact* p);
// Output address book
void Show(Contact* p);
// Sort address book
void Sort(Contact* p);
// Destroy the address book
void Destroy(Contact* p);
// Read the address book
void ReadFile(Contact* p);
// Save address book
void WriteFile(Contact* p);
( Two ) Function implementation
#include "Contact.h"
void scan(Peo arr[], int n)
{
printf(" Please enter the contact information \n");
printf(" Please enter a name :");
scanf("%s", arr[n].name);
printf(" Please enter age :");
scanf("%d", &arr[n].age);
printf(" Please enter gender :");
scanf("%s", arr[n].sex);
printf(" Please input the phone number :");
scanf("%s", arr[n].tele);
printf(" Please enter the address :");
scanf("%s", arr[n].addr);
}
// Compare
int compare(const void* e1, const void* e2)
{
return(strcmp(((Peo*)e1)->name, ((Peo*)e2)->name));
}
// menu
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");
}
// initialization
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);
}
// Capacity expansion
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;
}
}
}
// Add contacts
void Add(Contact* p)
{
assert(p);
// Determine if the address book is full
CheckCapacity(p);
// Input
scan(p->data, p->count);
printf(" Add contact succeeded \n");
p->count++;
}
// Delete Contact
void Delete(Contact* p)
{
assert(p);
char name[20] = { 0 };
if (p->count == 0)
{
printf(" Address book is empty \n");
return;
}
printf(" Please enter the name of the contact person :");
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(" Delete successful !\n");
p->count--;
return;
}
}
printf(" The contact does not exist \n");
}
// Find contacts
void Search(Contact* p)
{
assert(p);
char name[20] = { 0 };
printf(" Please enter the name of the inquiry contact :");
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", " full name ", " Age ", " Gender ", " Telephone ", " address ");
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(" The contact does not exist \n");
}
// Modify contact
void Modify(Contact* p)
{
assert(p);
printf(" Please enter the name of the modified contact :");
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(" The contact does not exist \n");
}
// Output address book
void Show(Contact* p)
{
assert(p);
printf("%-10s%-15s%-20s%-15s%-8s\n", " full name ", " Age ", " Gender ", " Telephone ", " address ");
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);
}
}
// Sort address book
void Sort(Contact* p)
{
assert(p);
qsort(p->data, p->count, sizeof(p->data[0]), compare);
printf(" Sort success \n");
}
// Destroy the address book
void Destroy(Contact* p)
{
assert(p);
free(p->data);
p->count = 0;
p->capacity = 0;
p = NULL;
printf(" The address book has been destroyed \n");
}
// Read the address book
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;
}
// Save address book
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(" Address book information saved \n");
fclose(fp);
fp = NULL;
}
( 3、 ... and ) Test implementation
#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(" The program has exited \n");
break;
default:
printf(" Input error \n Please re-enter \n");
break;
}
} while (input);
return 0;
}边栏推荐
猜你喜欢

This is a quick look-up table of machine & deep learning code

Number two 2010 real test site

用GaussDB(for Redis)存画像,推荐业务轻松降本60%

Tang's little helper

RTC 性能自动化工具在内存优化场景下的实践

Practice of RTC performance automation tool in memory optimization scenario

1--- electronic physical cognition

Stm8s003f3 internal flash debugging

Flexible current probe selection guide

Today's sleep quality record is 84 points
随机推荐
Save the image with gaussdb (for redis), and the recommended business can easily reduce the cost by 60%
Uniapp scroll bar topping effect, customized page scroll bar position (sorting)
可视化模型网络连接
Flexible current probe selection guide
Oracle import error: imp-00038: unable to convert to environment character set handle
Recognized by international authorities! Oceanbase was selected into the Forrester translational data platform report
Repair process of bad blocks of primary standby database
CircleIndicator组件,使指示器风格更加多样化
php内存管理机制与垃圾回收机制
Unittest framework application
通讯录(一)
Number two 2010 real test site
"Jargon" | what kind of experience is it to efficiently deliver games with Devops?
rust多线程安全计数
[QNX hypervisor 2.2 user manual]9.5 dump
Application of current probe in ECU and electrical system current measurement
408第二章线性表
【翻译】Logstash、Fluentd、Fluent Bit,还是Vector?如何选择合适的开源日志收集器...
RTC 性能自动化工具在内存优化场景下的实践
Yes, UDP protocol can also be used to request DNS server