📄 main.cpp
字号:
#include <iostream>
#include <fstream>
#include <string>
#include <stack>
#include "car.h"
using namespace std;
int main(int argc, char *argv[])
{
if (argc != 2) {
cerr << "Usage: " << argv[0] << " data-file\n";
cerr << " you shoud use this program in the cmd line"<<endl;
return EXIT_FAILURE;
}
string in_file_name=argv[1];
string out_file_name="result.txt";
ifstream is(in_file_name.c_str());
ofstream os(out_file_name.c_str());
if(!is){
cerr<<"Can't open the file "<<in_file_name<<endl;
return EXIT_FAILURE;
}
if(!os){
cerr<<"Can't open the file "<<out_file_name<<endl;
return EXIT_FAILURE;
}
stack<car*> sc,temp_sc;
car * pcar;
string carname,state;
while(!is.eof() )
{
is>>carname>>state;
if(state == "arrives")// when a car arrives
{
if( sc.size() < 5 )
{
pcar = new car(carname);
sc.push(pcar);
//os<<sc.top()->get_license_plate()<<" is arriving "<<endl;
}
else
os<<"Sorry "<<carname<<" , The lot is full ! "<<endl;
}
else if(state == "departs")// when a car departs
{
while(!sc.empty()&&sc.top()->getlicenseplate() != carname )//is a car in the front
{
temp_sc.push(sc.top());
sc.top()->move();
sc.pop();
}
if(sc.top()->getlicenseplate() == carname)
{
//os<<sc.top()->get_license_plate()<<" is leaving "<<endl;
os<<sc.top()->getlicenseplate()<<" moved "<<sc.top()->getmovetime()<<" times"<<endl;
delete sc.top();
sc.pop();
}
else
os<<"exception!"<<endl;
while(!temp_sc.empty())
{
sc.push(temp_sc.top());
temp_sc.pop();
}
}
}
os<<"\n\n These cars are still in the lot \n\n";
while(!sc.empty())
{
os<<sc.top()->getlicenseplate()<<" moved "<<sc.top()->getmovetime()<<" times"<<endl;
delete sc.top();
sc.pop();
}
os.close();
is.close();
cout<<"The result.txt is created ,you can check it now!"<<endl;
return EXIT_SUCCESS;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -