目标:使用大一上的知识实现可用二进制文件储存的商品管理系统。
系统功能:1.添加add:内容包括:商品名,商品价格,生产日期
2.展示所有商品show
3.从名单中删除商品delect
4.将商品按价格排序(从大到小)
5.保存(将名单以二进制文件的形式保存在文本文档)
开发工具;VS2022
一。搭建主函数:
代码如下:
#define _CRT_SECURE_NO_WARNINGS 1
#include"demo1.h"
void menu()
{
printf("************************\n");
printf("**1.add 2.show **\n");
printf("**3.delect 4.sort **\n");
printf("**5.save **\n");
printf("************************\n");
}
int main()
{
int input = 0;
struct goods goo;//创建表格
init(&goo);//初始化表格
do
{
menu();
printf("请输入:\n");
scanf("%d", &input);
switch (input)
{
case 1:
add(&goo);
break;
case 2:
show(&goo);
break;
case 3:
delect(&goo);
break;
case 4:
sort(&goo);
break;
case 5:
save(&goo);
break;
default:
break;
}
} while (input);
return 0;
}
二。资源文件:
#define _CRT_SECURE_NO_WARNINGS 1
#include"demo1.h"
void checkcap(struct table* ps)//增容函数
{
if (ps->size == ps->cap)
{
struct goods* ptr = realloc(ps->data, (ps->cap + 1) * sizeof(struct goods));
if (ptr != NULL)
{
ps->data = ptr;
ps->cap++;
printf("增容成功\n");
}
else
{
printf("增容失败\n");
}
}
}
void load(struct table*ps)
{
struct goods tmp={0};
FILE* pf = fopen("table.txt", "rb");
if (pf == NULL)
{
return 0;
}
while (fread(&tmp, sizeof(struct goods), 1, pf))
{
checkcap(ps);
ps->data[ps->size] = tmp;
ps->size++;
}
fclose(pf);
pf = NULL;
}
void add(struct table* ps)
{
printf("请输入商品名:\n");
scanf("%s", ps->data[ps->size].name);
printf("请输入商品价格:\n");
scanf("%d", &(ps->data[ps->size].prize));
printf("请输入商品生产日期:\n");
scanf("%d", &(ps->data[ps->size].productdate));
ps->size++;
printf("添加完毕\n");
}
void show(struct table* ps)
{
if (ps->size == 0)
{
printf("表格为空\n");
}
else
{
printf("%-20s\t%-5s\t%-10s\n", "品名", "价格", "生产日期");
int i = 0;
for (i = 0; i < ps->size; i++)
{
printf("%-20s\t%-5d\t%-10d\n",
ps->data[i].name,
ps->data[i].prize,
ps->data[i].productdate);
}
}
}
void init(struct table* ps)
{
ps->data = (struct goods*)malloc(3 * sizeof(struct goods));
if (ps->data == NULL)
{
return;
}
ps->size = 0;
ps->cap = 3;
load(ps);
}
void delect(struct table* ps)
{
printf("请输入想删除的商品名:\n");
char del_name[20];
int i = 0,j=0;
scanf("%s", del_name);
for (i = 0; i < ps->size; i++)
{
if (strcmp(del_name, ps->data[i].name) == 0)//在表中搜索和商品名对应的商品
{
for (j = i; j <= ps->size - 1; j++)//依次将被删除商品后面的商品移过来
{
ps->data[j] = ps->data[j + 1];
}
ps->size--;
printf("删除成功\n");
break;
}
if (i == ps->size - 1 && strcmp(del_name, ps->data[i].name) != 0)//表示无法查找到对应商品的情况
printf("表格中无此商品\n");
}
}
void sort(struct table* ps)
{
int i = 0, j = 0;
for (i = 0; i < ps->size; i++)
{
for (j = 0; j < ps->size - i - 1; j++)
{
if (ps->data[j].prize < ps->data[j + 1].prize)
{
struct goods tmp;
struct goods* p;
struct goods* q;
p = &ps->data[j];
q = &ps->data[j+1];
tmp = *p;
p = q;
q = &tmp;
ps->data[j] = *p;
ps->data[j + 1] = *q;
}
}
}
printf("按价格排序完毕\n");
}
void save(struct table* ps)
{
FILE* pf = fopen("table.txt", "wb");
if (pf == NULL)
{
return 0;
}
int i = 0;
for (i = 0; i < ps->size; i++)
{
fwrite(&ps->data[i],sizeof(struct goods),1,pf);
}
fclose(pf);
pf = NULL;
printf("保存成功\n");
}
三。头文件:
#pragma once
#include<stdio.h>
#include<string.h>
#include<errno.h>
#include<stdlib.h>
struct goods
{
char name[20];
int prize;
int productdate;
};
struct table
{
struct goods* data;//商品信息的地址
int size;//商品数量
int cap;//表格容量
};
void init(struct table* ps);
void add(struct table* ps);
void show(struct table* ps);
void delect(struct table* ps);
void sort(struct table* ps);//排序
void save(struct table* ps);//实现文件的存储
这是鄙人第一次在CSDN上发表博客,希望以简洁的方式记录编程学习生活,如代码有漏洞或更巧妙的实现方式,欢迎指正。