#include <iostream>
#include<string.h>
using namespace std;
static int n; //货架(栈)的最大容量
//信息结构体
typedef struct /*Inform*/ //可以去掉Inform,在需要在结构体中定义结构体对象(指针)时不能去掉
{
string name;
int a;
}Inform;
//栈的*顺序*结构体
typedef struct
{
Inform *base; //栈和队列存储的元素都为inform类,所以指针定义为inform类
Inform *top;
int stacksize;
}SqStack;
//队列*顺序*结构体
typedef struct
{
Inform *base;
int fro;
int rear;
}SqQueue;
void Found1(SqStack &s); //货物上架
void Found2(SqStack &s); //创建顺序空栈
void Foundd(SqQueue &d); //创建顺序队列
void TurnsZ1(SqStack &s1,SqQueue d); //对于前天未剩余(第一天)倒货
void TurnsZ2(SqStack &s1,SqStack &s1_,SqStack s2); //对于前天有剩余倒货
void Print(SqStack &s1); //取下货架上的货物
int main()
{
int day=1,k;
cout << "请输入货架的最大容量\t";
cin >> n;
SqStack s1,s1_,s2; //s1为货架货物; s1_为第二天上架货物暂存处 s2:临时栈
SqQueue d; //声明队列
Found2(s1);
Found2(s1_);
Found2(s2); //创建四个空栈
while(k!=3)
{
cout<<"第"<<day++<<"天货物上架"<<endl;
if(s1.base==s2.top)
{
Found1(s1);
TurnsZ1(s1,d);
cout<<"1:出售货物 2:继续上货 3:结束上货"<<endl;
if(k==1)
cin>>k;
switch(k)
{
case 1:
{
Print(s1);
cout<<"2:继续上货 3:结束上货"<<endl;
cin>>k;
}
case 2: break;
default:break;
}
}
else
{
Found1(s1_);
TurnsZ2(s1,s1_,s2);
cout<<"1:出售货物 2:继续上货 3:结束上货"<<endl;
k=1;
if(k==1)
cin>>k;
switch(k)
{
case 1:
{
Print(s1);
cout<<"2:继续上货 3:结束上货"<<endl;
cin>>k;
}
case 2: break;
default:break;
}
}
}
while(1)
{
int m;
cout<<"1:出售货物 2:货物暂存,退出程序"<<endl;
cin>>m;
if(m==1)
Print(s1);
else
break;
}
return 0;
}
void Found1(SqStack &s) //货物上架(栈)
{
Inform e;
cout << "输出break结束"<<endl;
while(1)
{
if(s.stacksize>n)
break;
cin >> e.name ;
if(e.name=="break")
break;
cin >> e.a;
*s.top++ = e;
s.stacksize++;
}
}
void Found2(SqStack &s) //创建空栈
{
s.base = new Inform [n];
s.top = s.base;
s.stacksize = 0;
}
void Foundd(SqQueue &d) //创建队列
{
d.base = new Inform [n];
d.fro=d.rear=0;
}
//倒货
void TurnsZ1(SqStack &s1,SqQueue d) //新品上架倒货 主要思想:1.货架始终为栈s1 2.第n天上货,货架无货,旧货入队列,新货入栈,之后队列旧货入栈
{
Foundd(d);
Inform e;
int t;
while(s1.base != s1.top)
{
e = *(s1.top-1); //*(s1.top--)指向的仍然是s1.top
s1.top--;
s1.stacksize--;
t=d.rear;
d.rear=(d.rear+1)%100; //循环顺序队列
d.base[t] = e;
}
while(d.fro != d.rear)
{
t=d.fro;
d.fro=(d.fro+1)%100;
e=d.base[t];
*s1.top++=e;
s1.stacksize++;
}
}
void TurnsZ2(SqStack &s1,SqStack &s1_,SqStack s2) //前天货架有余货,倒货。 主要思想:1.s1为货架栈 2.新货存入栈s1_, 货架(s1)货物导入s2,然后s1_入货架(s1),s2入货架(s1)
{
Inform e;
while(s1.top!=s1.base)
{
e=*(s1.top-1);
s1.top--;
s1.stacksize--;
*s2.top++=e;
s2.stacksize++;
}
while(s1_.top!=s1_.base)
{
e=*(s1_.top-1);
s1_.top--;
s1_.stacksize++;
*s1.top++=e;
s1.stacksize++;
}
while(s2.top!=s2.base)
{
e=*(s2.top-1);
s2.top--;
s2.stacksize--;
*s1.top++=e;
s1.stacksize++;
}
}
void Print(SqStack &s1)
{
cout<<"请输入从货架出售货物件数\t"<<endl;
int s;
cin>>s;
while(1)
{
if(s<=s1.stacksize)
{
for(int i=0;i<s;i++)
{
Inform e;
s1.top--;
e=*s1.top;
cout<<e.name<<" "<<e.a<<endl;
}
break;
}
else
cout<<"货架货物不足"<<endl;
}
}
原文地址:https://www.cnblogs.com/XingPengJu/p/9929107.html