C ++ Stack Push方法未将struct推入堆栈,只是返回0

问题描述 投票:0回答:1

这是我的双栈程序。当我尝试调用push方法时,它不会将结构推送到堆栈上,即使im传入参数的结构并且函数参数正在接收结构,它也会推送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;
}
c++ class struct stack typedef
1个回答
2
投票

尝试避免使用全局变量。或至少给它一个特殊的名称,例如glob_myVarName。在您的代码中,您具有全局变量vmrstruct vmr。您的push方法具有一个没有名称的参数。您已经指定了唯一的类型。方法引用全局vmr变量

© www.soinside.com 2019 - 2024. All rights reserved.