Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>So there are a number of problems with your code. I'll post what (should be) a corrected solution, which has copious comments to explain what's happening and where you've made mistakes. A few things up front:</p> <ol> <li><p>I'll use <code>std::string</code> instead of <code>char *</code> because it makes things much cleaner, and honestly, you should be using it in <code>C++</code> unless you have a very good reason not to (such as interoperability with a <code>C</code> library). This version also returns a <code>string</code> instead of taking a <code>char *</code> as a parameter.</p></li> <li><p>I'm using the stack from the standard library, <code>&lt;stack&gt;</code>, which is slightly different to your home-rolled one. <code>top()</code> shows you the next element <em>without</em> removing it from the stack, and <code>pop()</code> returns <code>void</code>, but removes the top element from the stack.</p></li> <li><p>It's a free function, not part of a class, but that should be easy to modify - it's simply easier for me to test this way.</p></li> <li><p>I'm not convinced your operator precedence tables are correct, however, I'll let you double check that.</p></li> </ol> <hr> <pre><code>#include &lt;stack&gt; #include &lt;cctype&gt; #include &lt;iostream&gt; std::string convertToPostfix(std::string&amp; infix) { std::string postfix; //Our return string std::stack&lt;char&gt; stack; stack.push('('); //Push a left parenthesis ‘(‘ onto the stack. infix.push_back(')'); //We know we need to process every element in the string, //so let's do that instead of having to worry about //hardcoded numbers and i, j indecies for(std::size_t i = 0; i &lt; infix.size(); ++i) { //If it's a digit, add it to the output //Also, if it's a space, add it to the output //this makes it look a bit nicer if(isdigit(infix[i]) || isspace(infix[i])) { postfix.push_back(infix[i]); } //Already iterating over i, so //don't need to worry about i++ //Also, these options are all mutually exclusive, //so they should be else if instead of if. //(Mutually exclusive in that if something is a digit, //it can't be a parens or an operator or anything else). else if(infix[i] == '(') { stack.push(infix[i]); } //This is farily similar to your code, but cleaned up. //With strings we can simply push_back instead of having //to worry about incrementing some counter. else if(isOperator(infix[i])) { char operator1 = infix[i]; if(isOperator(stack.top())) { while(!stack.empty() &amp;&amp; precedence(operator1,stack.top())) { postfix.push_back(stack.top()); stack.pop(); } } //This shouldn't be in an else - we always want to push the //operator onto the stack stack.push(operator1); } //We've hit a right parens - Why you had a for loop //here originally I don't know else if(infix[i] == ')') { //While top of stack is not a right parens while(stack.top() != '(') { //Insert into postfix and pop the stack postfix.push_back(stack.top()); stack.pop(); } // Discard the left parens - you'd forgotten to do this stack.pop(); } } //Remove any remaining operators from the stack while(!stack.empty()) { postfix.push_back(stack.top()); stack.pop(); } } </code></pre>
    singulars
    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. VO
      singulars
      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