Question

In: Computer Science

C++ Data Structure Write a program to change following infix expressions to postfix expressions using a...

C++ Data Structure

Write a program to change following infix expressions to postfix expressions using a stack

a) D-B+C

b) C*D+A*B

c) (A*B)*C+D*F-C

d) (A-4*(B-C)-D/E)*F

Solutions

Expert Solution

Code:

#include <iostream>
using namespace std;
#include <string.h>
void push(char c);
int pri(char s);
char pop();
int n=20;
int stack[20];
int top=-1;
int main()
{
int i,j=0;
char infix[20],l;
char postfix[20];
gets(infix);
for(i=0;infix[i]!='\0';i++)
{
if(infix[i]=='(')
{
push(infix[i]);
}
else if(infix[i]==')')
{
while(top!=-1 && stack[top]!='(')
{
postfix[j]=pop();
j++;
}
if(stack[top]=='(')
{
pop();
}
}
else if(isdigit(infix[i]) || isalpha(infix[i]))
{
postfix[j]=infix[i];
j++;
}
else if(infix[i]=='+' || infix[i]=='-' || infix[i]=='*' || infix[i]=='/' || infix[i]=='%')
{
while(top!=-1 && infix[i]!='(' && (pri(stack[top])>pri(infix[i])))
{
postfix[j]=pop();
j++;
}
push(infix[i]);
}
else
{
  
}
}
while(top!=-1 && stack[top]!='(')
{
postfix[j]=pop();
j++;
}
postfix[j]='\0';
puts(postfix);
return 0;
}
void push(char c)
{
top++;
stack[top]=c;
}
char pop()
{
char d;
d=stack[top];
top--;
return d;
}
int pri(char s)
{
if(s=='*' || s=='/' || s=='%')
return 1;
else if(s=='+' || s=='-')
return 0;
else
return -1;
}

Ouput:

Indentation:


Related Solutions

Write a program that converts prefix expressions to postfix and postfix expressions to prefix. (java) The...
Write a program that converts prefix expressions to postfix and postfix expressions to prefix. (java) The program for this project should consist of three classes. -The main class should create a GUI that allows the user to input an expression in either prefix or postfix and convert it to postfix or prefix, respectively. -The other class should contain the code to perform the conversions. --->The pseudocode for prefix to postfix, which requires two stacks, is shown below: tokenize the string...
Using a stack, write a program that turns a simple infix arithmetic expression into a postfix...
Using a stack, write a program that turns a simple infix arithmetic expression into a postfix expression. For example, 1 + 2 * 3 becomes 2 3 * 1 +. Also, evaluate the expression to ensure the expression is correct.
CS 209 Data Structure (Postfix notation) Postfix notation is a way of writing expressions without using...
CS 209 Data Structure (Postfix notation) Postfix notation is a way of writing expressions without using parentheses. For example, the expression (1 + 2) * 3 would be written as 1 2 + 3 *. A postfix expression is evaluated using a stack. Scan a postfix expression from left to right. A variable or constant is pushed into the stack. When an operator is encountered, apply the operator with the top two operands in the stack and replace the two...
C++ program that can take in EITHER "infix" or "prefix" (NOT POSTFIX) and transforms it into...
C++ program that can take in EITHER "infix" or "prefix" (NOT POSTFIX) and transforms it into the other one. Should use stack and have infixToPrefix and prefixToInfix functions with a simple main function for execution.
**write a java program infix to postfix**showing the expression tree*** I. Input All input data are...
**write a java program infix to postfix**showing the expression tree*** I. Input All input data are from a file "in.dat". The file contains a sequence of infix form expressions, one per line. The character '$' is an end mark. For example, the following file has four infix form expressions: 1 + 2 * 3 ^ ! ( 4 == 5 ) $ 3 * ( 6 - 4 * 5 + 1) + 2 ^ 2 ^ 3 $ 77...
* 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...
Using STL stack class, implement in C++ a function that converts an infix expression to postfix...
Using STL stack class, implement in C++ a function that converts an infix expression to postfix expression,
(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...
C++ OOP Make a program to evaluate infix arithmetic expressions containing integer operands and the operators...
C++ OOP Make a program to evaluate infix arithmetic expressions containing integer operands and the operators + (addition), - (subtraction), * (multiplication), / (division) and pairs of parentheses, properly nested. Use the following two-stack algorithm (by E. W. Dijkstra): If the next token in the expression is an integer, push the integer onto the value stack. If the next token in the expression is an operator, If the operator stack is empty or the priority of the operator is greater...
write a java program tht evaluates postfix expressions. args[0] = the output file the answer will...
write a java program tht evaluates postfix expressions. args[0] = the output file the answer will print to
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT