C++ 小型超市管理系统
程序员文章站
2022-10-31 14:26:37
#include
#include
#include
#include
#include
#define password 123456 //管理员密码(隐藏...
#include #include #include #include #include #define password 123456 //管理员密码(隐藏) #define close_passward 000000 //用户关闭程序密码(隐藏) #define shoppingcartmax 20 //购物车最大容量 #define goods_path goods_records.dat //商品文件存放 #define sale_path sale_records.dat //销售文件存放 #pragma warning(disable: 4996) //屏蔽安全警告 //商品信息 struct good { int id; //商品编号 char name[20]; //商品名称 char kind; //商品类型 int num; //商品数量 float price1; //进货价格 float price2; //出售价格 float date; //生产日期 int baozhiqi; //保质期 struct good *next; }*good; //销售信息 struct sale { int id; //商品编号 char name[20]; //商品名称 float price1; //进货价格 float price2; //出售价格 int num;//购买数量 char buyer[20];//顾客名字 char time[128];//购买时间 struct sale *next; }*sale; void initialization(); //初始化界面 void userwindows(); //用户主程序 void usermenu(); //用户主菜单 void userscan(); //用户浏览 int usercart(); //购物车 void goodsshow(struct good *good); //商品信息显示 void userpay(struct sale *shopping_cart, struct good *address[]); //结账 void administratorwindows(); //管理员主程序 void administratormenu(); //管理员主菜单 void goodsadd(); //新购商品入库 void goodsdetail(); //商品信息详情 void goodschange(); //商品信息更改 void goodsdelete(); //商品信息删除 void changeoperation(struct good *exist); //更改操作 void deleteoperation(struct good *good); //删除操作 void goodsbrowse(); //商品信息浏览 void allbrowse(); //浏览所有商品 void browsebytype(); //按类型浏览商品 void salemanage(); //商品销售管理 void saveandexit(); //保存并退出 int goodssavetofile(); //保存商品信息 int salesavetofile(); //保存销售信息 int goodsreadfromfile(); //读取商品记录 int salereadfromfile(); //读取销售记录 struct good *goodsfind(); //商品信息查找选择 struct good *goodsidfind(int goods_number); //根据id查找 struct good *goodsnamefind(char goods_name[20]); //根据名称查找 int main() { initialization(); while (1)//程序运行 { userwindows(); //用户员主界面 administratorwindows(); //管理员主界面 } return 0; } //初始化界面 void initialization() { printf("\n\n"); printf("\t*******************************************************************\n"); printf("\t| |\n"); printf("\t| |\n"); printf("\t| 欢迎使用 |\n"); printf("\t| |\n"); printf("\t| |\n"); printf("\t| 小型超市管理系统 |\n"); printf("\t| |\n"); printf("\t| |\n"); printf("\t| |\n"); printf("\t| 祝您使用愉快 |\n"); printf("\t| |\n"); printf("\t| |\n"); printf("\t| |\n"); printf("\t*******************************************************************\n"); sale = (struct sale *)malloc(sizeof(struct sale)); good = (struct good *)malloc(sizeof(struct good)); sale->next = null; good->next = null; salereadfromfile();//读取销售记录 goodsreadfromfile();//读取商品记录 _sleep(2000); //延迟 } //用户主程序 void userwindows() { int choose = 0; while (1) { usermenu(); //用户主菜单 scanf("%d", &choose); if (choose == 1) { userscan(); } else if (choose == 2) { usercart(); //开启购物车之旅 } else if (choose ==close_passward) { saveandexit(); } else if (choose == password) break; } } //用户主菜单 void usermenu() { system("cls"); printf("\n\n"); printf("\t*************************************************************\n"); printf("\t| |\n"); printf("\t| |\n"); printf("\t| 相信美好的事情即将发生 |\n"); printf("\t| |\n"); printf("\t| |\n"); printf("\t| 请选择操作 |\n"); printf("\t| |\n"); printf("\t| 【1】浏览商品 【2】购买商品 |\n"); printf("\t| |\n"); printf("\t| |\n"); printf("\t*************************************************************\n"); } //用户浏览 void userscan() { if (good->next == null) { printf("\n\n\t抱歉,商店商品已全售完,店主正在忙碌进货中..."); _sleep(2000); } else { system("cls"); struct good *goods_pointer = good; while (goods_pointer->next != null) { goods_pointer = goods_pointer->next; goodsshow(goods_pointer); } printf("\n\t是否购买商品?(按【y】确认)"); char flag; fflush(stdin); flag = getch(); if (flag == 'y' || flag == 'y') { usercart();//购物车 } } } //商品信息显示 void goodsshow(struct good *goods) { printf("\n\n\t***************商品信息******************"); printf("\n\t商品编号...%30d\n", goods->id); printf("\n\t-----------------------------------------"); printf("\n\t商品名称...%30s\n", goods->name); printf("\n\t-----------------------------------------"); printf("\n\t商品数量...%30d\n", goods->num); printf("\n\t-----------------------------------------"); printf("\n\t出售价格...%30.2f\n", goods->price2); printf("\n\t-----------------------------------------"); printf("\n\t生产日期...%30.2f\n", goods->date); printf("\n\t-----------------------------------------"); printf("\n\t保质期...%32d\n", goods->baozhiqi); printf("\n\t-----------------------------------------\n\n"); } //购物车 int usercart() { if (good->next == null) { printf("\n\n\t抱歉,商店商品已全售完,店主正在忙碌进货中..."); _sleep(2000); return 0; } system("cls"); printf("\n\n\n\t即将进入选购界面,请稍等..."); _sleep(2000); struct sale *shopping_cart; //购物车 shopping_cart = (struct sale *)malloc(sizeof(struct sale)); shopping_cart->next = null; //初始化购物车列表 int i = 0; struct good *goods, *address[shoppingcartmax]; //存放商品地址; for ( ; i < shoppingcartmax; i++) { goods = goodsfind(); //查找商品 if (goods == null) { printf("\n\n\n\t\t抱歉,没有您需要的商品!"); _sleep(2000); continue; } else if (goods == good) //在选择商品时选择【结束操作】 { system("cls"); if (shopping_cart->next == null) { printf("\n\n\n\t\t由于您购物车空空的,将结束本次购物!"); printf("\n\n\n\t购物系统正在关闭,请稍等..."); free(shopping_cart); //销毁购物车 _sleep(4000); break; } else { printf("\n\n\n\t您取消了本次选购,是否前往支付?"); printf("\n\n\t【y】前往支付 【任意键】放弃购物"); char choose1; choose1 = getch(); if (choose1 == 'y' || choose1 == 'y') { free(goods); userpay(shopping_cart, address); //结账 } else { printf("\n\n\n\t购物系统正在关闭,请稍等..."); free(shopping_cart); //销毁购物车 _sleep(2000); break; } } } system("cls"); goodsshow(goods); //显示商品信息 struct sale *cart_pointer = shopping_cart; int judge = 123; //判断是否重复购买,初始化赋随机值 while (cart_pointer->next != null) //查找购物车是否已存在该商品 { cart_pointer = cart_pointer->next; judge = strcmp(cart_pointer->name, goods->name); if (judge == 0) { printf("\t【购物车已有%d件该商品!,若要继续购买,将自动为您合并!】\n", cart_pointer->num); break; } } struct sale *sale; //可移动指针对销售进行操作 sale = (struct sale *)malloc(sizeof(struct sale)); printf("\n\n请输入要购买的数量:(输入【0】重新选择)\t"); scanf("%d", &sale->num); if (sale->num == 0) continue; if (judge == 0) //准备合并相同商品 { sale->num = (cart_pointer->num) + (sale->num); } if ((sale->num) > (goods->num)) //判断是否超过最大商品数量 { printf("\n\n\n\t【友情提示:很抱歉,该商品由于销售太火爆,目前只剩下%d件了】", goods->num); printf("\n\n\t\t【a】继续,购买%d件 【b】重新选择 【任意键】结束购物\n", goods->num); char choose; fflush(stdin); choose = getch(); if (choose == 'a' || choose == 'a') { sale->num = goods->num; printf("\n\n\n\t【谢谢您的理解和支持!】"); } else if (choose == 'b' || choose == 'b') continue; else break; } if (judge == 0) //购物车存在商品时,合并商品 { cart_pointer->num = sale->num; printf("\n\n\t【由于购物车存在该商品,已自动为您合并!】"); } else //购物车不存在商品时,添加到购物车 { address[i] = goods; // 记录商品地址 strcpy(sale->name, goods->name); // 记录商品名称 sale->id = goods->id; sale->price1 = goods->price1; sale->price2 = goods->price2; sale->next = null; while (cart_pointer->next != null) { cart_pointer = cart_pointer->next; } cart_pointer->next = sale;//添加到购物列车中 printf("\n\n\t已成功添加商品到购物车!"); } if (i == (shoppingcartmax-1))//购物车满时操作 { printf("\n\n\t【购物车已达到最大容量,将自动为您跳往支付界面!】"); _sleep(2000); userpay(shopping_cart, address); //结账 break; } printf("\n\n\n\t\t【1】:前往支付 【2】:继续浏览 【3】:放弃购物\n"); char choice; choice = getch(); if (choice == '1') { userpay(shopping_cart,address); //结账 break; } else if (choice == '3') break; } return 1; } //结账 void userpay(struct sale *shopping_cart, struct good *address[]) { system("cls"); float money = 0; struct sale *sale_pointer = shopping_cart; printf("\n\n安全交易系统已打开!"); printf("\n\n订单开始处理...\n...\n\n订单审核通过...\n\n正在生成订单...\n\n订单生成完成!"); printf("\n\n\n\t*************************订单详情**************************"); printf("\n\n\t商品名称\t商品单价\t购买数量\t消费总价\n"); while (sale_pointer->next != null) { sale_pointer = sale_pointer->next; printf("\n\t%s\t\t%.2f\t\t%d\t\t%.2f\t\t",sale_pointer->name, sale_pointer->price2, sale_pointer->num, (sale_pointer->price2)*(sale_pointer->num)); money = money + (sale_pointer->price2)*(sale_pointer->num); } printf("\n\n\t***********************************************************"); printf("\n\t总共消费金额为:%.2f", money); printf("\n\n\n\t确定完成交易?\t按【y】确认支付 【任意键】取消支付\n\t"); fflush(stdin); char flag; flag = getch(); if (flag == 'y' || flag == 'y') { char name[20]; printf("\n\n\t请输入您的名字:"); scanf("%s", name); printf("\n\n系统正在完成交易,请稍等..."); int i; struct good *goods_address; //临时存放商品地址 sale_pointer = shopping_cart; for (i = 0; sale_pointer->next != null; i++) { sale_pointer = sale_pointer->next; strcpy(sale_pointer->buyer, name); //记录购买者名字 _tzset(); _strdate(sale_pointer->time); //记录购买时间 goods_address = address[i]; goods_address->num = (goods_address->num) - (sale_pointer->num); //更改商品数量 if (goods_address->num == 0) { deleteoperation(goods_address);//清除数量为0的商品记录 } } //增加商品销售记录 sale_pointer = sale; while (sale_pointer->next != null) //定位到销售列表末尾 { sale_pointer = sale_pointer->next; } sale_pointer->next = shopping_cart->next; printf("\n\n\n\t交易完成,谢谢惠顾! o(∩_∩)o~\n\n"); printf("\n\n\t交易系统正在关闭,请稍后..."); _sleep(5000); } } //管理员主程序 void administratorwindows() { int flag = 1; //判断是否进入用户界面的标志 char choose; while (flag) { administratormenu(); choose = getch(); system("cls"); switch (choose) { case '1': { goodsadd(); //新购商品入库 break; } case '2': { goodsdetail(); //商品信息详情 break; } case '3': { goodschange(); //商品信息更改 break; } case '4': { goodsdelete(); //商品信息删除 break; } case '5': { goodsbrowse(); //商品信息浏览 break; } case '6': { salemanage(); //商品销售管理 break; } case '7': { system("cls"); flag = 0; //flag 设为0,准备进入用户界面 printf("\n\n\n\t即将进入用户界面,请稍等..."); _sleep(2000); break; //进入用户界面 } case '0': { saveandexit(); //保存并退出 exit(0); } } fflush(stdin); } } //管理员主菜单 void administratormenu() { system("cls"); printf("\n\t|-----------------------------------------------------------------|"); printf("\n\t| |"); printf("\n\t| 【小型超市管理系统】 |"); printf("\n\t| |"); printf("\n\t|-----------------------------------------------------------------|"); printf("\n\t| |"); printf("\n\t| 【1】∽∽∽∽∽∽∽【 新购商品入库 】 |"); printf("\n\t| |"); printf("\n\t| 【2】∽∽∽∽∽∽∽【 商品信息详情 】 |"); printf("\n\t| |"); printf("\n\t| 【3】∽∽∽∽∽∽∽【 商品信息更改 】 |"); printf("\n\t| |"); printf("\n\t| 【4】∽∽∽∽∽∽∽【 商品信息删除 】 |"); printf("\n\t| |"); printf("\n\t| 【5】∽∽∽∽∽∽∽【 商品信息浏览 】 |"); printf("\n\t| |"); printf("\n\t| 【6】∽∽∽∽∽∽∽【 商品销售管理 】 |"); printf("\n\t| |"); printf("\n\t| 【7】∽∽∽∽∽∽∽【 进入用户界面 】 |"); printf("\n\t| |"); printf("\n\t| 【0】∽∽∽∽∽∽∽【 保存退出程序 】 |"); printf("\n\t| |"); printf("\n\t|-----------------------------------------------------------------|"); printf("\n\n\t\t\t\t请输入您要选择的菜单...."); } //新购商品入库 void goodsadd() { while (1) { system("cls"); printf("\n\n\t\t r:日用品 w:文化用品 s:食品 d:电子产品"); printf("\n\n请输入商品信息:"); struct good *goods,*exist; //指针 *exist 判断是否已存在该商品 goods = (struct good *)malloc(sizeof(struct good)); printf("\n\n商品编号:"); scanf("%d", &goods->id); exist = goodsidfind(goods->id); //检查编号是否重复 if (exist != null) { printf("\n\n\t该商品信息已存在!请选择操作:\n\t【1】:重新输入\t【2】:前往编辑"); char choose1; choose1 = getch(); if (choose1 == '2') { changeoperation(exist); //更改操作 } free(goods); continue; } printf("商品名称:"); scanf("%s", &goods->name); exist = goodsnamefind(goods->name); //检查名称是否重复 if (exist != null) { printf("\n\n\t该商品信息已存在!请选择操作:\n\t【1】重新输入\t【2】前往编辑"); char choose2; choose2 = getch(); if (choose2 == '2') { changeoperation(exist); //更改操作 } free(goods); continue; } fflush(stdin); printf("商品类型:"); scanf("%c", &goods->kind); printf("商品数量:"); scanf("%d", &goods->num); printf("进货价格:"); scanf("%f", &goods->price1); printf("出售价格:"); scanf("%f", &goods->price2); printf("生产日期:"); scanf("%f", &goods->date); printf("保质期:"); scanf("%d", &goods->baozhiqi); system("cls"); printf("\n\n\t************************************************************************"); printf("\n\n\t 编号\t 名称\t 类型\t 数量\t 进价\t 售价\t 生产日期\t 保质期 \n\n"); printf("\t %d\t %s\t %c\t %d\t %.2f\t %.2f\t %.2f\t\t %d \n", goods->id, goods->name, goods->kind, goods->num, goods->price1, goods->price2, goods->date, goods->baozhiqi); printf("\n\n\t************************************************************************"); printf("\n\n\t确定录入此商品信息?\t(按【y】确认)\n\n"); struct good *goods_pointer = good; char check1; fflush(stdin); check1 = getch(); if (check1 == 'y' || check1 == 'y') { while (goods_pointer->next != null) { goods_pointer = goods_pointer->next; //定位到商品列表末尾 } goods->next = goods_pointer->next; goods_pointer->next = goods; } printf("\n\n\t是否继续录入商品信息?\t(按【y】确认)"); char check2; check2 = getch(); if (check2 != 'y' || check2 != 'y') { printf("\n\n\n\n\t【已结束商品信息录入,即将返回主程序...】"); _sleep(2000); break; } } } //商品信息详情 void goodsdetail() { struct good *goods; goods = goodsfind(); //查找是存在该商品 if (goods == null) { printf("\n\n\t【抱歉,不存在该商品!】"); _sleep(2000); } else if (goods == good) { printf("\n\n\n\t\t正在返回程序,请稍等..."); _sleep(2000); } else { system("cls"); printf("\n\n\t***********************************************************************"); printf("\n\n\t 编号\t 名称\t 类型\t 数量\t 进价\t 售价\t 生产日期\t 保质期 \n\n"); printf("\t %d\t %s\t %c\t %d\t %.2f\t %.2f\t %.2f\t\t %d \n", goods->id, goods->name, goods->kind, goods->num, goods->price1, goods->price2, goods->date, goods->baozhiqi); printf("\n\n\t***********************************************************************"); printf("\n\n\t按任意键继续..."); getch(); } } //商品信息查找选择 struct good *goodsfind() { if (good->next == null) { printf("\n\n\t\t抱歉,目前商店没有商品信息!"); _sleep(2000); return good; } while (1) { system("cls"); printf("\n\n\n\n"); printf("\t|-----------------------------------------------------------|\n"); printf("\t| |\n"); printf("\t| 【a】根据商品编号id选择 |\n"); printf("\t| |\n"); printf("\t| 【b】根据商品名称name选择 |\n"); printf("\t| |\n"); printf("\t| 【c】结束操作 |\n"); printf("\t| |\n"); printf("\t|-----------------------------------------------------------|\n"); printf("\t| |\n"); printf("\t| ∞请选择操作∞ |\n"); printf("\t| |\n"); printf("\t|-----------------------------------------------------------|\n"); struct good *goods; char choose; fflush(stdin); choose = getch(); if (choose == 'a' || choose == 'a') { printf("\n\t请输入商品编号:"); int goods_number; scanf("%d", &goods_number); goods = goodsidfind(goods_number); return goods; } else if (choose == 'b' || choose == 'b') { printf("\n\t请输入商品名称:"); fflush(stdin); char goods_name[20];//待输入名称 gets(goods_name); goods = goodsnamefind(goods_name); return goods; } else if (choose == 'c' || choose == 'c') return good; } } //根据id查找 struct good *goodsidfind(int goods_number) { struct good *goods=good; while (goods->next != null) { goods = goods->next; if (goods->id == goods_number) { return goods; } } return null; } //根据名称查找 struct good *goodsnamefind(char goods_name[]) { struct good *goods=good; while (goods->next != null) { goods = goods->next; if (strcmp(goods->name, goods_name) == 0) { return goods; } } return null; } //商品信息更改 void goodschange() { struct good *goods; goods = goodsfind(); //查找是存在该商品 if (goods == null) { printf("\n\n\t【不存在该商品!】"); _sleep(2000); } else if (goods == good) { printf("\n\n\n\t\t正在返回程序,请稍等..."); _sleep(2000); } else { changeoperation(goods); //更改操作 } } //更改操作 void changeoperation(struct good *goods) { int end = 0; char choose; while (1) { system("cls"); printf("\n\n\t***************************************************************************"); printf("\n\n\t编号\t 名称 \t 类型\t 数量\t 进价\t 售价\t 生产日期\t保质期 \n\n"); printf("\t %d\t %s\t %c\t %d\t %.2f\t %.2f\t %.2f\t\t %d \n", goods->id, goods->name, goods->kind, goods->num, goods->price1, goods->price2, goods->date, goods->baozhiqi); printf("\n\n\t***************************************************************************"); printf("\n\n\t\t【r】:日用品 【w】:文化用品 【s】:食品 【d】:电子产品\n"); printf("\n\t【1】商品编号\t【2】商品名称\t【3】商品类型\t【4】商品数量"); printf("\n\t【5】进货价格\t【6】出售价格\t【7】生产日期\t【8】保质期\n\t【0】保存退出\n"); printf("\n\t请选择序号进行操作:\n"); choose = getch(); switch (choose) { case '1': { printf("\n\t请输入商品编号:"); scanf("%d", &goods->id); break; } case '2': { printf("\n\t请输入商品名称:"); scanf("%s", &goods->name); break; } case '3': { printf("\n\t请输入商品类型:"); fflush(stdin); scanf("%c", &goods->kind); break; } case '4': { printf("\n\t请输入商品数量:"); scanf("%d", &goods->num); break; } case '5': { printf("\n\t请输入进货价格:"); scanf("%f", &goods->price1); break; } case '6': { printf("\n\t请输入出售价格:"); scanf("%f", &goods->price2); break; } case '7': { printf("\n\t请输入生产日期:"); scanf("%f", &goods->date); break; } case '8': { printf("\n\t请输入保质期:"); scanf("%d", &goods->baozhiqi); break; } case '0': { printf("\n\n\t正在保存,请稍等..."); _sleep(2000); end = 1; break; } } if (end == 1) break; } } //商品信息删除 void goodsdelete() { struct good *goods; goods = goodsfind(); //查找是存在该商品 if (goods == null) { printf("\n\n\t【抱歉,不存在该商品!】"); _sleep(2000); } else if (goods == good) { printf("\n\n\n\t\t正在返回程序,请稍等..."); _sleep(2000); } else { printf("\n\n\t************************************************************************"); printf("\n\n\t 编号\t 名称\t 类型\t 数量\t 进价\t 售价\t 生产日期\t 保质期 \n\n"); printf("\t %d\t %s\t %c\t %d\t %.2f\t %.2f\t %.2f\t\t %d \n", goods->id, goods->name, goods->kind, goods->num, goods->price1, goods->price2, goods->date, goods->baozhiqi); printf("\n\n\t************************************************************************"); printf("\n警告:是否确认删除?!(输入【y】确认)"); char choose; fflush(stdin); choose = getch(); if (choose == 'y' || choose == 'y') { deleteoperation(goods); //删除操作 } } } //删除操作 void deleteoperation(struct good *goods) { struct good *goods_pointer = good; while (goods_pointer->next != goods) { goods_pointer = goods_pointer->next; } goods_pointer->next = goods->next; free(goods); printf("\n\n\t操作成功!"); _sleep(300); } //商品信息浏览 void goodsbrowse() { system("cls"); if (good->next == null) { printf("\n\n\t\t抱歉,目前商店没有商品信息!"); _sleep(2000); } else { printf("\t\t请选择浏览方式:\n\n\t【1】全部浏览\t【2】按类型浏览"); char choose; while (1) { choose = getch(); if (choose == '1') { allbrowse(); break; } else if (choose=='2') { browsebytype(); break; } } printf("\n\n\n\t按任意键继续..."); getch(); } } //浏览所有商品 void allbrowse() { system("cls"); struct good *goods_pointer = good; printf("\n\n\t*********************************************************************************"); printf("\n\n\t 编号\t 名称\t\t 类型\t 数量\t 进价\t 售价\t 生产日期\t保质期 \n\n"); while (goods_pointer->next != null) { goods_pointer = goods_pointer->next; printf("\t %d\t %s\t\t %c\t %d\t %.2f\t %.2f\t %.2f\t\t%d \n", goods_pointer->id, goods_pointer->name, goods_pointer->kind, goods_pointer->num, goods_pointer->price1, goods_pointer->price2, goods_pointer->date, goods_pointer->baozhiqi); } printf("\n\n\t*********************************************************************************"); } //按类型浏览商品 void browsebytype() { system("cls"); printf("\n\n\t\t 【r】:日用品 【w】:文化用品 【s】:食品 【d】:电子产品"); printf("\n\n\n请选择要浏览的商品类型:"); char type; type = getch(); system("cls"); struct good *goods_pointer = good; printf("\n\n\t*********************************************************************"); printf("\n\n\t 编号\t 名称\t 类型\t 数量\t 进价\t 售价\t 生产日期\t 保质期 \n"); while (goods_pointer->next != null) { goods_pointer = goods_pointer->next; if (goods_pointer->kind == type) printf("\n\t %d\t %s\t %c\t %d\t %.2f\t %.2f\t %.2f\t\t %d", goods_pointer->id, goods_pointer->name, goods_pointer->kind, goods_pointer->num, goods_pointer->price1, goods_pointer->price2, goods_pointer->date, goods_pointer->baozhiqi); } printf("\n\n\t*********************************************************************"); } //商品销售管理 void salemanage() { system("cls"); printf("\n\n\t\t销售情况查看及利润统计"); printf("\n\n\t【r】:日用品 【w】:文化用品 【s】:食品 【d】:电子产品"); printf("\n\n\t***********************************************************"); if (sale->next == null) { printf("\n\n目前暂无销售记录!"); _sleep(2000); } else { printf("\n\n\t编号\t名称\t进价\t售价\t数量\t名字\t时间\n\n"); int number = 0;//商品数量 float profit = 0;//商品利润 struct sale *sale_pointer = sale; while (sale_pointer->next != null) { sale_pointer = sale_pointer->next; printf("\t %d\t %s\t %.2f\t %.2f\t %d\t %s\t %s\n",sale_pointer->id, sale_pointer->name, sale_pointer->price1, sale_pointer->price2, sale_pointer->num, sale_pointer->buyer, sale_pointer->time); profit = profit + ((sale_pointer->price2) - (sale_pointer->price1))*(sale_pointer->num); number = number + (sale_pointer->num); } printf("\n\t***********************************************************"); printf("\n\n\t共销售商品%d件,纯利润为%.2f", number, profit); printf("\n\n\n\t按任意键继续..."); getch(); } } //保存并退出 void saveandexit() { salesavetofile();//保存销售信息 goodssavetofile();//保存商品信息 system("cls"); printf("\n\n\n\n\n\n"); printf("\n\t\t #〓§〓〓〓〓〓§〓〓〓〓〓〓§〓〓〓〓〓§〓# "); printf("\n\t\t ↓ ↓ ↓ ↓ "); printf("\n\t\t ☆★☆ ☆★☆ ☆★☆ ☆★☆ "); printf("\n\t\t ☆ 谢 ☆ ☆ 谢 ☆ ☆ 使 ☆ ☆ 用 ☆ "); printf("\n\t\t ☆★☆ ☆★☆ ☆★☆ ☆★☆ "); printf("\n\t\t ↓ ↓ ↓ ↓ "); printf("\n\t\t ※ ※ ※ ※ "); _sleep(3000); exit(0); } //保存商品信息 int goodssavetofile() { file *fp; char buff[512]; if ((fp = fopen("goods_path", "wb")) == null) { sprintf(buff, "can not open file:%s to create", "goods_path"); _sleep(2000); return 0; } struct good *goods_pointer = good; //定位到链表头部 while (goods_pointer->next != null) { goods_pointer = goods_pointer->next; fwrite(goods_pointer, sizeof(struct good), 1, fp);//一个个的保存到文件中去 } fclose(fp); return 1; } //保存销售信息 int salesavetofile() { file *fp; char buff[512]; if ((fp = fopen("sale_path", "wb")) == null) { sprintf(buff, "can not open file:%s to create", "sale_path"); _sleep(2000); return 0; } struct sale *sale_pointer = sale; //定位到链表头部 while (sale_pointer->next != null) { sale_pointer = sale_pointer->next; fwrite(sale_pointer, sizeof(struct sale), 1, fp);//一个个的保存到文件中去 } fclose(fp); return 1; } //读取商品记录 int goodsreadfromfile() { file *fp; char buff[512]; if ((fp = fopen("goods_path", "rb")) == null) { sprintf(buff, "can not open file:%s to read", "goods_path"); return 1; } int number; fseek(fp, 0, 2); //定位到文件末尾 number = ftell(fp) / sizeof(struct good); //求出总结点数 fseek(fp, 0, 0); //定位到文件的开始处 int i; struct good *goods; struct good *goods_pointer = good; for (i = 0; i < number; i++) { goods = (struct good *)malloc(sizeof(struct good)); fread(goods, sizeof(struct good), 1, fp); goods->next = null; while (goods_pointer->next != null) //定位到商品列表末尾 { goods_pointer = goods_pointer->next; } goods_pointer->next = goods;//插入到链表当中 } fclose(fp);//关闭文件 return 0; } //读取销售记录 int salereadfromfile() { file *fp; char buff[512]; if ((fp = fopen("sale_path", "rb")) == null) { sprintf(buff, "can not open file:%s to read", "sale_path"); return 1; } int number; fseek(fp, 0, 2); //定位到文件末尾 number = ftell(fp) / sizeof(struct sale); //求出总结点数 fseek(fp, 0, 0); //定位到文件的开始处 int i; struct sale *sale; struct sale *sale_pointer = sale; for (i = 0; i < number; i++) { sale = (struct sale *)malloc(sizeof(struct sale)); fread(sale, sizeof(struct sale), 1, fp); sale->next = null; while (sale_pointer->next != null) //定位到销售列表末尾 { sale_pointer = sale_pointer->next; } sale_pointer->next = sale; //插入到链表当中 } fclose(fp); //关闭文件 return 0; }
上一篇: C++杂忆集(一)