主要目的是練習一下動态開辟記憶體和檔案讀寫
contact.h
#pragma once
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include <malloc.h>
#include<iostream>
using namespace std;
#define M 20
//人的資訊
struct People
{
char name[M];
char sex[M];
char address[M];
long long number;
};
//通訊錄
struct Contact
{
int sz;//目前People數組的下标
struct People* a = NULL; //動态開辟
int capacity;//初始化容量為2
};
struct Contact* init(struct Contact* p);//初始化
void load(struct Contact* p);//加載檔案内容
void preserve(struct Contact* p);//儲存檔案内容
void Show(struct Contact* p);///列印通訊錄
void Add(struct Contact* p);//添加使用者
bool Check_Size(struct Contact* p);//檢查通訊錄的擴容
void Del(struct Contact* p);//删除使用者
void Modify(struct Contact* p);//修改使用者資訊
int Search(struct Contact* p);///查找某使用者資訊
contact.cpp
#include "contact.h"
struct Contact* init(struct Contact* p)
{
struct Contact* t = (struct Contact*)malloc(sizeof(Contact));
if (!t){
perror("init");
}
p = t;
p->sz = 0;
p->capacity = 2;
struct People* tmp = (struct People*)malloc(2 * sizeof(People));
if (!tmp) {
perror("init");
}
p->a = tmp;
return p;
}
void load(struct Contact* p)
{
FILE* f = fopen("contact.txt", "r");
if (!f) {
perror("FILE\n");
}
while (Check_Size(p) && fscanf(f, "%s%s%s%lld", p->a[p->sz].name, p->a[p->sz].sex, p->a[p->sz].address, &((p->a)[p->sz].number) ) != EOF )
{
p->sz++;
}
fclose(f);
f = NULL;
}
void preserve(struct Contact* p)
{
FILE* f = fopen("contact.txt", "w");
for (int i = 0; i < p->sz; i++)
{
fprintf(f,"%s %s %s %lld\n", p->a[i].name, p->a[i].sex, p->a[i].sex,p->a[i].number);
}
fclose(f);
f = NULL;
}
void Show(struct Contact* p)
{
printf("%10s\t%10s\t%10s\t%10s\t\n", "姓名", "性别","位址","電話");
for (int i = 0; i < p->sz; i++)
{
printf("%10s\t%10s\t%10s\t%10lld\t\n", p->a[i].name, p->a[i].sex, p->a[i].address, p->a[i].number);
}printf("\n");
}
void Add(struct Contact* p)
{
Check_Size(p);//檢查目前是否已經滿了
printf("請輸入姓名\n"); scanf("%s", p->a[p->sz].name);
printf("請輸入性别\n"); scanf("%s", p->a[p->sz].sex );
printf("請輸入位址\n"); scanf("%s", p->a[p->sz].address);
printf("請輸入電話\n"); scanf("%lld", &((p->a)[p->sz].number));
(p->sz)++;
}
bool Check_Size(struct Contact* p)
{
if (p->sz == p->capacity)
{
struct People* tmp = (struct People*)realloc(p->a, ( p->capacity + 10) * sizeof(struct People) );
if (!tmp)
{
perror("Check_Size");
}
p->a = tmp;
p->capacity += 10;
}
return true;
}
void Del(struct Contact* p)//通過名字來查找删除
{
int pos = Search(p);
if (pos == -1)
{
printf("未找到該聯系人\n");
return;
}
for (int i = pos; i+1 < p->sz; i++)
{
memmove(p->a + i, p->a + i + 1, sizeof(People));
}
printf("已經删除查找到的聯系人\n");
p->sz--;///減小size
}
void Modify(struct Contact* p)
{
int pos = Search(p);
if (pos == -1)
{
printf("未找到該聯系人\n");
return;
}
printf("請輸入姓名\n"); scanf("%s", p->a[pos].name);
printf("請輸入性别\n"); scanf("%s", p->a[pos].sex);
printf("請輸入位址\n"); scanf("%s", p->a[pos].address);
printf("請輸入電話\n"); scanf("%lld", &((p->a)[pos].number));
printf("修改成功\n");
}
int Search(struct Contact* p)
{
char str[M] = { 0 };
printf("請輸入姓名\n"); scanf("%s", str);
int pos = -1;
for (int i = 0; i < p->sz; i++)
{
if (strcmp(str, p->a[i].name) == 0)
{
printf("%10s\t%10s\t%10s\t%10lld\t\n", p->a[i].name, p->a[i].sex, p->a[i].address, p->a[i].number);
do {
printf("是否找到該聯系人?1(yes):0(no)\n");
int op;
scanf("%d", &op);
if (op == 1)
{
pos = i;
break;
}
else if (op == 0)
{
break;
}
else {
printf("輸入錯誤\n");
}
} while (1);
}
}
return pos;
}
test.cpp
#include "contact.h"
void menu()
{
printf("****************1.add 2.del****************\n");
printf("****************3.modify 4.search*************\n");
printf("****************5.show 0.Exit***************\n");
}
enum Option
{
Exit = 0,
add = 1,
del = 2,
modify = 3,
search = 4,
show = 5
};
int main()
{
struct Contact* pa = NULL;
pa = init(pa);
load(pa);
; do {
menu();
int input;
scanf("%d", &input);
switch (input)
{
case Exit:
preserve(pa);
return 0;
break;
case add:
Add(pa);
break;
case del:
Del(pa);
break;
case modify:
Modify(pa);
break;
case search:
Search(pa);
break;
case show:
Show(pa);
break;
default:
printf("輸入有誤\n");
break;
}
} while (1);
return 0;
}