Question

In: Computer Science

implement the algorithm described in this chapter to convert a prefix expression to postfix form. involve...

implement the algorithm described in this chapter to convert a prefix expression to postfix form. involve the classes that programming problems 4 and 5 describe

This is to be written in C++. I cannot provide anymore information, I cannot provide any class information, etc. This is all the problem that book gave me. This is for Data Structures and algorithms.

Solutions

Expert Solution

//code


#include <iostream>
#include <stack>
using namespace std;

bool checkIfOperator(char x) {
switch (x) {
case '+':
case '-':
case '/':
case '*':
   return true;
}
return false;
}
string PrefixToPostfix(string prefix) {

stack<string> s;

for (int i = prefix.size() - 1; i >= 0; i-=1) {

   if (checkIfOperator(prefix[i])) {

   string a = s.top(); s.pop();
   string b = s.top(); s.pop();

   string temp = a + b + prefix[i];

   s.push(temp);
   }

   else {

   s.push(string(1, prefix[i]));
   }
}
return s.top();
}
int main() {
string prefix;
cin >> prefix;
cout << "Postfix : " << PrefixToPostfix(prefix);
return 0;
}


Related Solutions

2. Convert the following infix form expression into the postfix form expression by using a stack:...
2. Convert the following infix form expression into the postfix form expression by using a stack: A+(B*(C-D)+E)/F-G*H Show the stack after each push/pop.
An arithmetic expression can be represented in three different formats: infix, prefix, and postfix. In the...
An arithmetic expression can be represented in three different formats: infix, prefix, and postfix. In the infix notation, the operator comes between two operands. In the postfix notation, the operator comes after its two operands. For example, an expression a/b can be transformed to ab/. The following are some examples of the postfix expressions: 2+6*4 Its postfix notation is 2 6 4 * + 2-3+5/4*9-1 Its postfix expression is 2 3 – 5 4 / 9 * + 1 -...
Convert an infix expression to its postfix representation in JAVA
Convert an infix expression to its postfix representation in JAVA
Implement an infix expression to postfix expression convertor in C++. Note: - Support +, -, *,...
Implement an infix expression to postfix expression convertor in C++. Note: - Support +, -, *, /, ( and ) in infix expressions. - Operands will be nonnegative only. - Assume infix expressions are valid and well formatted (one blank space to separate operand/operator) For example, - 3 * 4 + 5 ➔ 3 4 * 5 + - 3 + 4 * 5 ➔ 3 4 5 * + - (3 + 4) * (5 + 6) ➔ 3...
(Convert infix to postfix) Note: Postfix notation is a way of writing expression without using parentheses....
(Convert infix to postfix) Note: Postfix notation is a way of writing expression without using parentheses. For example, the expression ( 11 + 12 ) * 13 would be written as 11 12 + 13 * Assume that ALWAYS there is a space between operands and operators in the input expression. Use two stacks, one to store the operands and one to store the operators. Your program only accpets following operators : ( ) + - / * Write a...
java Convert the following Infix expression to a Postfix expression. Show all work                            Infix Expres
java Convert the following Infix expression to a Postfix expression. Show all work                            Infix Expression                                                 Stack                             Postfix Expression ---------------------------------------------------------------------------------------------------------------------------- 8 - 9*(2 + 1/4) + 3*7                                                     (empty)                            (blank) Evaluate the following Postfix expression. Once again, show all work.            Postfix                                               Stack                     Result           ---------------------------------------    --------------------    --------------            2 7 + 12 4 / * 8 5 + -
this is my code I want the opposite i want to convert a postfix expression to...
this is my code I want the opposite i want to convert a postfix expression to infix expression #include <iostream> #include <string> #define SIZE 50 using namespace std; // structure to represent a stack struct Stack {   char s[SIZE];   int top; }; void push(Stack *st, char c) {   st->top++;   st->s[st->top] = c; } char pop(Stack *st) {   char c = st->s[st->top];   st->top--;   //(A+B)*(C+D)   return c; } /* function to check whether a character is an operator or not. this function...
Convert the expression into postfix notation 19 + 2 ∗ 5  + (1 - 6/(1 ∗ 2))
Convert the expression into postfix notation 19 + 2 ∗ 5  + (1 - 6/(1 ∗ 2))
Implement the Metropolis-Hastings algorithm below¶ Make sure to read this: Implement the algorithm described above (in...
Implement the Metropolis-Hastings algorithm below¶ Make sure to read this: Implement the algorithm described above (in the "How it works" section), where you take some user-defined number of steps that randomly step in W and I and are scaled by a step_size. This means you don't want to take steps of a discrete size, but instead use a random distribution of step sizes that are scaled by your step_size variable. You'll want to take steps that have an equal chance...
* the Algorithm: * write a java program to convert infix to postfix create(OpStk) OpStk.push("#") token...
* the Algorithm: * write a java program to convert infix to postfix create(OpStk) OpStk.push("#") token = nextToken() while (token != "#") if (token is an operand) append token to postfix expression else if (token is "(") // Left paren - Start of sub-expr OpStk.push( token ) else if (token is ")") // Right paren - End of sub-expr pop operators off the stack and append to the postfix expression - stop when you've popped a "(" else (token is...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT