C++ C++;用字符数组实现Deque

C++ C++;用字符数组实现Deque,c++,arrays,char,deque,C++,Arrays,Char,Deque,因此,我正在编写一个程序,在命令行语句中输入一个字符串,并将单词分成两到三部分(2表示偶数,前半部分和后半部分,3表示奇数,前半部分,中间字母和后半部分),并反转上半部分和下半部分的字符,并将字符重新连接到一个字符串中。输出时,它会变得更难看,因为我必须使用deque并使用push和pop函数来移动字符。因此,我有一些问题我不太明白。首先,对于某些情况,ABOTOM整数原因是爆炸到了惊人的大值,这毫无意义,因为它应该保持固定在0。其次,当我从A弹出时,我得到一个空字符串,当我从B弹出时,它从de

因此,我正在编写一个程序,在命令行语句中输入一个字符串,并将单词分成两到三部分(2表示偶数,前半部分和后半部分,3表示奇数,前半部分,中间字母和后半部分),并反转上半部分和下半部分的字符,并将字符重新连接到一个字符串中。输出时,它会变得更难看,因为我必须使用deque并使用push和pop函数来移动字符。因此,我有一些问题我不太明白。首先,对于某些情况,ABOTOM整数原因是爆炸到了惊人的大值,这毫无意义,因为它应该保持固定在0。其次,当我从A弹出时,我得到一个空字符串,当我从B弹出时,它从deque中每隔一个字符交替出现。但是.h文件中将字符放入deque的循环似乎完全按照我的预期工作。有没有关于屠宰场的建议,或者为什么pops不起作用

文件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
//Kevin Shaffer TwoStackKAS.h
#包括
#包括
#包括
#伊夫德夫·托斯塔卡什_
#定义两个stackkas_H_
使用名称空间std;
二类堆栈{
字符元素[];
内特阿博托姆,巴博托姆;
int原子空间,BtopSpace;
整数总大小;
公众:
两个堆栈(int-maxAdds){
totalSize=2*maxAdds+1;
字符元素[总大小];
const int Bbottom=totalSize-1;//两个堆栈的底部!
常数int abotom=0;
原子空间=0;
BtopSpace=totalSize-1;//两个堆栈的顶部!

屠宰场不会成长…它永远不会初始化! 看看你的构造器。你不是在分配ABOTOM,而是在定义一个新的变量来屏蔽成员变量。你可以多次这样做

由于VS2015实际上不接受
char元素[];
:“不允许不完整的类型”,因此最好使用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;
    }
class二栈
{
私人:
//在分配因变量之前,必须先分配totalSize
整数总大小;
字符串元素;
内特阿博托姆,巴博托姆;
int原子空间,BtopSpace;
公众:
两个堆栈(int-maxAdds)
:totalSize(2*MaxAdd+1)
,元素(总大小“”)
,abotom(0)
,Bbottom(总尺寸-1)
,原子空间(0)
,BtopSpace(总大小-1)
{
//最好不要在构造函数中使用

这段代码可能真的有问题。在TwoStacks构造函数中,您在堆栈上隐藏了带有新变量的成员变量。我确定这不是您的意图?首先,您要执行int/int,因此得到的结果不正确,例如9/2=4 int length=word.size();int half=length/2;Chris,这对我所做的很好,如果我的长度是9,我想忽略中间的字符。kfunk,我不知道“隐藏成员变量”是什么意思。那么,你认为拥有一个成员变量
char元素[]类中的
和构造函数中的局部变量
char elements[totalSize];
(VLA,可变长度数组,该语言甚至不支持)将有助于您判断这件事是否有效?事实上,该构造函数根本无法正确初始化任何内容(见上文)。
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;
    }