这几天想根据17.5的队列ADT-FIFO模拟实际情形:
题目:
一家早餐店,拥有以下商品(生产时间*价格):“肉包(3mins*2¥)、油条(5mins*5¥)、牛奶(1mins*8¥)、豆腐脑(2mins*12¥)”,随机点单。(生产时间和价格与现实不符)
早餐店所处位置决定了客流量,队伍最长为10人,客流量代表着人进入队伍的概率。早晨客流量(繁华街为30%,偏僻小巷为10%),中午客流量*0.5,晚上客流量*0.1,关店客流量*0。
目标是计算每小时商品数量、营业额、客流量、被拒人数。
<队伍后5人中,每分钟会有一定的概率(第5位5%,按照位次递增至10%)离开队伍。(这个目前还不会,以后再说)>
问题1(solved):物品数量统计总是为一个相同的负数
后来发现是统计数据时指向了数组的地址而不是具体项的地址。
问题2(solved):每小时物品数量统计时,每种物品数量相同。
后来发现是统计每种物品数量时,未把count[i]++代码放入if括号,造成每种物品都是同数量增加。
有些代码懒得用函数封包了。
#ifndef QUEUE_H #define QUEUE_H #define _CRT_SECURE_NO_WARNINGS #include#include #include #include #include #define MAX_QUEUE 10 #define BUN_NAME "肉包" #define BUN_TIME 3 #define BUN_PRICE 2 #define STICK_NAME "油条" #define STICK_TIME 5 #define STICK_PRICE 5 #define MILK_NAME "牛奶" #define MILK_TIME 1 #define MILK_PRICE 8 #define BEANCURD_NAME "豆腐脑" #define BEANCURD_TIME 2 #define BEANCURD_PRICE 12 typedef struct menu { char *name; short time; short price; }Menu; const Menu menus[4]={ {BUN_NAME,BUN_TIME,BUN_PRICE}, {STICK_NAME,STICK_TIME,STICK_PRICE}, {MILK_NAME,MILK_TIME,MILK_PRICE}, {BEANCURD_NAME,BEANCURD_TIME,BEANCURD_PRICE}, }; typedef struct item { short order[4]; short time; short price; }Item; typedef struct node { Item item; struct node *next; }Node; typedef struct queue { Node *front; Node *rear; short nodes; }Queue; typedef struct record { short count[4]; short turnover; short flows; short reject; }Record; void exit_normal(void); void exit_abnormal(void); char *s_gets(char *p,int n); void InitializeQueue(Queue *pqueue); short IsQueueEmpty(const Queue *pqueue); short IsQueueFull(const Queue *pqueue); short EnQueue(Item *item,Queue *pqueue); short DeQueue(Item *item,Queue *pqueue); void EmptyQueue(Item *item,Queue *pqueue); #endif
#include"queue.h"
void exit_normal(void)
{
fputs("Done!ProgramOver!n",stderr);
}
void exit_abnormal(void)
{
fputs("Error!ProgramOver!n",stderr);
}
char *s_gets(char *p,int n)
{
char *gets=NULL,*find=NULL;
if(*(gets=fgets(p,n,stdin))!=10&&gets)
{
if(find=strchr(gets,10))
*find=0;
else
while(getchar()!=10);
}
else
return 0;
return gets;}
void InitializeQueue(Queue *pqueue)
{
pqueue->front=pqueue->rear=NULL;
pqueue->nodes=0;
}
short IsQueueEmpty(const Queue *pqueue)
{
return (!pqueue->nodes?1:0);
}
short IsQueueFull(const Queue *pqueue)
{
return (pqueue->nodes==MAX_QUEUE?1:0);
}
short EnQueue(Item *item,Queue *pqueue)
{
if(IsQueueFull(pqueue))
return 0;
Node *pnew=NULL;
if(!(pnew=(Node *)calloc(1,sizeof(Node))))
return 0;
pnew->item=*item;
pnew->next=NULL;
if(IsQueueEmpty(pqueue))
pqueue->front=pnew;
else
pqueue->rear->next=pnew;
pqueue->rear=pnew;
pqueue->nodes++;
return 1;}
short DeQueue(Item *item,Queue *pqueue)
{
if(IsQueueEmpty(pqueue))
return 0;
Node *ptemp=pqueue->front;
*item=ptemp->item;
pqueue->front=pqueue->front->next;
free(ptemp);
pqueue->nodes--;
if(!pqueue->nodes)
pqueue->rear=NULL;
return 1;}
void EmptyQueue(Item *item,Queue *pqueue)
{
short temp=pqueue->nodes;
while(DeQueue(item,pqueue));
printf("%d名顾客全部被驱赶.n",temp);
}
#include"queue.h"
#define TIME_WHOLE 1440
#define TIME_N 480
#define TIME_E 960
#define TIME_C 1200
#define PCUS_H_M 0.3
#define PCUS_H_N (PCUS_H_M*0.5)
#define PCUS_H_E (PCUS_H_M*0.1)
#define PCUS_L_M 0.1
#define PCUS_L_N (PCUS_L_M*0.5)
#define PCUS_L_E (PCUS_L_M*0.1)
typedef struct flow
{
float pcus_m;
float pcus_n;
float pcus_e;
}Flow;
int main(int argc,char *argv[])
{
atexit(exit_normal);
srand((unsigned int)time(NULL)*100);
short time_now=0;//当前计数时间
short time_wait=0;//首名顾客等待时间,用于启动Dequeue()
float pcus=0;//当前客流量概率值
Item item={0};
Flow flow={0};
Queue queue={0};
//Queue *qscan;
Record records[25]={0};//统计数据结构体数组
short records_index=1;//统计数据结构体数组索引,0为全天数据总和
int ch,choice;
for(fputs("选择你想经营早餐店的地点:1.繁华街,2.偏僻小巷n你的选择是:",stdout),ch=scanf("%d",&choice);
ch!=1||choice!=1&&choice!=2;
fputs("选择你想经营早餐店的地点:1.繁华街,2.偏僻小巷n你的选择是:",stdout),ch=scanf("%d",&choice))
{
fputs("WrongAnswer!Again!n",stdout);
while(getchar()!=10);
}
while(getchar()!=10);
system("cls");
printf("你选择了%s经营早餐店。n",(choice==1?"繁华街":"偏僻小巷"));
if(choice==1)
{
flow.pcus_m=(float)PCUS_H_M;
flow.pcus_n=(float)PCUS_H_N;
flow.pcus_e=(float)PCUS_H_E;
}
else
{
flow.pcus_m=(float)PCUS_L_M;
flow.pcus_n=(float)PCUS_L_N;
flow.pcus_e=(float)PCUS_L_E;
}
while(time_now%hd:%hd.n",(time_now/60+6)<24?(time_now/60+6):(time_now/60-18),time_now%60);//起点时间定为6点
choice=rand()%100;//此分钟客流量概率
if(choice>pcus*100)//此分钟客流量比较
{
if(!pcus)//如果为零
printf("由于早餐店已关门,1名顾客离开了。n");
else
{
for(int i=0;i<4;
item.order[i]=(short)rand()%2,i++);//生成购买菜单
for(int i=0;i<4;
item.time+=(menus[i].time)*(item.order[i]),item.price+=(menus[i].price)*(item.order[i]),i++);//计算购买金额与等待时间
if(item.time)//如果购买为真
{
if(EnQueue(&item,&queue))//如果加入队列成功
{
fputs("1名顾客进入了队伍,想购买:",stdout);
for(int i=0;i<4;i++)
if(item.order[i])
printf("%s,",menus[i].name);
fputs("b.n",stdout);
if(queue.nodes==1)//如果为队列唯一
time_wait=item.time;
}
else//如果加入队列失败
{
records[records_index].reject++;
printf("由于队列已满,1名顾客被拒,本小时共有%hd名顾客被拒。n",records[records_index].reject);
}
}
else//如果购买为假
fputs("1名顾客未找到想买的东西,离开了。n",stdout);
}
}
if(!IsQueueEmpty(&queue))
{
time_wait--;
if(!time_wait)
{
if(DeQueue(&item,&queue))
{
fputs("1名顾客购买了:",stdout);
for(int i=0;i<4;i++)
{
if(item.order[i])
{
printf("%s,",menus[i].name);
(records[records_index].count[i])++;
//粗心错误:未把这行代码放入if括号,造成物品数量相同
}
}
fputs("b.",stdout);
printf("共支付%hd¥.离开了队伍.n",item.price);
records[records_index].turnover+=item.price;
records[records_index].flows++;
time_wait=queue.front->item.time;//更新等待时间
}
}
}
Sleep(500);
}
records_index++;
}
fputs("每小时数据统计如下:n",stdout);
for(int i=1;i<25;i++)
{
printf("第%d个小时:n售出商品:",i);
for(int j=0;j<4;j++)
{
printf("%s:%hd个,",menus[j].name,records[i].count[j]);
//粗心错误原为:printf("%s:%hd个,",menus[j].name,records[j].count);造成物品数量统计总是为一个相同的负数
records[0].count[j]+=records[i].count[j];
}
fputs("b.n",stdout);
printf("营业额:%hdn",records[i].turnover);
records[0].turnover+=records[i].turnover;
printf("客流量:%hdn",records[i].flows);
records[0].flows+=records[i].flows;
printf("拒客数:%hdn",records[i].reject);
records[0].reject+=records[i].reject;
}
fputs("全天数据:n",stdout);
fputs("全天营业额:",stdout);
for(int j=0;j<4;j++)
printf("%s:%hd个,",menus[j].name,records[0].count[j]);
printf("营业额:%hdn",records[0].turnover);
printf("客流量:%hdn",records[0].flows);
printf("拒客数:%hdn",records[0].reject);
return 0;} 


