回 帖 发 新 帖 刷新版面

主题:停车场问题

我们作业题,可能对有些同学会帮上一些忙由于时间有限没写注释,看不懂可以邮箱问我hahazhoul@yahoo.com.cn
#include<iostream.h>

enum error_code{success,underflow,overflow};
const int maxstack=2;

struct car{
    float intime,outtime;
    int num;
};
typedef car    stack_entry;
typedef car    queue_entry;
typedef car node_entry;
struct node{
    node_entry entry;
    node *next;
    node();
    node(node_entry item,node *add_on=NULL);
};

node::node()
{
    next=NULL;
}
node::node(node_entry item,node *add_on)
{
    entry=item;
    next=add_on;
}

class queue{
public:
    queue();
    bool empty() const ;
    error_code append(const queue_entry &item);
    error_code serve();
    error_code retrieve(queue_entry &item)const;
    error_code gett(int m,queue_entry &item);
//    ~queue();
//    queue(const queue& original);
//    void operator =(const queue &original);
protected:
    node *front,*rear;
};
queue::queue()
{
    front=rear=NULL;
}
error_code queue::append (const queue_entry &item)
{
    node *new_rear =new node(item);
    if(new_rear==NULL)return overflow;
    if(rear==NULL)front=rear=new_rear;
    else{
        rear->next=new_rear;
        rear=new_rear;
    }
    return success;
}
error_code queue::retrieve(queue_entry &item)const
{
    if(front==NULL) 
        return underflow;
    item=front->entry;
    return success;
}
error_code queue::serve ()
{
    if(front==NULL)return underflow;
    node *old_front=front;
    front=old_front->next;
    if(front==NULL)rear=NULL;
    delete old_front;
    return success;
}
error_code queue::gett(int m,queue_entry &item)
{
    node *old_front=front;
    for(int x=0;x<m;x++)
    {
        old_front=front;
        if(front->entry.num ==item.num){break;} 
        front=old_front->next;
    
    }
    if(x==m)front=old_front;
    if(m==0)
        return overflow;
    if(front->entry.num!=item.num)
        return overflow;
    else
        return success;
    
}

回复列表 (共3个回复)

沙发

class stack
{
public:
    stack();
//    bool empty()const;
    error_code pop();
    stack_entry  top();
    error_code push(const stack_entry car_temp);
    int get(stack_entry car_temp);

private:
    int count;
    stack_entry entry[maxstack];
};
error_code    stack::push(const stack_entry car_temp)
{
    error_code outcome=success;
    if(count>=maxstack)
        outcome=overflow;
    else entry[count++]=car_temp;
    return outcome;
}
error_code    stack::pop ()
{
    error_code outcome=success;
    if(count==0)
        outcome=underflow;
    else --count;
    return outcome;
}
stack_entry  stack::top ()
{
    stack_entry car_temp;
    //error_code outcome=success;
    //if(count==0)
    //    outcome=underflow;
    //else
        car_temp=entry[count-1];
    //return outcome;
        return car_temp;
}
stack::stack()
{
    count=0;
}
int stack::get(stack_entry car_temp)
{    int j;
    for(j=0;j<count;j++)
    {
        if(entry[count-j].num==car_temp.num)break;
    }
    if(entry[count-j].num!=car_temp.num||count==0)
         return -1;
    return j;
}

板凳

main()
{    
    char i;
    int j,m,n;
    n=m=0;
    stack_entry  car_temp0,car_temp1,car_temp2;
    //queue_entry car_temp3,car_temp4;
    stack temp1,temp2;
    queue temp3,temp4;
    while(1)    
    {
        while(1)
        {
            cout<<"汽车出站(A)或进站(I)或结束(E):"<<endl;
            cin>>i;
            if(i=='i'||i=='I')
            {
                m++;
                cout<<"请输入车牌号(数字):"<<endl;
                cin>>car_temp0.num ;
                cout<<"输入进站时间(数字):"<<endl;
                cin>>car_temp0.intime;
                if(temp1.push(car_temp0)==overflow)
                {
                    cout<<"车库已满已将车安放到外车道上"<<endl;
                    if(temp3.append(car_temp0)==overflow)
                    {
                        cout<<"对不起临时停车场已满不能停车!"<<endl;break;
                    }
                    n++;
                    cout<<"汽车在临时停车场的位置为:"<<n<<"号位"<<endl;
                }
                
                else
                    cout<<"汽车在车库的位置为:"<<m<<"号位"<<endl;
            }
            

3 楼

if(i=='a'||i=='A')
            {
                
                cout<<"请输入车牌号:"<<endl;
                cin>>car_temp0.num ;
                
                j=temp1.get(car_temp0);
                if(j==-1)
                {
                
                    if(temp3.gett(n,car_temp0)==overflow)
                    {
                        cout<<"车库和临时停车场都没有这辆车 请确定!"<<endl;break;
                    }
                    n--;
                    cout<<"汽车没有进车库,收费为0 :)"<<endl;    break;
                }
                m--;
                for(int k=1;k<j;k++)
                {
                    car_temp1=temp1.top();
                    temp1.pop ();
                    temp2.push (car_temp1);
                }
                car_temp0=temp1.top();
                cout<<"输入出站时间:"<<endl;
                cin>>car_temp0.outtime;
                temp1.pop();
                for(k=1;k<j;k++)
                {
                    car_temp1=temp2.top ();
                    temp2.pop();
                    temp1.push(car_temp1);
                }
                cout<<"汽车出站在车库的时间为:"<<(car_temp0.outtime-car_temp0.intime)<<"小时"<<endl;
                cout<<"汽车应交的费用为:"<<(car_temp0.outtime-car_temp0.intime)*2<<"元"<<endl;
                if(temp1.push(car_temp0)!=overflow&&temp3.retrieve(car_temp1)!=underflow)
                {
                        car_temp2=temp1.top();
                        temp1.pop ();
                        car_temp1.intime=car_temp2.outtime;
                        temp1.push(car_temp1);
                        temp3.serve();
                
                }
            }
            if(i=='e'||i=='E')return 0;
        }
    }
    return 0;
}

我来回复

您尚未登录,请登录后再回复。点此登录或注册