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.

c++

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

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

What is new features in Javascript ES2020 ECMAScript 2020

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Random Password Generator Online

HTML Color Picker online | HEX Color Picker | RGB Color Picker

Loops in C++ | For, While, and Do While Loops in C++

In this Video We are going to see how to use Loops in C++. We will see How to use For, While, and Do While Loops in C++.

Using isdigit() in C/C++

In this article, we'll take a look at using the isdigit() function in C/C++. This is a very simple way to check if any value is a digit or not. Let's look

C++ Tutorial for Beginners 19 - C++ Structures | Struct declaration | C++ Data Structures

Welcome to this course on C++ Tutorial for Beginners. In this video we will see How to use C++ Structures.

Object Oriented Programming in C++ | C++ OOPs Concepts | Learn Object Oriented C++

C++ is general purpose, compiled, object-oriented programming language and its concepts served as the basis for several other languages such as Java, Python, Ruby, Perl etc.

A Complete Guide to fread() in C/C++

In this article, we’ll take a look at using fread() in C/C++.