这两天把动态内存还有文件函数这部分的课听了一下,也跟着把通讯录升级了一下 存储元素不再是固定的了,当存储的元素等于上限时内存就动态增长 每次退出信息又会销毁,可以用fwrite 和 fread让它保存到文件中,不过每次fwrite它就会覆盖之前的内容,所以每次初始化的时候都要先读取出来,等退出了再一次性读取写入文件中,数量多的话速度就会慢,想实现一下,今晚就下了个MySQL,听听网课琢磨琢磨 把这个改进版记录下来
int main()
{
srand((unsigned int)time(NULL));
game();
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <errno.h>
#define MAX_NAME 16
#define MAX_SAT 6
#define MAX_TELE 12
#define MAX_ADDR 15
#define DEFAULT_SZ 3
定义在头文件的函数声明 各个的作用都写下来了
void game();
void menu();
void chushihua(struct uct* p1);
void addformation(struct uct* p1);
void showformation(struct uct* p1);
void subformation(struct uct* p1);
void searchformation(const struct uct* p1);
void modifyformation(struct uct* p1);
int cmp_stu_name(const void* p1, const void* p2);
void IncreasetCapacity(struct uct*p1);
void realise(struct uct* p1);
void savecontact(struct uct* p1);
void loadcontact(struct uct* p1);
enum arr
{
EXIT,
ADD,
SUB,
SEARCH,
MODIFY,
SHOW,
SORT,
SAVE
};
struct information
{
char name[MAX_NAME];
int age;
char set[MAX_SAT];
char tele[MAX_TELE];
char addr[MAX_ADDR];
};
struct uct
{
struct information *data;
int size;
int capacity;
};
我一般喜欢函数从上往下实现
void menu()
{
printf("*******************************\n");
printf("*****1 add 2 sub*****\n");
printf("*****3 search 4 modify**\n");
printf("*****5 show 6 sort****\n");
printf("*****7 save 0 exit****\n");
printf("*******************************\n");
}
void IncreasetCapacity(struct uct* p1);
void loadcontact(struct uct* p1)
{
struct information tmp = { 0 };
FILE* pfRead = fopen("test.dat", "rb");
if (pfRead == NULL)
{
printf("loadcontact::%s", strerror(errno));
return;
}
while (fread(&tmp, sizeof(struct information), 1, pfRead))
{
IncreasetCapacity(p1);
p1->data[p1->size] = tmp;
p1->size++;
}
fclose(pfRead);
pfRead = NULL;
}
void chushihua(struct uct* p1)
{
p1->data = (struct information*)malloc(DEFAULT_SZ * sizeof(struct information));
if (p1->data == NULL)
return;
p1->size = 0;
p1->capacity = DEFAULT_SZ;
loadcontact(p1);
}
void savecontact(struct uct* p1)
{
FILE* pfWrite = fopen("test.dat","wb");
if (p1 == NULL)
{
printf("%s\n",strerror(errno));
return;
}
int i = 0;
for (i = 0; i < p1->size; ++i)
{
fwrite(&(p1->data[i]), sizeof(struct information), 1, pfWrite);
}
fclose(pfWrite);
pfWrite = NULL;
printf("信息保存成功\n");
}
void IncreasetCapacity(struct uct* p1)
{
if (p1->capacity == p1->size)
{
p1->capacity += 2;
struct information* ptr = (struct information*)realloc(p1->data, p1->capacity * sizeof(struct information));
if (ptr != NULL)
{
p1->data = ptr;
printf("增容成功!\n");
}
else
printf("增容失败!\n");
}
}
void addformation(struct uct* p1)
{
IncreasetCapacity(p1);
printf("请添加信息\n");
printf("请输入名字 -> \n");
scanf("%s", p1 ->data[p1->size].name);
printf("请输入年龄 -> \n");
scanf("%d",&(p1 ->data[p1->size].age));
printf("请输入性别 -> \n");
scanf("%s", p1 ->data[p1->size].set);
printf("请输入电话 -> \n");
scanf("%s", p1 ->data[p1->size].tele);
printf("请输入地址 -> \n");
scanf("%s", p1 ->data[p1->size].addr);
printf(" 添加成功 !\n");
p1->size++;
}
void showformation(struct uct* p1)
{
int i = 0;
if (p1->size == 0)
{
printf("内容为空\n");
}
else
{
printf("%-16s\t%-3s\t%-6s\t%-12s\t%-15s\n","姓名","年龄","性别","电话","地址");
for (i = 0; i < p1->size; ++i)
{
printf("%-16s\t%-3d\t%-5s\t%-12s\t%-15s\n", p1->data[i].name,
p1->data[i].age,
p1->data[i].set,
p1->data[i].tele,
p1->data[i].addr);
}
}
}
static int sertch(const struct uct* p1,char name[MAX_NAME])
{
int i = 0;
for (i = 0; i < p1->size; ++i)
{
if (0 == strcmp(p1->data[i].name, name))
return i;
}
return -1;
}
void subformation(struct uct* p1)
{
char name[MAX_NAME];
printf("请输入要删除人的名字");
scanf("%s", name);
int ret = sertch(p1,name);
if (ret == -1)
printf("该联系人不存在!\n\n");
else
{
while (ret < p1->size - 1)
{
p1->data[ret] = p1->data[ret + 1];
++ret;
}
p1->size--;
printf("删除成功!\n\n");
}
}
void searchformation(const struct uct* p1)
{
char name[MAX_NAME];
printf("请输入你要查找人的姓名:");
scanf("%s", name);
int i = sertch(p1, name);
if (i == -1)
printf("未查找到该联系人\n");
else
{
printf("%-16s\t %-3s\t %-6s\t %-12s\t %-15s\n", "姓名", "年龄", "性别", "电话", "地址");
printf("%-16s\t%-3d\t%-5s\t%-12s\t%-15s\n", p1->data[i].name,
p1->data[i].age,
p1->data[i].set,
p1->data[i].tele,
p1->data[i].addr);
}
}
void modifyformation(struct uct* p1)
{
char name[MAX_NAME];
printf("请输入要修改人的信息:");
scanf("%s", name);
int ret = sertch(p1, name);
if (ret == -1)
{
printf("未找到该联系人\n");
}
else
{
printf("请添加信息\n");
printf("请输入名字 -> \n");
scanf("%s", p1->data[ret].name);
printf("请输入年龄 -> \n");
scanf("%d", &(p1->data[ret].age));
printf("请输入性别 -> \n");
scanf("%s", p1->data[ret].set);
printf("请输入电话 -> \n");
scanf("%s", p1->data[ret].tele);
printf("请输入地址 -> \n");
scanf("%s", p1->data[ret].addr);
printf(" xiugai成功 !\n");
}
}
int cmp_stu_name(const void* p1,const void* p2)
{
return strcmp(((struct information*)p1)->name,((struct information*)p2)->name);
}
void realise(struct uct* p1)
{
free(p1->data);
p1->data = NULL;
}
void game()
{
int input = 0;
struct uct s1;
chushihua(&s1);
do
{
menu();
printf("\n");
printf("\n");
printf("please input a number:");
scanf_s("%d", &input);
int sz = s1.size;
switch (input)
{
case ADD:
addformation(&s1);
break;
case SUB:
subformation(&s1);
break;
case SEARCH:
searchformation(&s1);
break;
case MODIFY:
modifyformation(&s1);
break;
case SHOW:
showformation(&s1);
break;
case SORT://排序
qsort(s1.data, sz, sizeof(s1.data[0]),cmp_stu_name);
break;
case SAVE://保存
savecontact(&s1);
break;
case EXIT://退出
savecontact(&s1);
realise(&s1);
printf("退出 \n");
break;
default :
printf("输入错误!\n");
break;
}
} while (input);
}
明天把扫雷记录下来吧,有一个地方偏偏不信那个邪就认为自己的那种方法可以实现,改了好多次一直有问题,不过也只是很次的方法,能用自己想的方法实现递归和记录估计会比听懂别人的方法写出来要开心更久?
|