C++ 在堆栈程序中实现计数器以计算对象移动的次数

C++ 在堆栈程序中实现计数器以计算对象移动的次数,c++,arrays,input,stack,counter,C++,Arrays,Input,Stack,Counter,全部。我已经了解了堆叠以及它们是如何工作的,我正在做一个练习,通过两个堆叠(停车场的两条车道)将汽车驶入停车场。我的代码需要一些工作,但我正在尝试实现某种计数器,它允许我查看一辆车在堆栈中移动了多少次 这是我现在的代码: #include <iostream> #include <fstream> #include <string> #include <cassert> using namespace std; struct car {

全部。我已经了解了堆叠以及它们是如何工作的,我正在做一个练习,通过两个堆叠(停车场的两条车道)将汽车驶入停车场。我的代码需要一些工作,但我正在尝试实现某种计数器,它允许我查看一辆车在堆栈中移动了多少次

这是我现在的代码:

#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);
     }
}
#包括
#包括
#包括
#包括
使用名称空间std;
结构车
{
字符码;
字符串许可证;
整小时;
int-min;
int num_移动;
};
类堆栈
{
公众:
静态常数int容量=10;
typedef car STACK_TYPE;///从int修改为car
stack();//构造函数
void push(堆栈类型);//将元素添加到堆栈
STACK_TYPE pop();//返回top元素
bool是空的();//堆栈是空的吗?
布尔已满;
私人:
int top;
堆叠式物品[容量];
};
stack::stack()
{
top=-1;//堆栈上最初没有任何内容
}
void stack::push(堆栈类型值)
{
断言(top!=容量-1);
top++;
项目[顶部]=价值;
}
stack::stack_类型stack::pop()
{
堆栈类型值;
断言(顶部>=0);
值=项目[顶部];
顶部--;
回报(价值);
}
bool stack::is_empty()
{
if(top==-1)返回true;
否则返回false;
}
bool stack::是否已满()
{
if(top==CAPACITY-1)返回true;
否则返回false;
}
无效到达(字符串&a、int&b、int&c)//打印到达车辆的许可证和时间
{

cout据我所知,您只需要在
stack::push
中增加车辆的
num\u moves
,尽管我建议在
car
中使用成员函数:

void car::parkIn(const stack &garage)
{
    ++num_moves;
    garage.push(*this);
}

好的,那么问题是什么?