Printing individual operations from a postfix expression string in C++

Printing individual operations from a postfix expression string in C++

Wanting help with breaking apart postfix expressions and outputting each individual operation. My issue is NOT evaluating the actual expression, but rather printing each individual operation before evaluating.

Wanting help with breaking apart postfix expressions and outputting each individual operation. My issue is NOT evaluating the actual expression, but rather printing each individual operation before evaluating.

I am using C++ to write a program that evaluates postfix expressions. The types of expressions that will be taken in are in infix notation, and use capital letters (A-Z) and these four operations: *, /, +, and -.

An example of this would be: (A + B) * (F - G)

I have written a function that changes infix to postfix:

AB+FG-*

Now I would like to write a function that outputs each individual operation. For the example above, it would look something like this:

Operations:

AB+

FG-

AB+FG-*

It would need to output the operations in order of precedence. Since A and B are in parentheses, they would need to be done first, and so on. I have tried to write a function that takes a postfix expression as input and returns nothing. The function creates a stack of type char and loops through the expression from beginning to end.

If the character is an operand, it is pushed to the stack. If the character is an operator, the two top values of the stack are stored in char variables and then outputted with their respective operator.

However, my function does not seem to be outputting the operations correctly.

#include<iostream>
#include<stack>
#include <algorithm> 
#include <cstdlib>
#include<string>

void operations(string exprsn) {

char op1, op2; int len, x, j = 0; len = exprsn.length(); stack<char>s; string ns;

for (int i = 0; i < len; i++) {

if (exprsn[i] &gt;= 'A' &amp;&amp; exprsn[i] &lt;= 'Z') {
    s.push(exprsn[i]);
}

else if (isOperator(exprsn[i])) {
    op1 = s.top();
    s.pop();
    op2 = s.top();
    s.pop();
    switch (exprsn[i]) {
    case '+':
        s.push(op2 + op1 + '+' );
        cout &lt;&lt; op2 &lt;&lt; op1 &lt;&lt; "+\n";
        break;
    case '-':
        s.push(op2 + op1 + '-');
        cout &lt;&lt; op2 &lt;&lt; op1 &lt;&lt; "-\n";
        break;
    case '*':
        s.push(op2 + op1 + '*');
        cout &lt;&lt; op2 &lt;&lt; op1 &lt;&lt; "*\n";
        break;
    case '/':
        s.push(op2 + op1 + '/');
        cout &lt;&lt; op2 &lt;&lt; op1 &lt;&lt; "/\n";
        break;
    }

    }


}

}

int main(){ string s = "AB+CD-*"; operations(s); return 0; }

Expected output:

AB+

CD-

AB+CD-*

Actual output:

AB+

CD-

«┤*

I am not sure what's happening with the last line, but I think it has something to do with the way I am pushing the characters into the stack.

Here is another example:

Infix expression: ( A + B ) / C + ( D - E ) * F * ( G - H )

After being converted to postfix notation:

AB+C/DE-FGH-+

Expected output:

AB+

DE-

GH-

DE-F*

GH-*

AB+C/

AB+C/DE-FGH-+

Actual output:

AB+

«C/

DE-

╢F*

GH-

&╝*

+

I know I am going horribly wrong somewhere, and I am having difficulty understanding. Any help is greatly appreciated. Thank you.

Angular 9 Tutorial: Learn to Build a CRUD Angular App Quickly

What's new in Bootstrap 5 and when Bootstrap 5 release date?

Brave, Chrome, Firefox, Opera or Edge: Which is Better and Faster?

How to Build Progressive Web Apps (PWA) using Angular 9

What is new features in Javascript ES2020 ECMAScript 2020

C/C++ vs. Rust: A developer’s perspective

In this post, you'll see the difference between Rust and C/C++ in a developer’s perspective

Variable Introduction in C#[Bangla]

LIKE | COMMENT | SHARE | SUBSCRIBE A variable is nothing but a name given to a storage area that our programs can manipulate. Each variable in C# has a speci...