#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("nn"); 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("nn"); 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("nnt抱歉,商店商品已全售完,店主正在忙碌进货中..."); _sleep(2000); } else { system("cls"); struct good *goods_pointer = good; while (goods_pointer->next != null) { goods_pointer = goods_pointer->next; goodsshow(goods_pointer); } printf("nt是否购买商品?(按【y】确认)"); char flag; fflush(stdin); flag = getch(); if (flag == 'y' || flag == 'y') { usercart();//购物车 } } } //商品信息显示 void goodsshow(struct good *goods) { printf("nnt***************商品信息******************"); printf("nt商品编号...%30dn", goods->id); printf("nt-----------------------------------------"); printf("nt商品名称...%30sn", goods->name); printf("nt-----------------------------------------"); printf("nt商品数量...%30dn", goods->num); printf("nt-----------------------------------------"); printf("nt出售价格...%30.2fn", goods->price2); printf("nt-----------------------------------------"); printf("nt生产日期...%30.2fn", goods->date); printf("nt-----------------------------------------"); printf("nt保质期...%32dn", goods->baozhiqi); printf("nt-----------------------------------------nn"); } //购物车 int usercart() { if (good->next == null) { printf("nnt抱歉,商店商品已全售完,店主正在忙碌进货中..."); _sleep(2000); return 0; } system("cls"); printf("nnnt即将进入选购界面,请稍等..."); _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("nnntt抱歉,没有您需要的商品!"); _sleep(2000); continue; } else if (goods == good) //在选择商品时选择【结束操作】 { system("cls"); if (shopping_cart->next == null) { printf("nnntt由于您购物车空空的,将结束本次购物!"); printf("nnnt购物系统正在关闭,请稍等..."); free(shopping_cart); //销毁购物车 _sleep(4000); break; } else { printf("nnnt您取消了本次选购,是否前往支付?"); printf("nnt【y】前往支付 【任意键】放弃购物"); char choose1; choose1 = getch(); if (choose1 == 'y' || choose1 == 'y') { free(goods); userpay(shopping_cart, address); //结账 } else { printf("nnnt购物系统正在关闭,请稍等..."); 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("nn请输入要购买的数量:(输入【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("nnnt【友情提示:很抱歉,该商品由于销售太火爆,目前只剩下%d件了】", goods->num); printf("nntt【a】继续,购买%d件 【b】重新选择 【任意键】结束购物n", goods->num); char choose; fflush(stdin); choose = getch(); if (choose == 'a' || choose == 'a') { sale->num = goods->num; printf("nnnt【谢谢您的理解和支持!】"); } else if (choose == 'b' || choose == 'b') continue; else break; } if (judge == 0) //购物车存在商品时,合并商品 { cart_pointer->num = sale->num; printf("nnt【由于购物车存在该商品,已自动为您合并!】"); } 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("nnt已成功添加商品到购物车!"); } if (i == (shoppingcartmax-1))//购物车满时操作 { printf("nnt【购物车已达到最大容量,将自动为您跳往支付界面!】"); _sleep(2000); userpay(shopping_cart, address); //结账 break; } printf("nnntt【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("nn安全交易系统已打开!"); printf("nn订单开始处理...n...nn订单审核通过...nn正在生成订单...nn订单生成完成!"); printf("nnnt*************************订单详情**************************"); printf("nnt商品名称t商品单价t购买数量t消费总价n"); while (sale_pointer->next != null) { sale_pointer = sale_pointer->next; printf("nt%stt%.2ftt%dtt%.2ftt",sale_pointer->name, sale_pointer->price2, sale_pointer->num, (sale_pointer->price2)*(sale_pointer->num)); money = money + (sale_pointer->price2)*(sale_pointer->num); } printf("nnt***********************************************************"); printf("nt总共消费金额为:%.2f", money); printf("nnnt确定完成交易?t按【y】确认支付 【任意键】取消支付nt"); fflush(stdin); char flag; flag = getch(); if (flag == 'y' || flag == 'y') { char name[20]; printf("nnt请输入您的免费精选名字大全:"); scanf("%s", name); printf("nn系统正在完成交易,请稍等..."); 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("nnnt交易完成,谢谢惠顾! o(∩_∩)o~nn"); printf("nnt交易系统正在关闭,请稍后..."); _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("nnnt即将进入用户界面,请稍等..."); _sleep(2000); break; //进入用户界面 } case '0': { saveandexit(); //保存并退出 exit(0); } } fflush(stdin); } } //管理员主菜单 void administratormenu() { system("cls"); printf("nt|-----------------------------------------------------------------|"); printf("nt| |"); printf("nt| 【小型超市管理系统】 |"); printf("nt| |"); printf("nt|-----------------------------------------------------------------|"); printf("nt| |"); printf("nt| 【1】∽∽∽∽∽∽∽【 新购商品入库 】 |"); printf("nt| |"); printf("nt| 【2】∽∽∽∽∽∽∽【 商品信息详情 】 |"); printf("nt| |"); printf("nt| 【3】∽∽∽∽∽∽∽【 商品信息更改 】 |"); printf("nt| |"); printf("nt| 【4】∽∽∽∽∽∽∽【 商品信息删除 】 |"); printf("nt| |"); printf("nt| 【5】∽∽∽∽∽∽∽【 商品信息浏览 】 |"); printf("nt| |"); printf("nt| 【6】∽∽∽∽∽∽∽【 商品销售管理 】 |"); printf("nt| |"); printf("nt| 【7】∽∽∽∽∽∽∽【 进入用户界面 】 |"); printf("nt| |"); printf("nt| 【0】∽∽∽∽∽∽∽【 保存退出程序 】 |"); printf("nt| |"); printf("nt|-----------------------------------------------------------------|"); printf("nntttt请输入您要选择的菜单...."); } //新购商品入库 void goodsadd() { while (1) { system("cls"); printf("nntt r:日用品 w:文化用品 s:食品 d:电子产品"); printf("nn请输入商品信息:"); struct good *goods,*exist; //指针 *exist 判断是否已存在该商品 goods = (struct good *)malloc(sizeof(struct good)); printf("nn商品编号:"); scanf("%d", &goods->id); exist = goodsidfind(goods->id); //检查编号是否重复 if (exist != null) { printf("nnt该商品信息已存在!请选择操作:nt【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("nnt该商品信息已存在!请选择操作:nt【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("nnt************************************************************************"); printf("nnt 编号t 名称t 类型t 数量t 进价t 售价t 生产日期t 保质期 nn"); printf("t %dt %st %ct %dt %.2ft %.2ft %.2ftt %d n", goods->id, goods->name, goods->kind, goods->num, goods->price1, goods->price2, goods->date, goods->baozhiqi); printf("nnt************************************************************************"); printf("nnt确定录入此商品信息?t(按【y】确认)nn"); 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("nnt是否继续录入商品信息?t(按【y】确认)"); char check2; check2 = getch(); if (check2 != 'y' || check2 != 'y') { printf("nnnnt【已结束商品信息录入,即将返回主程序...】"); _sleep(2000); break; } } } //商品信息详情 void goodsdetail() { struct good *goods; goods = goodsfind(); //查找是存在该商品 if (goods == null) { printf("nnt【抱歉,不存在该商品!】"); _sleep(2000); } else if (goods == good) { printf("nnntt正在返回程序,请稍等..."); _sleep(2000); } else { system("cls"); printf("nnt***********************************************************************"); printf("nnt 编号t 名称t 类型t 数量t 进价t 售价t 生产日期t 保质期 nn"); printf("t %dt %st %ct %dt %.2ft %.2ft %.2ftt %d n", goods->id, goods->name, goods->kind, goods->num, goods->price1, goods->price2, goods->date, goods->baozhiqi); printf("nnt***********************************************************************"); printf("nnt按任意键继续..."); getch(); } } //商品信息查找选择 struct good *goodsfind() { if (good->next == null) { printf("nntt抱歉,目前商店没有商品信息!"); _sleep(2000); return good; } while (1) { system("cls"); printf("nnnn"); 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("nt请输入商品编号:"); int goods_number; scanf("%d", &goods_number); goods = goodsidfind(goods_number); return goods; } else if (choose == 'b' || choose == 'b') { printf("nt请输入商品名称:"); 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("nnt【不存在该商品!】"); _sleep(2000); } else if (goods == good) { printf("nnntt正在返回程序,请稍等..."); _sleep(2000); } else { changeoperation(goods); //更改操作 } } //更改操作 void changeoperation(struct good *goods) { int end = 0; char choose; while (1) { system("cls"); printf("nnt***************************************************************************"); printf("nnt编号t 名称 t 类型t 数量t 进价t 售价t 生产日期t保质期 nn"); printf("t %dt %st %ct %dt %.2ft %.2ft %.2ftt %d n", goods->id, goods->name, goods->kind, goods->num, goods->price1, goods->price2, goods->date, goods->baozhiqi); printf("nnt***************************************************************************"); printf("nntt【r】:日用品 【w】:文化用品 【s】:食品 【d】:电子产品n"); printf("nt【1】商品编号t【2】商品名称t【3】商品类型t【4】商品数量"); printf("nt【5】进货价格t【6】出售价格t【7】生产日期t【8】保质期nt【0】保存退出n"); printf("nt请选择序号进行操作:n"); choose = getch(); switch (choose) { case '1': { printf("nt请输入商品编号:"); scanf("%d", &goods->id); break; } case '2': { printf("nt请输入商品名称:"); scanf("%s", &goods->name); break; } case '3': { printf("nt请输入商品类型:"); fflush(stdin); scanf("%c", &goods->kind); break; } case '4': { printf("nt请输入商品数量:"); scanf("%d", &goods->num); break; } case '5': { printf("nt请输入进货价格:"); scanf("%f", &goods->price1); break; } case '6': { printf("nt请输入出售价格:"); scanf("%f", &goods->price2); break; } case '7': { printf("nt请输入生产日期:"); scanf("%f", &goods->date); break; } case '8': { printf("nt请输入保质期:"); scanf("%d", &goods->baozhiqi); break; } case '0': { printf("nnt正在保存,请稍等..."); _sleep(2000); end = 1; break; } } if (end == 1) break; } } //商品信息删除 void goodsdelete() { struct good *goods; goods = goodsfind(); //查找是存在该商品 if (goods == null) { printf("nnt【抱歉,不存在该商品!】"); _sleep(2000); } else if (goods == good) { printf("nnntt正在返回程序,请稍等..."); _sleep(2000); } else { printf("nnt************************************************************************"); printf("nnt 编号t 名称t 类型t 数量t 进价t 售价t 生产日期t 保质期 nn"); printf("t %dt %st %ct %dt %.2ft %.2ft %.2ftt %d n", goods->id, goods->name, goods->kind, goods->num, goods->price1, goods->price2, goods->date, goods->baozhiqi); printf("nnt************************************************************************"); 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("nnt操作成功!"); _sleep(300); } //商品信息浏览 void goodsbrowse() { system("cls"); if (good->next == null) { printf("nntt抱歉,目前商店没有商品信息!"); _sleep(2000); } else { printf("tt请选择浏览方式:nnt【1】全部浏览t【2】按类型浏览"); char choose; while (1) { choose = getch(); if (choose == '1') { allbrowse(); break; } else if (choose=='2') { browsebytype(); break; } } printf("nnnt按任意键继续..."); getch(); } } //浏览所有商品 void allbrowse() { system("cls"); struct good *goods_pointer = good; printf("nnt*********************************************************************************"); printf("nnt 编号t 名称tt 类型t 数量t 进价t 售价t 生产日期t保质期 nn"); while (goods_pointer->next != null) { goods_pointer = goods_pointer->next; printf("t %dt %stt %ct %dt %.2ft %.2ft %.2ftt%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("nnt*********************************************************************************"); } //按类型浏览商品 void browsebytype() { system("cls"); printf("nntt 【r】:日用品 【w】:文化用品 【s】:食品 【d】:电子产品"); printf("nnn请选择要浏览的商品类型:"); char type; type = getch(); system("cls"); struct good *goods_pointer = good; printf("nnt*********************************************************************"); printf("nnt 编号t 名称t 类型t 数量t 进价t 售价t 生产日期t 保质期 n"); while (goods_pointer->next != null) { goods_pointer = goods_pointer->next; if (goods_pointer->kind == type) printf("nt %dt %st %ct %dt %.2ft %.2ft %.2ftt %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("nnt*********************************************************************"); } //商品销售管理 void salemanage() { system("cls"); printf("nntt销售情况查看及利润统计"); printf("nnt【r】:日用品 【w】:文化用品 【s】:食品 【d】:电子产品"); printf("nnt***********************************************************"); if (sale->next == null) { printf("nn目前暂无销售记录!"); _sleep(2000); } else { printf("nnt编号t名称t进价t售价t数量t免费精选名字大全t时间nn"); int number = 0;//商品数量 float profit = 0;//商品利润 struct sale *sale_pointer = sale; while (sale_pointer->next != null) { sale_pointer = sale_pointer->next; printf("t %dt %st %.2ft %.2ft %dt %st %sn",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("nt***********************************************************"); printf("nnt共销售商品%d件,纯利润为%.2f", number, profit); printf("nnnt按任意键继续..."); getch(); } } //保存并退出 void saveandexit() { salesavetofile();//保存销售信息 goodssavetofile();//保存商品信息 system("cls"); printf("nnnnnn"); printf("ntt #〓§〓〓〓〓〓§〓〓〓〓〓〓§〓〓〓〓〓§〓# "); printf("ntt ↓ ↓ ↓ ↓ "); printf("ntt ☆★☆ ☆★☆ ☆★☆ ☆★☆ "); printf("ntt ☆ 谢 ☆ ☆ 谢 ☆ ☆ 使 ☆ ☆ 用 ☆ "); printf("ntt ☆★☆ ☆★☆ ☆★☆ ☆★☆ "); printf("ntt ↓ ↓ ↓ ↓ "); printf("ntt ※ ※ ※ ※ "); _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; }
本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。
ctvol管理联系方式QQ:251552304
本文章地址:https://www.ctvol.com/c-cdevelopment/605692.html