#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#define MAX 20
// 定义结构体
struct Product {
int id; // 编号
char category[50]; // 类别
char name[50]; // 名称
float price; // 价格
float discount; // 折扣比例
char productionDate[20]; // 生产时间
int inventory; // 存货数量
};
//逐行读取 CSV 文件内容并保存到结构体数组中
void checkFile(struct Product products[], int* len) {
FILE* file= fopen("products_list.csv", "r");
// 打开文件
if (file!=NULL) {
char line[300];//假设每行最多300字符
*len = 0;
// 逐行读取文件内容
while (fgets(line, 300, file) != NULL) {
// 使用逗号分隔符解析每一行的数据
sscanf_s(line, "%d,%[^,],%[^,],%f,%f,%[^,],%d\n",
&products[*len].id,
products[*len].category,50,
products[*len].name,50,
&products[*len].price,
&products[*len].discount,
products[*len].productionDate, 20,
&products[*len].inventory);
(*len)++; // 增加数组长度
}
fclose(file); // 关闭文件
}
}
// 将结构体数组数据逐行存储到 CSV 文件中
static void saveToCSV(struct Product products[], int *len) {
FILE* file;
errno_t err = fopen_s(&file, "products_list.csv", "w");
if (err == 0 && file != NULL) {
// 逐行写入数据
for (int i = 0; i < *len; i++) {
if (products[i].id != -1)
{
fprintf(file, "%d,%s,%s,%.2f,%.2f,%s,%d\n",
products[i].id, products[i].category, products[i].name,
products[i].price, products[i].discount, products[i].productionDate,
products[i].inventory);
}
}
fclose(file);
printf("数据成功写入到 'products_list.csv' 文件。\n");
}
else {
printf("无法打开文件 'products_list.csv' 进行写入。\n");
}
}
// 添加商品
static void addProduct(struct Product products[], int* len) {
struct Product newProduct = {0,"","",0,0,"",0};
// 用户输入产品信息
printf("请输入产品编号: ");
scanf_s("%d", &newProduct.id);
printf("请输入产品类别: ");
scanf_s("%s", &newProduct.category, 50);
printf("请输入产品名称: ");
scanf_s("%s", &newProduct.name, 50);
printf("请输入产品价格: ");
scanf_s("%f", &newProduct.price);
printf("请输入产品折扣比例: ");
scanf_s("%f", &newProduct.discount);
printf("请输入产品生产日期: ");
scanf_s("%s", &newProduct.productionDate, 20);
printf("请输入产品库存数量: ");
scanf_s("%d", &newProduct.inventory);
products[*len] = newProduct;
printf("修改成功!\n");
(*len)++;
saveToCSV(products, len);
system("pause");
}
// 修改商品信息函数
static void update_Pro(struct Product products[], int* len) {
int productId = -1;
// 获取用户输入的产品编号
printf("请输入要修改的商品编号: ");
scanf_s("%d", &productId);
// 查找该编号对应的商品在数组中的位置
int index = -1;
for (int i = 0; i < *len; i++) {
if (products[i].id == productId) {
index = i;
break;
}
}
// 如果找到对应编号的商品
if (index != -1) {
int choice;
// 提示用户选择要修改的属性
printf("请选择要修改的属性:\n");
printf("1. 商品类别\n");
printf("2. 商品名称\n");
printf("3. 商品价格\n");
printf("4. 折扣比例\n");
printf("5. 生产时间\n");
printf("6. 存货数量\n");
// 获取用户选择
printf("请输入选择编号: ");
scanf_s("%d", &choice);
// 根据用户选择进行相应的属性修改
switch (choice) {
case 1:
printf("请输入新的商品类别: ");
scanf_s("%49s", products[index].category, (unsigned)_countof(products[index].category));
break;
case 2:
printf("请输入新的商品名称: ");
scanf_s("%49s", products[index].name, (unsigned)_countof(products[index].name));
break;
case 3:
printf("请输入新的商品价格: ");
scanf_s("%f", &products[index].price);
break;
case 4:
printf("请输入新的折扣比例: ");
scanf_s("%f", &products[index].discount);
break;
case 5:
printf("请输入新的生产时间: ");
scanf_s("%19s", products[index].productionDate, (unsigned)_countof(products[index].productionDate));
break;
case 6:
printf("请输入新的存货数量: ");
scanf_s("%d", &products[index].inventory);
break;
default:
printf("无效的选择。\n");
break;
}
saveToCSV(products, len);
printf("修改成功,");
system("pause");
}
else {
printf("未找到该商品编号。\n");
}
}
// 打印商品信息
static void printProductInfo(struct Product product) {
printf("------------------------------\n");
printf("编号:%d\n", product.id);
printf("类别:%s\n", product.category);
printf("名称:%s\n", product.name);
printf("价格:%.2f\n", product.price);
printf("折扣比例:%.2f\n", product.discount);
printf("生产日期:%s\n", product.productionDate);
printf("库存数量:%d\n", product.inventory);
}
//查询商品信息
static void search_Pro(struct Product products[], int* len) {
int queryChoice;
// 用户选择查询方式
printf("请选择查询方式:\n");
printf("1. 编号\n");
printf("2. 类别\n");
printf("3. 名称\n");
printf("4. 生产日期\n");
printf("请输入选择: ");
scanf_s("%d", &queryChoice);
switch (queryChoice) {
case 1: // 根据编号查询
{
int productId;
printf("请输入要查询的商品编号: ");
scanf_s("%d", &productId);
for (int i = 0; i < *len; i++) {
if (products[i].id == productId) {
// 打印查询到的商品信息
printProductInfo(products[i]);
system("pause");
return; // 查询到商品后直接返回
}
}
// 未查询到商品
printf("未找到编号为 %d 的商品。\n", productId);
break;
}
case 2: // 根据类别查询
{
char category[50] = {0};
printf("请输入要查询的商品类别: ");
scanf_s("%s", category, 50); // 保留一个字符用于零终止符
for (int i = 0; i < *len; i++) {
if (strcmp(products[i].category, category) == 0) {
// 打印查询到的商品信息
printProductInfo(products[i]);
}
}
system("pause");
break;
}
case 3: // 根据名称查询
{
char name[50] = { 0 };
printf("请输入要查询的商品名称: ");
scanf_s("%49s", name, (unsigned)_countof(name));
for (int i = 0; i < *len; i++) {
if (strcmp(products[i].name, name) == 0) {
// 打印查询到的商品信息
printProductInfo(products[i]);
}
}
system("pause");
break;
}
case 4: // 根据生产日期查询
{
char productionDate[20] = { 0 };
printf("请输入要查询的商品生产日期: ");
scanf_s("%s", productionDate, 20);
for (int i = 0; i < *len; i++) {
if (strcmp(products[i].productionDate, productionDate) == 0) {
// 打印查询到的商品信息
printProductInfo(products[i]);
}
}
system("pause");
break;
}
default:
printf("无效的选择。\n");
system("pause");
break;
}
}
// 删除商品函数
static void delete_Pro(struct Product products[], int*
没有合适的资源?快使用搜索试试~ 我知道了~
C语言期末:商品管理系统
共53个文件
tlog:14个
vsidx:5个
pdb:4个
1 下载量 44 浏览量
2024-02-08
14:42:19
上传
评论
收藏 2.06MB RAR 举报
温馨提示
更多介绍请查看我的资源同名文章 商品管理系统 商品信息包括:包括编号、类别、名称、价格、折扣比例、生产时间 、存货数量等要求: 1、信息首先保存在文件中,然后打开文件进行读写操作,信息的改写必须与文件中保存的数据同步。 2、完成以下功能: 1 系统以菜单方式工作 2 增加商品信息 3 删除指定的商品信息 4 修改指定的商品信息 5 能按照多种方式查询(至少两种,如编号、类别、名称等 6 统计功能。按类别统计各种商品的存货数量,平均价格及存货量最多的商品信息 7 按照商品类别进行排序
资源推荐
资源详情
资源评论
收起资源包目录
商品管理系统.rar (53个子文件)
商品管理系统
CppProperties.json 382B
商品管理系统.sln 1KB
.vs
ProjectSettings.json 44B
slnx.sqlite 312KB
VSWorkspaceState.json 125B
商品管理系统
v17
Browse.VC.db 1.82MB
Solution.VC.db 428KB
.suo 27KB
.wsuo 31KB
ipch
AutoPCH
bf609510d35eb15f
MAIN.ipch 3.13MB
f692921bcd3597e4
MAIN.ipch 2.38MB
c2a17868f663de45
MAIN.ipch 3.13MB
FileContentIndex
df12550c-d814-404d-a6f1-78ce8be31fd0.vsidx 13KB
61f1cab7-ae26-4b31-badd-3fa5938c4ccb.vsidx 10KB
7ef939b0-7722-41ee-9ff7-0d983fa5117b.vsidx 12KB
8a78da7e-7dac-4bd0-bcee-6834a3fd695a.vsidx 31KB
e62d2bef-b98c-4504-9f2b-9b255d917230.vsidx 8KB
x64
Debug
商品管理系统.exe 76KB
商品管理系统.pdb 1.46MB
商品管理系统
商品管理系统.vcxproj 6KB
products_list.csv 238B
商品管理系统.vcxproj.user 168B
商品管理系统.vcxproj.filters 964B
x64
Debug
vc143.pdb 84KB
商品管理系统.ilk 1.04MB
vc143.idb 51KB
商品管理系统.exe.recipe 310B
main.obj 65KB
商品管理系统.tlog
CL.write.1.tlog 320B
商品管理系统.lastbuildstate 164B
Cl.items.tlog 135B
CL.command.1.tlog 638B
link.command.1.tlog 1KB
link.read.1.tlog 2KB
link.write.1.tlog 324B
CL.read.1.tlog 3KB
商品管理系统.log 114B
main.c 12KB
Debug
vc143.pdb 76KB
商品管理系统.ilk 467KB
vc143.idb 43KB
商品管理系统.exe.recipe 306B
main.obj 43KB
商品管理系统.tlog
CL.write.1.tlog 296B
商品管理系统.lastbuildstate 166B
Cl.items.tlog 131B
CL.command.1.tlog 670B
link.command.1.tlog 1018B
link.read.1.tlog 3KB
link.write.1.tlog 292B
CL.read.1.tlog 3KB
Debug
商品管理系统.exe 47KB
商品管理系统.pdb 964KB
共 53 条
- 1
资源评论
a辰龙a
- 粉丝: 2549
- 资源: 58
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- Pytorch-pytorch深度学习教程之前馈神经网络.zip
- Pytorch-pytorch深度学习教程之线性回归.zip
- Pytorch-pytorch深度学习教程之基本操作.zip
- 基于QT的地图可视化桌面系统后台数据库为MySQL5.7源码.zip
- 基于simulink的PLL锁相环系统仿真【包括模型,文档,参考文献,操作步骤】
- 基于EM-GMM模型的目标跟踪和异常行为检测matlab仿真【包括程序,注释,参考文献,操作步骤,说明文档】
- 2109010044_胡晨燕_选课管理数据库设计与实现.prj
- 帕鲁介绍的PPT备份没什么好下的
- demo1-202405
- 两种方式修改Intel网卡MAC地址
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功