33,311
社区成员
发帖
与我相关
我的任务
分享
#include"stdio.h"
#include"stdlib.h"
#include"string.h"
struct caf//定义结构体caf咖啡店
{
char caf_name[30];
long int caf_id;
char codename;
double size;
int good[20];
}cafe[10];
struct goods//定义结构体goods商品
{
char goods_name[30];
long int goods_id;
float cost;
float price;
int sale;
int amount[10];
struct goods *next;//定义指向goods结构体的指针next,由此构建链表
};
void imputcaf(caf cafe[10]);//各函数定义
struct goods *imputgoods(goods *G);
void readcaf(struct caf cafe[10]);
struct goods *readgoods(struct goods *G);
void save(struct caf cafe[10],struct goods *G);
int len(struct goods *G)
{
struct goods *z;
z=(struct goods *)malloc(sizeof(struct goods));
z=G->next;
int l=1;
while(z->next!=NULL)
{
l++;
z=z->next;
}
return l;
}
void main()//进入主函数
{
printf("***欢迎使用咖啡店管理系统1.0***\n");
struct caf cafe[10];
struct goods *myg=(goods *)malloc(sizeof(goods));;
if(fopen("D://cafe.txt","rb")==NULL)
{
printf("检测到您的计算机的D盘上无相关数据,正在为您进行数据自动输入\n");
imputcaf(cafe);
myg=imputgoods(myg);
save(cafe,myg);
}
else
{
printf("检测到您的计算机的D盘上有相关数据,已为您完成数据自动读入\n");
readcaf(cafe);
myg=readgoods(myg);
}
save(cafe,myg);
}
void imputcaf(struct caf cafe[10])//定义输入函数
{
for(int i=0;i<10;i++)//输入咖啡店数据
{
if(i==0){strcpy_s(cafe[i].caf_name,"Good Luck");}
if(i==1)
strcpy_s(cafe[i].caf_name,"Delicious");
if(i==2)
strcpy_s(cafe[i].caf_name,"Good Taste");
if(i==3)
strcpy_s(cafe[i].caf_name,"Happy Time");
if(i==4)
strcpy_s(cafe[i].caf_name,"Enjoy Life");
if(i==5)
strcpy_s(cafe[i].caf_name,"Romance");
if(i==6)
strcpy_s(cafe[i].caf_name,"Rose");
if(i==7)
strcpy_s(cafe[i].caf_name,"Dream House");
if(i==8)
strcpy_s(cafe[i].caf_name,"Tsinghua");
if(i==9)
strcpy_s(cafe[i].caf_name,"Peking");
cafe[i].caf_id=201600+i;
cafe[i].codename=65+i;
cafe[i].size=70+3*i;
}
printf("咖啡店数据自动输入成功\n");
}
struct goods *imputgoods(goods *G)
{
struct goods *input_temp,*input_cursor;
G=(struct goods *)malloc(sizeof(struct goods));
G->next=NULL;
input_cursor=G;
for(int i=0;i<6;i++)
{
input_temp=(struct goods *)malloc(sizeof(struct goods));
input_temp->next=NULL;
if(i==0){strcpy_s(input_temp->goods_name,"coffee");}
if(i==1){strcpy_s(input_temp->goods_name,"cake");}
if(i==2){strcpy_s(input_temp->goods_name,"cola");}
if(i==3){strcpy_s(input_temp->goods_name,"juice");}
if(i==4){strcpy_s(input_temp->goods_name,"chocolate");}
if(i==5){strcpy_s(input_temp->goods_name,"ice cream");}
input_temp->goods_id=100000+i;
input_temp->cost=10*(i+1)*1.0;
input_temp->price=11*(i+1)*1.0;
for(int j=0;j<10;j++)
{
input_temp->amount[j]=100+i+j;
}
input_temp->sale=10*(90-i);
input_cursor->next=input_temp;
input_cursor=input_cursor->next;
}
printf("商品数据自动输入成功\n");
return G;
}
void readcaf(struct caf cafe[10])
{
FILE *f1;
f1=fopen("D://cafe.txt","rb");
for(int i=0;i<10;i++)
{
fread(&cafe[i],sizeof(struct caf),1,f1);
}
fclose(f1);
}
struct goods *readgoods(struct goods *G){
FILE *f2;
struct goods *rg_cursor;
rg_cursor=G;
f2=fopen("D://goods.txt","rb");
while(true){
struct goods *g=(struct goods *)malloc(sizeof(struct goods));
if(fread(g,sizeof(struct goods),1,f2)!=1) break;
rg_cursor->next=g;
rg_cursor=rg_cursor->next;
}
rg_cursor->next=NULL;
return G;
fclose(f2);
}
void save(struct caf cafe[10],struct goods *G)//定义存储函数save,将各咖啡店的数据存储到磁盘上
{
FILE *save_f1,*save_f2;
struct goods *save_g=(struct goods *)malloc(sizeof(struct goods));
save_g=G->next;
fopen_s(&save_f1,"D://cafe.txt","wb");
fopen_s(&save_f2,"D://goods.txt","wb");
for(int i=0;i<len(G);i++)
{
fwrite(save_g,sizeof(struct goods),1,save_f2);
for(int j=0;j<10;j++)
{
cafe[j].good[i]=save_g->amount[j];
fwrite(&cafe[j],sizeof(struct caf),1,save_f1);
}
save_g=save_g->next;
}
printf("数据存储成功\n");
fclose(save_f1);
fclose(save_f2);
}
#include"stdio.h"
#include"stdlib.h"
#include"string.h"
#include"io.h"
struct caf//定义结构体caf咖啡店
{
char caf_name[30];
long int caf_id;
char codename;
double size;
int good[20];
}cafe[10];
struct goods//定义结构体goods商品
{
char goods_name[30];
long int goods_id;
float cost;
float price;
int sale;
int amount[10];
struct goods *next;//定义指向goods结构体的指针next,由此构建链表
};
void imputcaf(caf cafe[10]);//各函数定义
struct goods *imputgoods(goods *G);
void readcaf(struct caf cafe[10]);
struct goods *readgoods(struct goods *G);
void save(struct caf cafe[10],struct goods *G);
int len(struct goods *G)
{
struct goods *z;
z=(struct goods *)malloc(sizeof(struct goods));
z=G->next;
int l=1;
while(z->next!=NULL)
{
l++;
z=z->next;
}
return l;
}
void main()//进入主函数
{
printf("***欢迎使用咖啡店管理系统1.0***\n");
struct caf cafe[10];
struct goods *myg=(goods *)malloc(sizeof(goods));;
if(access("D://cafe.txt",0)==-1)
{
printf("检测到您的计算机的D盘上无相关数据,正在为您进行数据自动输入\n");
imputcaf(cafe);
myg=imputgoods(myg);
}
else
{
printf("检测到您的计算机的D盘上有相关数据,已为您完成数据自动读入\n");
readcaf(cafe);
myg=readgoods(myg);
}
save(cafe,myg);
}
void imputcaf(struct caf cafe[10])//定义输入函数
{
for(int i=0;i<10;i++)//输入咖啡店数据
{
if(i==0){strcpy_s(cafe[i].caf_name,"Good Luck");}
if(i==1)
strcpy_s(cafe[i].caf_name,"Delicious");
if(i==2)
strcpy_s(cafe[i].caf_name,"Good Taste");
if(i==3)
strcpy_s(cafe[i].caf_name,"Happy Time");
if(i==4)
strcpy_s(cafe[i].caf_name,"Enjoy Life");
if(i==5)
strcpy_s(cafe[i].caf_name,"Romance");
if(i==6)
strcpy_s(cafe[i].caf_name,"Rose");
if(i==7)
strcpy_s(cafe[i].caf_name,"Dream House");
if(i==8)
strcpy_s(cafe[i].caf_name,"Tsinghua");
if(i==9)
strcpy_s(cafe[i].caf_name,"Peking");
cafe[i].caf_id=201600+i;
cafe[i].codename=65+i;
cafe[i].size=70+3*i;
}
printf("咖啡店数据自动输入成功\n");
}
struct goods *imputgoods(goods *G)
{
struct goods *input_temp,*input_cursor;
G=(struct goods *)malloc(sizeof(struct goods));
G->next=NULL;
input_cursor=G;
for(int i=0;i<6;i++)
{
input_temp=(struct goods *)malloc(sizeof(struct goods));
input_temp->next=NULL;
if(i==0){strcpy_s(input_temp->goods_name,"coffee");}
if(i==1){strcpy_s(input_temp->goods_name,"cake");}
if(i==2){strcpy_s(input_temp->goods_name,"cola");}
if(i==3){strcpy_s(input_temp->goods_name,"juice");}
if(i==4){strcpy_s(input_temp->goods_name,"chocolate");}
if(i==5){strcpy_s(input_temp->goods_name,"ice cream");}
input_temp->goods_id=100000+i;
input_temp->cost=10*(i+1)*1.0;
input_temp->price=11*(i+1)*1.0;
for(int j=0;j<10;j++)
{
input_temp->amount[j]=100+i+j;
}
input_temp->sale=10*(90-i);
input_cursor->next=input_temp;
input_cursor=input_cursor->next;
}
printf("商品数据自动输入成功\n");
return G;
}
void readcaf(struct caf cafe[10])
{
FILE *f1;
f1=fopen("D://cafe.txt","rb");
for(int i=0;i<10;i++)
{
fread(&cafe[i],sizeof(struct caf),1,f1);
}
fclose(f1);
}
struct goods *readgoods(struct goods *G){
FILE *f2;
struct goods *rg_cursor;
rg_cursor=G;
f2=fopen("D://goods.txt","rb");
while(true){
struct goods *g=(struct goods *)malloc(sizeof(struct goods));
if(fread(g,sizeof(struct goods),1,f2)!=1) break;
rg_cursor->next=g;
rg_cursor=rg_cursor->next;
}
rg_cursor->next=NULL;
return G;
fclose(f2);
}
void save(struct caf cafe[10],struct goods *G)//定义存储函数save,将各咖啡店的数据存储到磁盘上
{
FILE *save_f1,*save_f2;
struct goods *save_g=(struct goods *)malloc(sizeof(struct goods));
save_g=G->next;
fopen_s(&save_f1,"D://cafe.txt","wb");
fopen_s(&save_f2,"D://goods.txt","wb");
for(int i=0;i<len(G);i++)
{
fwrite(save_g,sizeof(struct goods),1,save_f2);
for(int j=0;j<10;j++)
{
cafe[j].good[i]=save_g->amount[j];
fwrite(&cafe[j],sizeof(struct caf),1,save_f1);
}
save_g=save_g->next;
}
printf("数据存储成功\n");
fclose(save_f1);
fclose(save_f2);
}