[英]C++ Stack Push method not pushing struct onto stack, just returning 0
這是我的雙棧程序。 當我嘗試調用 push 方法時,它不會將結構壓入堆棧,它會壓入 1,即使我傳入了參數的結構並且 function 參數正在接收結構。 任何幫助是極大的贊賞。 我只想將創建的結構推送到堆棧,然后當我彈出它時,我想彈出該結構並將其打印到控制台。 我正在為此苦苦掙扎,因為如果我更改 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
。 在您的代碼中,您有一個全局變量vmr
和struct vmr
。 您的 push 方法有一個沒有名稱的參數。 您只指定了類型。 而method指的是全局vmr
變量。
你的方法應該有這樣的簽名:
void repairStack::pushTie(struct vmr v)
但是當您聲明變量(而不是結構本身)時, struct
關鍵字在 C++ 中是多余的。 所以你可以使用
void repairStack::pushTie(vmr v)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.