有没有大佬知道为什么我的while循环没用

 

#include<iostream>
using namespace std;
template<class T>
struct Node
{
    T data;
    Node<T> *next;
};
template<class T>
class LinkStack
{
    Node<T> *top;
public:
    LinkStack();
    ~LinkStack();
    void Push(T x);
    T Pop(T a);
    T Top();
    bool Empty();
    void Traverse();
    int Length();
};
template <class T>
class LinkList {
        Node <T> *head;
    public:
        LinkList();
        LinkList(T a[],int n);
        ~LinkList();
        int ListLength();
        T Get(int pos);
        int Locate(T item);
        void PrintLinkList();
        void Insert(int i,T item);
        T Delete(int i,T a);
        void Traverse();
        bool Empty();
        
};
struct VehicleType
{ //车辆类型 
    unsigned int num;
    unsigned int time;
 };
class StoppingPlace
{
    private:
        //停车场类的数据成员
        LinkStack<VehicleType> * pStopPath;     //停车场的停车道 
        LinkList<VehicleType> * pShortcutPath;  //便道 
        int maxNumOfStopVehicle;      //停车场的停车道停放车辆的最大数 
        int rate;          //停车单位时间的收费值
    //辅助函数
    bool ExistVehicleInStopPath(const VehicleType &vehicle) const;
      //停车场的停车道中是否存在车辆 vehicle
    int LocateInpShortcutPath(const VehicleType &vehicle) const;
    public:
    //方法声明及重载函数默认方法声明:
    StoppingPlace(int n,int r)               //构造函数
{
    maxNumOfStopVehicle=n;
    rate=r;
}
    virtual~StoppingPlace()
    {
    }                           //析构函数 
    void Arrive(const VehicleType &vehicle); //处理车辆到达的情形  
    void DisplayStatus() const;        //显示停车道与便道中车辆状态
    void Leave(const VehicleType &vehicle); //处理车辆离开的情形 
};
template<class T>
LinkStack<T>::LinkStack()
{
    top=NULL;
}
template<class T>
void LinkStack<T>::Push(T x)
{
    Node<T> *s;
    s=new Node<T>;
    s->data=x;
    s->next=top;
    top=s;
}
template<class T>
T LinkStack<T>::Pop(T a)
{
    if(top==NULL)
    {
        cerr<<"下溢";
        exit(1);
    }
    T x; 
    x=top->data;
    a=x;
    Node<T> *p;
    p=top;
    top=top->next;
    delete p;
    return x;
}
template<class T>
T LinkStack<T>::Top()
{
    if(top==NULL)
    {
        cerr<<"下溢";
        exit(1);
    }
    return top->data;
}
template<class T>
bool LinkStack<T>::Empty()
{
    return top==NULL;
 } 
 template<class T>
 LinkStack<T>::~LinkStack()
 {
     Node<T> *p;
     p=top;
     while(p)
     {
         Node<T> *q;
         q=p;
         p=p->next;
         delete q;
     }
     top=NULL;
 }
 template<class T>
 int LinkStack<T>::Length()
 {
     T ve;
     LinkStack<T> tmps;
     int i;
     while(!this->Empty())
     {
         this->Pop(ve);
         tmps.Push(ve);
         i++;
     }
     while(!tmps.Empty())
     {
         tmps.Pop(ve);
         this->Push(ve);
     }
     return i;
 }
 template<class T>
 void LinkStack<T>::Traverse()
 {
     T ve;
     LinkStack<T> tmps;
     while(!this->Empty())
     {
         this->Pop(ve);
         tmps.Push(ve);
     }
     while(!tmps.Empty())
     {
         tmps.Pop(ve);
         cout<<"("<<ve.num<<","<<ve.time<<")"<<"  "<<endl;
         this->Push(ve);
     }
 }
 template<class T>
LinkList<T>::LinkList() {
    head=new Node<T>;
    head->next=NULL;
}
template<class T>
LinkList<T>::LinkList(T a[],int n) {
    head=new Node<T>;
    Node<T> *rear;
    Node<T> *s;
    rear=head;
    for(int i=0; i<n; i++) {
        s=new Node<T>;
        s->data=a[i];
        rear->next=s;
        rear=s;
    }
    rear->next=NULL;
}
template<class T>
int LinkList<T>::ListLength() {
    int num=0;
    Node<T> *p;
    p=head->next;
    while(p) {
        p=p->next;
        num++;
    }
    return num;
}
template<class T>
T LinkList<T>::Get(int pos) {
    Node<T> *p;
    p=head->next;
    int j=1;
    while(p&&j<pos) {
        p=p->next;
        j++;
    }
    if(!p||j>pos) {
        cerr<<"查找位置非法";
        exit(1);
    } else
        return p->data;
}
template<class T>
int LinkList<T>::Locate(T item) {
    Node<T> *p;
    p=head->next;
    int j=1;
    while(p&&p->data!=item) {
        p=p->next;
        j++;
    }
    if(p)
        return j;
    else
        return 0;
}
template<class T>
void LinkList<T>::PrintLinkList() {
    Node<T> *p;
    p=head->next;
    while(p) {
        cout<<p->data<<endl;
        p=p->next;
    }
}
template<class T>
void LinkList<T>::Insert(int i,T item) {
    Node<T> *p;
    p=head;
    int j=0;
    while(p&&j<i-1) {
        p=p->next;
        j++;

    }
    if(!p) {
        cerr<<"插入位置非法";
        exit(1);
    } else {
        Node<T> *s;
        s=new Node<T>;
        s->data=item;
        s->next=p->next;
        p->next=s;
    }
}
template<class T>
T LinkList<T>::Delete(int i,T a) {
    Node<T> *p;
    p=head;
    int j=0;
    while(p&&j<i-1) {
        p=p->next;
        j++;
    }
    if(!p||!p->next) {
        cerr<<"删除位置非法";
        exit(1);
    } else {
        Node<T> *q;
        q=p->next;
        T x;
        x=q->data;
        p->next=q->next;
        delete q;
        a=x;
        return x;
    }
}
template<class T>
LinkList<T>::~LinkList() {
    Node<T> *p;
    p=head;
    while(p) {
        Node<T> *q;
        q=p;
        p=p->next;
        delete q;
    }
    head=NULL;
}
template<class T>
bool LinkList<T>::Empty()
{
    if(this->head->next==NULL)
    return true;
    else
    return false;
}
template<class T>
 void LinkList<T>::Traverse()
 {
     VehicleType ve;
     Node<T> *p;
    p=head->next;
    while(p) {
        ve=p->data;
        cout<<"("<<ve.num<<","<<ve.time<<")"<<"  "<<endl;
        p=p->next;
    }
 }

bool StoppingPlace::ExistVehicleInStopPath(const VehicleType &Vehicle) const
{
    VehicleType ve;        //临时元素
    LinkStack<VehicleType> tmps;   //临时栈
    bool found=false;             //表示是否找到车辆
    while(!pStopPath->Empty()&&!found)
    {   //检查停车场的停车道的车辆
        pStopPath->Pop(ve);
        tmps.Push(ve);
        if(Vehicle.num==ve.num)
        {   //已找到车辆
            found=true;
            
        }
    }
    while(!tmps.Empty())
    {    //将临时栈中的车辆送回停车道pStopPath
         tmps.Pop(ve);
         pStopPath->Push(ve);
        
    }
    return found;
}
int StoppingPlace::LocateInpShortcutPath(const VehicleType &vehicle) const
{
    //操作结果:在便道中查找车辆vehicle的位置,查找成功,返回正数,否则返回0
    VehicleType ve;  //临时元素
    for(int pos=1;pos<=pShortcutPath->ListLength();pos++)
    {   //查找在便道中的车辆
        if(vehicle.num==ve.num)
        {     //已找到车辆
              return pos;    //返回车辆位置
            
        }
        
    }
    return 0;    //查找失败
}
void StoppingPlace::DisplayStatus() const
//操作结果:显示停车道与便道中车辆状态
{
    cout<<"停车道中的车辆:";
    pStopPath->Traverse(); 
    cout<<endl;
    cout<<"便道中的车辆:";
    pShortcutPath->Traverse(); 
    cout<<endl;
    
}
void StoppingPlace::Arrive(const VehicleType &vehicle)
{
    if(pStopPath->Length()<maxNumOfStopVehicle)
    {
        pStopPath->Push(vehicle);
    }
    else
    {
        pShortcutPath->Insert(pShortcutPath->ListLength()+1,vehicle);
    }
}
void StoppingPlace::Leave(const VehicleType &vehicle)
{
    LinkStack<VehicleType> tmps;
    VehicleType ve;
    if(ExistVehicleInStopPath(vehicle))
    {
        for(pStopPath->Pop(ve);vehicle.num!=ve.num;pStopPath->Pop(ve))
        {
            tmps.Push(ve);
        }
        cout<<"在停车道中存在编号为"<<vehicle.num<<"的车辆"<<endl;
        cout<<"此车将离开,应收停车费"<<(vehicle.time-ve.time)*rate<<"元."<<endl;
        while(!tmps.Empty())
        {
            tmps.Pop(ve);
            pStopPath->Push(ve);
         } 
         if(!pShortcutPath->Empty())
         {
             pShortcutPath->Delete(1,ve);
             pStopPath->Push(ve);
         }
    }
    else if(LocateInpShortcutPath(vehicle)!=0)
    {
        int pos=LocateInpShortcutPath(vehicle);
        cout<<"在便道中存在编号为"<<vehicle.num<<"的车辆"<<endl;
        cout<<"此车将离开,不收停车费."<<endl;
        pShortcutPath->Delete(pos,ve); 
    }
    else
    {
        cout<<"在停车道与便道中不存在编号为"<<vehicle.num<<"的车辆"<<endl; 
    }
}

int main()
{
    int max;
    int fei;
    int case1=0;
    int hao;
    int t;
    
    cout<<"输入停车道停车辆的最大数与停单位时间的收费值:";
    cin>>max>>fei;
    StoppingPlace a1(max,fei);
    while(case1==0)
    {
        cout<<"1.车辆到达"<<endl;
        cout<<"2.车辆离开"<<endl;
        cout<<"3.显示状态"<<endl;
        cout<<"4.结束"<<endl;
        cout<<"选择功能:"<<endl;
        int i;
        cin>>i;
        switch(i){
            case 1:
                cout<<"输入车辆编号与到达时间:";
                cin>>hao>>t;
                VehicleType ve;
                ve.num=hao;
                ve.time=t;
                a1.Arrive(ve);
                break;
            case 2:
                cout<<"输入车辆编号与离开时间:";
                cin>>hao>>t;
                VehicleType ve1;
                ve1.num=hao;
                ve1.time=t;
                a1.Leave(ve);
                break;
            case 3:
                a1.DisplayStatus();
                break;
            case 4:
                case1=1;
                break;
                
                
                
                
                
        }
    }
    return 0;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值