0

但是将字符放入双端队列的 .h 文件中的循环似乎完全按照我的预期工作。关于 ABottom 的任何建议,或者为什么流行音乐不起作用?

文件 1:

// Kevin Shaffer TwoStackKAS.h

#include<iostream> 
#include<string>
#include<vector>

#ifndef TWOSTACKKAS_H_
#define TWOSTACKKAS_H_

using namespace std;

class TwoStacks {
    char elements[];
    int Abottom, Bbottom;
    int AtopSpace, BtopSpace;
    int totalSize;

public:
    TwoStacks(int maxAdds) {
        totalSize = 2*maxAdds +1;
        char elements[totalSize]; 
        const int Bbottom = totalSize-1; //bottom for both stacks!
        const int Abottom = 0;
        AtopSpace= 0; 
        BtopSpace = totalSize-1; //top for both stacks! 
        cout<<"Stack Size: "<<totalSize<<endl;
    }


    virtual bool empty() const { 
        return Abottom == AtopSpace && Bbottom==BtopSpace;
    }

    virtual bool full() const { return AtopSpace==BtopSpace;}
    virtual int stackSize() {
        cout<<Abottom<<" Abottom"<<endl;
        return (AtopSpace - Abottom +Bbottom -BtopSpace);
    }

    virtual char popA() {
        if (empty()){    
            cerr << "Attempting to pop Empty stack!"<< endl;
            return ' ';    //prepare EmptyQexceptiin    
        } else {
            cout << elements[--AtopSpace] << " testpopA"<< endl;
            return elements[--AtopSpace];
        }
    }

    virtual char popB() {    
        if (empty()){    //later EmptyQException
            cerr <<"Attempting to pop an empty stack!" << endl;
            return ' ';        
        } else {
            //cout <<elements->at(++BtopSpace) << endl;
            cout << elements[++BtopSpace] << " test"<< endl;
            return elements[++BtopSpace];

        }
    }

     virtual void pushB(char newItem){    
        elements[BtopSpace--] = newItem;
    }

     virtual void pushA(char newItem){    
        elements[AtopSpace++] = newItem;
    }

    virtual string toString() const {
        string out = "";
        for (int i = 0 ; i<=Bbottom; i++) {
            out += elements[i];}
        return out;
    }
};  

#endif

和文件2:

/** Kevin Shaffer
 * Given an input string, reverse each half of the string; 
 * pivot on the middle element if it exists.
 * uses double ended stack in twoStackKAS.h*/


#include<string>
#include "TwoStackKAS.h"
#include<iostream>
#include<string>

using namespace std;
int main (int argc, char* argv[]){
    if (argc<=1){return 0;}
    string word = argv[1];
    int length = word.size();                   // gets length of word
    int half = length/2;
    TwoStacks* sd = new TwoStacks(length/2);
    //cout<<sd->stackSize()<<endl;
    for(int i = 0; i < length/2; i++){  
        sd->pushA(word[i]);
        cout << word[i] << endl;
    }

    for(int i = length; i >= length/2; i--){   //Second half of word
        sd->pushB(word[i] );     //has been pushed 
        cout << word[i] << endl; //in reverse order. 
    } 
    //cout << word << endl;

    //Recombine word 
    if(length%2==1){ word = word[length/2];}
    else{ word = "";}
    cout<<sd->stackSize()<<endl;
    string leftHalf; string rightHalf;
    string myWord; //new word (shuffled)
    for(int i=0; i< half; i++) {
        leftHalf +=  sd->popA();
        rightHalf += sd->popB();
    }
    //cout<<"Stack: "<<sd->toString()<<endl;
    cout << rightHalf << endl;
    cout << leftHalf << endl;
    myWord = leftHalf + word + rightHalf;
    cout<<myWord<<endl;
    return 0;
}   
4

1 回答 1

0

ABottom 没有增长......它从未初始化!看看你的构造函数。您没有分配 Abottom,而是定义了一个新变量来掩盖成员变量。你这样做多次。

而且由于 VS2015 实际上不接受char elements[];:“不允许不完整类型”,因此使用 std::string 而不是 char* 可能更好

一个更好的构造函数是这样的。

class TwoStacks
{
private:
    // totalSize has to be assigned before assigning dependent variables
    int totalSize;
    string elements;
    int Abottom, Bbottom;
    int AtopSpace, BtopSpace;

public:
    TwoStacks(int maxAdds)
        : totalSize(2 * maxAdds + 1)
        , elements(totalSize, ' ')
        , Abottom(0)
        , Bbottom(totalSize - 1)
        , AtopSpace(0)
        , BtopSpace(totalSize - 1)
    {
        // preferably don't cout in a constructor
        cout << "Stack Size: " << totalSize << endl;
    }
于 2017-02-07T09:48:37.520 回答