全部。我已经了解了堆栈及其工作原理,并且正在开展一项练习,以通过 2 个堆栈(停车场的 2 条车道)将汽车移入停车场。我的代码需要一些工作,但我正在尝试实现某种计数器,让我可以查看汽车在堆栈中移动了多少次。
这是我现在的代码:
#include <iostream>
#include <fstream>
#include <string>
#include <cassert>
using namespace std;
struct car
{
char code;
string license;
int hour;
int min;
int num_moves;
};
class stack
{
public:
static const int CAPACITY = 10;
typedef car STACK_TYPE; ///MODIFIED from int to car
stack(); // constructor
void push(STACK_TYPE); // add element to the stack
STACK_TYPE pop(); // return top element
bool is_empty(); // is stack empty?
bool is_full();
private:
int top;
STACK_TYPE items[CAPACITY];
};
stack::stack()
{
top=-1;//nothing on stack initially
}
void stack::push(STACK_TYPE value)
{
assert(top !=CAPACITY-1);
top++;
items[top]=value;
}
stack::STACK_TYPE stack::pop()
{
STACK_TYPE value;
assert(top>=0);
value= items[top];
top--;
return(value);
}
bool stack::is_empty()
{
if (top== -1) return true;
else return false;
}
bool stack::is_full()
{
if (top==CAPACITY-1) return true;
else return false;
}
void arriving(string &a, int &b, int &c) //prints arriving car's license & time
{
cout<<"car has been parked in the garage."<<endl;
cout<<"license plate number: "<<a <<endl;
cout<<"current time:" << b<<":";
if (c>=10) cout<<c<<endl;
else cout<<"0"<<c<<endl;
cout <<"*********************"<<endl;
}
void departing(string &a, int&b, int &c)//prints departing car's license & time
{
//cout << "Another car must be moved out of the way!";
cout<<"License: "<<a<<endl;
cout<<"Time of departure: " <<b<<":";
if (c>=10) cout<<c<<endl;
else cout<<"0"<<c<<endl;
}
void time_parked(int &a, int &b, int &c, int &d)//prints time parked
{
int hour_diff;
int min_diff;
if (d<b)//if departing minutes is less than arriving minutes
{
c--;
}
hour_diff = c-a;
min_diff = d-b;
cout<<"Time Parked: "<<hour_diff<<":";
if (min_diff>=10) cout<<min_diff<<endl;
else cout<<"0"<<min_diff<<endl;
}
void print_garage();
stack garage;
stack garage2;
stack street; ///GLOBAL
int main ()
{
// stack garage;
// stack street;
car cars[100];
car temp;
int new_hour;
int new_min;
int x=0;
fstream textfile;
textfile.open("lab4.txt");
while(!textfile.eof())
{
textfile>>cars[x].code;
textfile>> cars[x].license;
if (cars[x].code =='A')
{
if (garage.is_full())
{
cout<<"Garage is full."<<endl;
cout<<"License: "<<cars[x].license<<endl;
cout<<"Moving future cars to garage2!";
if (garage2.is_full())
{
cout<<"Garage is full."<<endl;
cout<<"License: "<<cars[x].license<<endl;
}
else{
textfile>>cars[x].hour;
textfile>>cars[x].min;
arriving(cars[x].license, cars[x].hour, cars[x].min);
garage2.push(cars[x]);
}
}
else
{
textfile>>cars[x].hour;
textfile>>cars[x].min;
arriving(cars[x].license, cars[x].hour, cars[x].min);
garage.push(cars[x]);
}
}
if (cars[x].code=='D')
{
textfile>>new_hour;
textfile>>new_min;
while (!garage.is_empty())
{
temp = garage.pop();
street.push(temp);
while (temp.license!=cars[x].license)
{//backing cars from garage to street until finding the matching license car
if(!garage.is_empty())
{
temp = garage.pop();
street.push(temp);
}
}
if (temp.license!=cars[x].license)
{
cout<<"Car not found"<<endl;
cout<<"License: "<<cars[x].license;
}
else
{
departing(cars[x].license, new_hour, new_min);
time_parked(cars[x].hour, cars[x].min, new_hour, new_min);
street.pop();
}
}
while(!street.is_empty())
{
temp = street.pop();
garage.push(temp);
}
print_garage();
}
}
}
void print_garage()
{
car temp;
while (!garage.is_empty())
{
temp = garage.pop();
cout<<temp.license<<endl;
street.push(temp);
}
while(!street.is_empty())
{
temp = street.pop();
garage.push(temp);
}
}