C++ Stack Push 方法不将结构插入堆栈,只返回 0

标签 c++ class struct stack typedef

这是我的双栈程序。当我尝试调用 push 方法时,它不会将结构压入堆栈,它会压入 1,即使我传入参数的结构并且函数参数正在接收结构。任何帮助是极大的赞赏。我只想将我创建的结构推送到堆栈,然后当我弹出它时,我想弹出该结构并将其打印到控制台。我为此苦苦挣扎,因为如果我更改 vmr,它就会提示。我的代码如下:

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
using namespace std;
const int MAX = 20;
int vmr = 1;
int seed;
int upper;
int lower;
float randNum;
float randNumArr;
int dagabacount = 0;
int numberArrivals;
clock_t start;
double duration;
int fixedvehicles = 0;

//struct for vmr
struct vmr{
    char type;
    char name;
    int repairTime;
    int startTime;
    int finishTime;
};

//declaring dualstack
class repairStack
{
    private:
        int topT;
        int topS;
        int ele[MAX];

    public:
        repairStack();
        void pushTie   (struct vmr);
        void pushStar  (struct  vmr);
        int  popTie    (int *vmr); 
        int  popStar   (int *vmr);
}; 

//initializing dualstack
repairStack::repairStack()
{
    topT = -1;
    topS = MAX;
}

//pushing to tiestack
void repairStack::pushTie( struct vmr )
{
    if( topS == topT + 1 )
    {
        cout<<"\nStack Overflow tStack";
        dagabacount++;
        return;
    }

    topT++;
    ele[topT] = vmr;

    //cout<<"\nInserted item in tStack : "<< vmr;    
}

//pushing to starstack
void repairStack::pushStar( struct vmr )
{
    if( topS == topT + 1 )
    {
        cout<<"\nStack Overflow sStack";
        dagabacount++;
        return;
    }

    topS--;
    ele[topS] = vmr;

    //cout<<"\nInserted item in sStack : "<< vmr <<endl;    
}

//popping to tiestack
int repairStack::popTie( int *vmr )
{
    if( topT == -1 )
    {
        cout<<"\nStack Underflow tStack";
        return -1;
    }

    *vmr = ele[topT--];
    return 0;
}

//popping to starstack
int repairStack::popStar( int *vmr )
{
    if( topS == MAX )
    {
        cout<<"\nStack Underflow sStack";
        return -1;
    }

    *vmr = ele[topS++];
    return 0;
}

int getNumArrivals(){
        randNumArr = float(rand()) / (float(RAND_MAX) + 1.0);
          if(randNumArr < 0.25){
             return 1;
          }
          else if(.25 <= randNumArr < .50){
              return 2;
          }
          else if(.50 <= randNumArr < .75){
              return 3;
          }
          else{
             return 4;
          }
}

int main()
{ 
    //asking user for bounds
    cout << "Please give seed value: ";
    cin >> seed;
    cout << "Please give lower bound of stack: ";
    cin >> lower;
    cout << "Please give upper bound of stack: ";
    cin >> upper;
    srand(seed);
    //initalizing stack 's'
    repairStack s = repairStack();

    //initializing clock to 0 for first arrivals
    start = clock();
    duration = ( clock() - start ) / (double) CLOCKS_PER_SEC;
    cout<<"printf: "<< duration <<'\n';

    while(dagabacount < 5){
        //getting numberArrivals either 1,2,3,4
        numberArrivals = getNumArrivals();
        cout << "Number of Arrivals: " << numberArrivals;
        for(int i = 0; i < numberArrivals; i++){
        //getting random number between 0 and 1 to see if its T or S

          randNum = float(rand()) / (float(RAND_MAX) + 1.0);
          //if tie then push tie
          if(randNum < 0.75){
            struct vmr Tie_A;
            Tie_A.type='T';
            Tie_A.repairTime = 3;
            Tie_A.startTime = clock();
            Tie_A.finishTime = 0;
            Tie_A.name = 'A';
            s.pushTie(Tie_A);
          }
          //if star then push star
          else{
            struct vmr StarA;
            StarA.type='S';
            StarA.repairTime = 7;
            StarA.startTime = clock();
            StarA.finishTime = 0;
            StarA.name = 'A';
            s.pushStar(StarA);
          }
        }

        //fixing star if star stack is not empty, if so, fix a tiefighter if theres one in stack
        if(s.popStar(&vmr) == 0){
            cout << "\nFixed Star" << endl << endl;
            fixedvehicles++;
        }
        else{
            if(s.popTie(&vmr) == 0){
                cout << "\nFixed Tie" << endl << endl;
                fixedvehicles++;
            }
            else{
                cout << "Underflow" << endl;
            }
        }

    }
        cout << "\n5 Vehicles Rejected" << endl;
        cout << "# of Vehicles Serviced: " << fixedvehicles << endl;
        cout << "Average Time of Repair: " << endl;
    return 0;
}

最佳答案

尽量避免使用全局变量。或者至少给它特殊的名称,例如 glob_myVarName。在您的代码中,您有一个全局变量 vmrstruct vmr。您的 push 方法有一个没有名称的参数。您只指定了类型。而method是指全局的vmr变量。

你的方法应该有这样的签名:

void repairStack::pushTie(struct vmr v)

但是当您声明变量(而不是结构本身)时,struct 关键字在 C++ 中是多余的。所以你可以使用

void repairStack::pushTie(vmr v)

关于C++ Stack Push 方法不将结构插入堆栈,只返回 0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58112507/

相关文章:

c++ - 隐藏的 QMainWindow : application crashes after QMessageBox is displayed 问题

c++ STL map.find() 或 map.operator[] 不能在带有 const 限定符的类成员函数中使用

php - PHP 可以将类名中的对象实例化为字符串吗?

python - 为什么 PyCharm 在此处使用 @property 时会发出警告?

c++ - 在 using 语句中指定类名有什么作用?

python - 将底层结构公开为 Python 扩展中自定义类型的成员

c# - 为什么 C# 中的类没有循环布局问题?

c++ - 如何获取 Chrome 当前版本的当前 URL

c++ - 不为带有模板的用户定义运算符调用隐式构造函数

C - 如何将 realloc 与指针一起使用