所以我正在编写一个程序,它在命令行语句中接受一串字符,并将该单词分成两个或三个部分(2个表示偶数,上半部分和后半部分,3表示奇数,第一个“半个,“中间字母”和“第二个”半部分,然后将第一半和第二半的字符反转并重新连接成一个字符串htat输出。它有点丑陋,因为我必须使用deque并使用push和pop函数来移动字符。所以我有一些我不太了解的问题。首先,ABottom整数由于某种原因正在爆发出极大的值,这没有任何意义,因为它应该是其次,当我从A弹出时,我得到一个空字符串,当我从B弹出时,它会从双端队列中交替出现所有其他字符。但.h文件中的循环将字符放入deque似乎完全像我预期的那样工作。有关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;
}
答案 0 :(得分: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;
}