顺序结构栈与队列之货物货架管理
程序员文章站
2023-04-08 14:22:03
#include #include using namespace std; static int n; //货架(栈)的最大容量 //信息结构体typedef struct /*Inform*/ //可以去掉Inform,在需要在结构体中定义结构体对象(指针 ......
#include <iostream>
#include<string.h>
#include<string.h>
using namespace std;
static int n; //货架(栈)的最大容量
//信息结构体
typedef struct /*inform*/ //可以去掉inform,在需要在结构体中定义结构体对象(指针)时不能去掉
{
string name;
int a;
}inform;
typedef struct /*inform*/ //可以去掉inform,在需要在结构体中定义结构体对象(指针)时不能去掉
{
string name;
int a;
}inform;
//栈的*顺序*结构体
typedef struct
{
inform *base; //栈和队列存储的元素都为inform类,所以指针定义为inform类
inform *top;
int stacksize;
}sqstack;
typedef struct
{
inform *base; //栈和队列存储的元素都为inform类,所以指针定义为inform类
inform *top;
int stacksize;
}sqstack;
//队列*顺序*结构体
typedef struct
{
inform *base;
int fro;
int rear;
}sqqueue;
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); //取下货架上的货物
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;
}
{
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++;
}
}
{
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;
}
{
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++;
}
}
{
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;
}
}
{
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;
}
}
上一篇: Java中使用elasticsearch搜索引擎实现简单、修改等操作
下一篇: 就是你瞎了