Note that there are some explanatory texts on larger screens.

plurals
  1. POC++ Deque implementation with character arrays
    primarykey
    data
    text
    <p>So I'm writing a program that takes in a string of characters in a command line statement, and breaks up the word into two or three pieces (2 for even, first half and second half, 3 for odd, first "half", middle letter, and second "half), and reverses the characters of the first and second halves and re concatenates the characters into a single string htat is outputted. It gets a little uglier than that, as I have to use a deque and use push and pop functions to move around the characters. So I have a few problems I don't really understand. First off, the ABottom integer for some reason is blowing up to outrageously large values, which makes no sense as it is supposed to stay fixed at 0. Secondly, when I pop from A, I get an empty string, and when I pop from B, it alternates every other character from the deque. But the loops in the .h file that put the characters in the deque seems to be working exactly as I anticipated. Any suggestions about the ABottom, or why the pops aren't working?</p> <p>File 1:</p> <pre><code>// Kevin Shaffer TwoStackKAS.h #include&lt;iostream&gt; #include&lt;string&gt; #include&lt;vector&gt; #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&lt;&lt;"Stack Size: "&lt;&lt;totalSize&lt;&lt;endl; } virtual bool empty() const { return Abottom == AtopSpace &amp;&amp; Bbottom==BtopSpace; } virtual bool full() const { return AtopSpace==BtopSpace;} virtual int stackSize() { cout&lt;&lt;Abottom&lt;&lt;" Abottom"&lt;&lt;endl; return (AtopSpace - Abottom +Bbottom -BtopSpace); } virtual char popA() { if (empty()){ cerr &lt;&lt; "Attempting to pop Empty stack!"&lt;&lt; endl; return ' '; //prepare EmptyQexceptiin } else { cout &lt;&lt; elements[--AtopSpace] &lt;&lt; " testpopA"&lt;&lt; endl; return elements[--AtopSpace]; } } virtual char popB() { if (empty()){ //later EmptyQException cerr &lt;&lt;"Attempting to pop an empty stack!" &lt;&lt; endl; return ' '; } else { //cout &lt;&lt;elements-&gt;at(++BtopSpace) &lt;&lt; endl; cout &lt;&lt; elements[++BtopSpace] &lt;&lt; " test"&lt;&lt; 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&lt;=Bbottom; i++) { out += elements[i];} return out; } }; #endif </code></pre> <p>And file 2: </p> <pre><code>/** 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&lt;string&gt; #include "TwoStackKAS.h" #include&lt;iostream&gt; #include&lt;string&gt; using namespace std; int main (int argc, char* argv[]){ if (argc&lt;=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&lt;&lt;sd-&gt;stackSize()&lt;&lt;endl; for(int i = 0; i &lt; length/2; i++){ sd-&gt;pushA(word[i]); cout &lt;&lt; word[i] &lt;&lt; endl; } for(int i = length; i &gt;= length/2; i--){ //Second half of word sd-&gt;pushB(word[i] ); //has been pushed cout &lt;&lt; word[i] &lt;&lt; endl; //in reverse order. } //cout &lt;&lt; word &lt;&lt; endl; //Recombine word if(length%2==1){ word = word[length/2];} else{ word = "";} cout&lt;&lt;sd-&gt;stackSize()&lt;&lt;endl; string leftHalf; string rightHalf; string myWord; //new word (shuffled) for(int i=0; i&lt; half; i++) { leftHalf += sd-&gt;popA(); rightHalf += sd-&gt;popB(); } //cout&lt;&lt;"Stack: "&lt;&lt;sd-&gt;toString()&lt;&lt;endl; cout &lt;&lt; rightHalf &lt;&lt; endl; cout &lt;&lt; leftHalf &lt;&lt; endl; myWord = leftHalf + word + rightHalf; cout&lt;&lt;myWord&lt;&lt;endl; return 0; } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload