停车管理程序
#include<iostream> #include<string>
#include<fstream>
#include<stack>
using namespace std;
class Car
{
private:
string licenceplate;
int times;
public:
Car(string x,int y);
string getlicence();
int gettimes();
void settimes();
};
string Car::getlicence()
{
return licenceplate;
}
int Car::gettimes()
{
return times;
}
Car::Car(string plate,int time)
{
this->licenceplate=plate;
this->times=time;
}
void Car::settimes()
{
this->times++;
}
void main()
{
//打开文件,并将数据存入数组中。
string datas[60];
ifstream read;
const int maxsize=5;
int i=0;
read.open("data.txt");
if(!read)
{
cout<<"file not open!"<<endl;
system("pause");
return;
}
for ( i=0; i<60; i++)
{
read>>datas[i];
}
read.close();
//声明栈对象
stack<Car*> cars;
//循环判断是否为arrive状态
for( i=0;i<58;i++)
{
if(datas[i+1]=="arrives")
{
//判断是否为满
if(cars.size()==maxsize)
{
cout<<"Sorry PORSCHE, the lot is full!"<<endl;
continue;
}
else
{
Car* a;
a=new Car(datas[i],0);
cars.push(a);
}
}
else if(datas[i+1]=="departs")
{
if(cars.empty()==true)
{
cout<<" the lot is empty!"<<endl;
}
else
{
while((cars.top())->getlicence()!=datas[i])
{
cars.top()->settimes();
cars.pop();
if(cars.empty())
break;
}
if(!cars.empty())
{
cars.top()->settimes();
cout<<datas[i]<<" was moved "<<cars.top()->gettimes()<<" times while it was here"<<endl;
cars.pop();
}
}
}
}
if(cars.empty())
{
cout<<"stack is empty!"<<endl;
system("pause");
return;
}
cout<<cars.top()->getlicence()<<endl;
system("pause");
}
我是编程菜鸟 ,希望高手帮我改一下这个程序,要求在挪出一辆车时,它前边的车移出后,再移入栈,。,,谢了