求大神 输入in时无反应 Do_command()中有毒啊

发布于 2022-09-01 20:45:56 字数 9663 浏览 5 评论 0

#include <iostream>
#include <string>
//#include <stack>

using namespace std;

enum Error_code{success,overflow,underflow};

const int S_maxsize=2;

//节点中的entry
struct Car
{
    string in_out;
    string car_numbers;
    string start_time;    //24小时制
    string end_time;
    /*double stay_time;*/
    string location;
    int location_num;
    //Car *next;        //!!!!

    Car()
    {
        in_out="in";
        car_numbers="No number";
        start_time="0000";
        /*stay_time=0;*/
    }
    Car(string &user_in_out,string &user_car_numbers,string user_now_time)//Car *add_on=NULL!!!,数组不允许引用?忘了
    {
        in_out=user_in_out;
        car_numbers=user_car_numbers;
        if(user_in_out=="in")    
            start_time=user_now_time;
        /*else
        {
            for(int i=0;i<5;i++)    
                end_time[i]=user_now_time[i];
        }*/
    }
};

typedef Car Stack_entry;
typedef Car Queue_entry;
typedef Car Node_entry;

//链式结构中的节点
struct Node
{
    Node_entry entry;
    Node *next;

    Node()
    {
        next=NULL;
    }
    Node(const Node_entry item,Node *add_on=NULL)
    {
        entry=item;
        next=add_on;
    }
};

//模拟停车场
class Car_Stack    
{
public:
    Car_Stack();
    bool empty()const;
    Error_code push(const Stack_entry &item);
    Error_code top( Stack_entry &item)const;
    Error_code pop();
    int Get_count();
    bool Find_the_car(string car_numbers,Car &temp);    
    bool Out_Stack(Car &temp);
protected:
    int count;
    /*static*/ Stack_entry entry[S_maxsize];
};

//模拟停车场外车道,加拷贝构造函数和运算符重载
class Car_Queue        
{
public:
    Car_Queue();
    bool empty()const;    
    Error_code apppend(const Stack_entry &item);
    Error_code retrieve( Stack_entry &item)const;
    Error_code serve();
    int Get_count();
    ~Car_Queue();    //怎么写
    Car_Queue(const Car_Queue &original);
    void operator=(const Car_Queue &original);
protected:
    int count;
    Node *front,*rear;
};

Car_Stack::Car_Stack()
{
    count=0;
}
bool Car_Stack::empty()const
{
    return count==0;
}
Error_code Car_Stack::push(const Stack_entry &item)
{
    if(count==S_maxsize)
        return overflow;
    else
        entry[count++]=item;
    return success;
}
Error_code Car_Stack::top( Stack_entry &item)const
{
    if(empty())
        return underflow;
    else
        item=entry[count-1];
    return success;
}
Error_code Car_Stack::pop()
{
    if(empty())
        return underflow;
    else
        
    return success;
}
int Car_Stack::Get_count()
{
    return count;
}

bool Car_Stack::Find_the_car(string car_numbers,Car &temp)
{
    bool Is_existed=false;

    for(int i=0;i<count;i++)
    {
        if(car_numbers==entry[i].car_numbers)
        {
            temp=entry[i];
            return Is_existed=true; 
        }
    }
    return Is_existed;
}

bool Car_Stack::Out_Stack(Car &temp)
{
    int car_index;
    Car_Stack s;
    bool Is_left=false;

    for(int i=0;i<count;i++)
    {
        if(temp.car_numbers==entry[i].car_numbers)
        {
            car_index=i;
            Is_left=true;
            break;
        }
    }
    for(int j=count-1;j>car_index;j--)
    {
        s.push(entry[j]);
    }

    pop();

    for(int i=0;i<s.count;i++)
    {
        this->push(s.entry[i]);
    }
    return Is_left;
}

Car_Queue::Car_Queue()
{
    front=rear=NULL;
}
bool Car_Queue::empty()const
{
    return count==0;
}

Error_code Car_Queue::apppend(const Stack_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;        //!!!把struct Node的定义放与类定义之前
        rear=new_rear;
    }
    return success;
}

Error_code Car_Queue::retrieve( Stack_entry &item)const
{
    if(front==NULL)
        return underflow;
    else
        item=front->entry;
    return success;

}
Error_code Car_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;    
}
int Car_Queue::Get_count()
{
    return count;
}
Car_Queue::~Car_Queue()
{
    while(!empty())
        serve();
}
Car_Queue::Car_Queue(const Car_Queue &original)
{
    Node *new_copy;
    Node *original_node=original.front;
    if(original_node==NULL)
        front=rear=NULL;
    else
    {
        front=new_copy=new Node(original_node->entry);
        while(original_node->next!=NULL)
        {
            original_node=original_node->next;
            new_copy->next=new Node(original_node->entry);
            new_copy=new_copy->next;
        }
        original_node=original.rear;
        rear=new_copy=new Node(original_node->entry);
    }
}

void Car_Queue::operator=(const Car_Queue &original)
{
    Node *new_rear,*new_front,*new_copy,*original_node=original.front;
    if(original_node==NULL)
        new_rear=new_front=NULL;
    else
    {
        new_copy=new_front=new Node(original_node->entry);
        while(original_node->next!=NULL)
        {
            original_node=original_node->next;
            new_copy->next=new Node(original_node->entry);
            new_copy=new_copy->next;
        }
        original_node=original.rear;
        new_rear=new_copy=new Node(original_node->entry);
        new_rear->next=NULL;
    }
    while(!empty())
    {
        serve();
        front=new_front;
        rear=new_rear;
    }
}

double Cal_stay_time(Car temp)
{
    double start_h,start_m;
    double end_h,end_m;
    double stay_time;

    start_h=10*temp.start_time[0]+temp.start_time[1];
    start_m=10*temp.start_time[2]+temp.start_time[3];
    end_h=10*temp.end_time[0]+temp.end_time[1];
    end_m=10*temp.end_time[2]+temp.end_time[3];
    if(end_m>=start_m)
        stay_time=end_h-start_h+((end_m-start_m)/60);
    else
        stay_time=end_h-start_h+((end_m+60-start_m)/60)-1;
    return stay_time;
}

void Do_command(string in_out,string car_numbers,string now_time)
{

        if(in_out=="out")/*if(in_out=="out")*/        //记得pop掉out的车!
        {
            Car temp;
            bool Is_existed;
            bool Is_left=false;
            Car_Stack c1;

            Is_existed=c1.Find_the_car(car_numbers,temp);

            if(!Is_existed)
                cout<<"Please check your car numbers,it is not existed or it is wrong!"<<endl;
            else
                Is_left=c1.Out_Stack(temp);

            if(Is_left)        //初始化的时候给Is_left赋初值
            {
                cout<<"You car is leaving the parking lot!"<<endl;
                cout<<"And the car stayed "<<Cal_stay_time(temp)<<"hours in parkig lot"<<endl;
                cout<<"The parking money you should pay is: "<<5*Cal_stay_time(temp)<<endl;    //停车费$5.0/h.    
            }
        }

        if(in_out=="in")
        {
            Car temp(in_out,car_numbers,now_time);
            Car_Stack c1;
            Car_Queue c2;
            /*bool Is_in=false;*/

            if(c1.push(temp)==success)
            {
                temp.location="Parking lot";
                temp.location_num=c1.Get_count()-1;
            }
            else
            {
                /*if(c2.apppend(temp)==overflow)
                    is_park=false;*/
                /*else
                {*/
                c2.apppend(temp);
                temp.location="Road";
                temp.location_num=c2.Get_count()-1;
                /*}*/
            }
            cout<<"Your car is located in: "<<temp.location<<endl;
            cout<<"And the number is: "<<temp.location_num<<endl;
        }
}

void introduction()
{
    cout<<"Welcome to a virtual parking system!"<<endl;
    cout<<"Here are some rules you're better to know."<<endl;
    cout<<"You just need to input three datas :"<<endl;
    cout<<"[in]:you want to park;[out]:you want to leave."<<endl;
    cout<<"And enter your car number."<<endl;
    cout<<"Then enter the current time in 24 ways with only 4 numbers."<<endl;
    cout<<"For example,now is 20:03,you just enter 2003!"<<endl;
    cout<<"Beforing you end,[y]to qiut the parking system;[n] to continue."<<endl;
    cout<<"Now,try it!"<<endl;
}

bool Judge_time(string now_time)
{
    int hour,minute;
    bool Is_legal=true;

    hour=now_time[0]*10+now_time[1];
    minute=now_time[2]*10+now_time[3];

    if(hour>24||minute>60||now_time.size()>4)
        Is_legal=false;

    return Is_legal;
}

void main()
{
    string in_out;
    string car_numbers;
/*    int now_time[5];*/    //从string改为了用数组,string也可用下标形式
    string now_time;    //检查时间合法性
    char Is_quit='n';

    introduction();
    
    cout<<"Please enter the information of your car:"<<endl;
    cin>>in_out>>car_numbers>>now_time;
    if(Judge_time(now_time))
    {
        cout<<"Please check your time!"<<endl;
        cout<<"Then enter the current time in 24 ways with only 4 numbers."<<endl;
        cout<<"For example,now is 20:03,you just enter 2003!"<<endl;
    }

    while(Is_quit=='n')
    {
        Do_command(in_out,car_numbers,now_time);

        cout<<"Continue?[y]to qiut the parking system;[n] to continue."<<endl;
        cin>>Is_quit;
    }
    
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文