超市商品管理系统中文提示By AzxXINER#include <stdlib.h>#include <stdio.h>#include <string.h>#include <Windows.h>//------------------------------------------------------------- //Max count of good,def 100 temporary//------------------------------------------------------------- #define MAX 100int current_cnt = 0;//------------------------------------------------------------- //Good Information Definition//------------------------------------------------------------- typedef struct GoodInfo{char good_id[30];char good_name[30];char good_price[10];char good_discount[10];int good_amount;int good_remain;}GoodInfo;GoodInfo *Goods[MAX];//------------------------------------------------------------- //free goodinfo memory//------------------------------------------------------------- void freeGoodInfo(){int i = 0;for(i = 0;i < MAX;i++)free(Goods[i]);Goods[i] = NULL;}//-------------------------------------------------------------//read a goodinfo from file//------------------------------------------------------------- GoodInfo* readGoodInfo(FILE* fp){GoodInfo* pGoodInfo = (GoodInfo*)malloc(sizeof(GoodInfo));fscanf(fp,"%s",&pGoodInfo->good_id);fscanf(fp,"\t%s",&pGoodInfo->good_name);fscanf(fp,"\t%s",&pGoodInfo->good_price);fscanf(fp,"\t%s",&pGoodInfo->good_discount);fscanf(fp,"\t%d",&pGoodInfo->good_amount);fscanf(fp,"\t%d\n",&pGoodInfo->good_remain);return pGoodInfo;}//-------------------------------------------------------------//check whether the file exists or not//-------------------------------------------------------------int check_nullfile(){FILE *fp = fopen("F:\\课程\\C语言程序设计\\综合实验2超市商品管理系统\\goodinfo.txt","r");//file not existif(!fp){printf("商品信息初始化文件不存在!\n请您放到E盘根目录!\n");fp = fopen("F:\\课程\\C语言程序设计\\综合实验2超市商品管理系统\\goodinfo.txt","w");fclose(fp);}//file already existelse{int temp;//res for try to read file if file null feof() can't jarge if file is nullint res = fscanf(fp,"%d",&temp);fclose(fp);if(res<=0)return -1;elsereturn 1;}}//-------------------------------------------------------------//initialize//-------------------------------------------------------------void info_init(){int i = 0,j = 0;int res = check_nullfile();FILE *fp = fopen("F:\\课程\\C语言程序设计\\综合实验2超市商品管理系统\\goodinfo.txt","r");for(i=0; i<MAX; ++i){Goods[i] = NULL;}while( res == 1 && !feof(fp) ){Goods[j] = readGoodInfo(fp);j++;current_cnt++;}fclose(fp);}//-------------------------------------------------------------//write one goodinfo into file//-------------------------------------------------------------void writeGoodInfo(FILE* fp,GoodInfo* pGoodInfo){fprintf(fp,"%s\t",pGoodInfo->good_id);fprintf(fp,"%s\t",pGoodInfo->good_name);fprintf(fp,"%s\t",pGoodInfo->good_price);fprintf(fp,"%s\t",pGoodInfo->good_discount);fprintf(fp,"%d\t",pGoodInfo->good_amount);fprintf(fp,"%d\n",pGoodInfo->good_remain);}//-------------------------------------------------------------//write all goodinfos into file//-------------------------------------------------------------void info_flush(){int i = 0;FILE *fp = fopen("F:\\课程\\C语言程序设计\\综合实验2超市商品管理系统\\goodinfo.txt","w");for(i=0; i<MAX; ++i){if(Goods[i])writeGoodInfo(fp,Goods[i]);}freeGoodInfo();fclose(fp);}//-------------------------------------------------------------//output//-------------------------------------------------------------void info_output(int i){printf("商品序号:%s\t商品名称:%s\n", Goods[i]->good_id,Goods[i]->good_name);printf("商品价格:%s\t商品折扣:%s\t",Goods[i]->good_price,Goods[i]->good_discount);printf("商品数量:%d\t商品剩余:%d\t\n\n" ,Goods[i]->good_amount, Goods[i]->good_remain);}//-------------------------------------------------------------//modify a goodinfo//-------------------------------------------------------------int info_change(){int i;if ( -1 == (i = info_search()) )return 0;else{printf("输入新的商品信息(以换行符区分信息条目):\n");scanf("%s\n",Goods[i] -> good_id);scanf("%s\n",Goods[i] -> good_name);scanf("%s\n",Goods[i] -> good_price);scanf("%s\n",Goods[i] -> good_discount);scanf("%d\n",&Goods[i] -> good_amount);scanf("%d",&Goods[i] -> good_remain);printf("商品信息修改成功!\n\n");return 0;}}//------------------------------------------------------------- //delete a goodinfo//------------------------------------------------------------- int info_dele(){int i;if (current_cnt > 0){if ( -1 == (i = info_search()) )return 0;else{free(Goods[i]);//Goods[i] = NULL;printf("删除商品信息成功!\n\n");current_cnt--;return 0;}}else{printf("没有任何商品信息!");return 0;}}//------------------------------------------------------------- // search a goodinfo//------------------------------------------------------------- int info_search(){int i;char name[20];scanf("%s",name);for ( i = 0; i < current_cnt; i++)if (strcmp(name,Goods[i]->good_name) == 0){printf("查询到以下信息:\n");info_output(i);return i;}if (i >= current_cnt){printf("该商品不存在!\n\n");return -1;}}//------------------------------------------------------------- //insert one goodinfo//------------------------------------------------------------- void info_insert(){int i = current_cnt;if (i<MAX){Goods[i] = (GoodInfo*)malloc(sizeof(GoodInfo));scanf("%s\n",Goods[i] -> good_id);scanf("%s\n",Goods[i] -> good_name);scanf("%s\n",Goods[i] -> good_price);scanf("%s\n",Goods[i] -> good_discount);scanf("%d\n",&Goods[i] -> good_amount);scanf("%d",&Goods[i] -> good_remain);current_cnt++;printf("插入商品信息成功!\n\n");info_output(i);}elseprintf("商品信息过多,无法再插入新的信息!");}//------------------------------------------------------------- //start UI//------------------------------------------------------------- void start(){printf("超市商品管理系统\n""************************************\n""** 1.商品信息的修改: **\n""** 2.删除某个商品信息: **\n""** 3.查找某个商品信息: **\n""** 4.插入某个商品信息: **\n""** 其他.退出系统. **\n""************************************\n""** 请输入你的选择:");}//------------------------------------------------------------- //main entry//------------------------------------------------------------- int main(void){int choose = 0;info_init();while(1){start();scanf("%d",&choose);switch (choose){case 1:printf("请输入你要修改的商品的名称: ");info_change();break;case 2:printf("请输入你要删除的商品的名称: ");info_dele();break;case 3:printf("请输入你要查询的商品的名称: ");info_search();break;case 4:printf("请输入你要插入的商品的信息(以换行符区分信息条目):\n");info_insert();break;default:printf("保存数据中,请稍后");Sleep(1000);printf(".");Sleep(1000);printf(".");Sleep(1000);printf(".");Sleep(1000);info_flush();printf("数据保存成功!已退出系统。