順序結構棧與隊列之貨物貨架管理


#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;
    }
}

注意!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。



 
粤ICP备14056181号  © 2014-2020 ITdaan.com