In C++, write a program that reads a postfix expression from an input file. Then, the program should evaluate the postfix format and display the results.
Use these concepts for the program:
1) Struct Node 2) Enqueue and Dequeue 3) Push and Pop 4) DO NOT USE classes 5) Possibly Stack
Precondition:
Example:
12+
Result is 3
C++ Code:
#include <bits/stdc++.h>
using namespace std;
struct Stack
{
int top;
int size;
int* vec;
};
int isEmpty(struct Stack* stack)
{
if(stack->top == -1)
{
return 1;
}
else
return 0;
}
char pop(struct Stack* stack)
{
if (!isEmpty(stack))
{
stack->top--;
return
stack->vec[stack->top+1] ;
}
return '$';
}
void push(struct Stack* stack, char operand)
{
stack->top+=1;
stack->vec[stack->top] = operand;
}
int Postfix(string line)
{
struct Stack* stack = (struct Stack*)
malloc(sizeof(struct Stack));
stack->size = line.length();
stack->top = -1;
stack->vec = (int*) malloc(stack->size *
sizeof(int));
for (int i = 0; i<line.length();
i++) // Scan all characters one by
one
{
if (line[i]>='0'
&& line[i] <='9') // if numbers push
them in the stack
push(stack, line[i] - '0');
else
// else it is an operator, so pop two operands and evalutae them
and push result to stack
{
int operand1 = pop(stack);
int operand2 = pop(stack);
if(line[i]=='+')
{
push(stack, operand2 + operand1);
}
else if(line[i]=='-')
{
push(stack, operand2 - operand1);
}
else if(line[i]=='*')
{
push(stack, operand2 * operand1);
}
else
{
push(stack, operand2 / operand1);
}
}
}
return pop(stack);
}
int main()
{
string line;
ifstream myfile ("input.txt");
if (myfile.is_open())
{
while ( getline
(myfile,line) )
{
//cout<<line<<endl;
}
myfile.close();
}
cout<<"Result is "<<
Postfix(line);
return 0;
}
-------------------------------------------------------------------------------------
Feel free to leave a comment if you have any doubt.
Happy Learning.
In C++, write a program that reads a postfix expression from an input file. Then, the...
write a program in c++ Read an infix expression from an input file and convert to postfix. Instead of displaying directly on the screen, first place in a queue, and then display the contents of the queue on the screen. Precondition: The expression will be read from a file (input.txt) that contains a single line. There will be no spaces between the operands and the operators. The following operators are allowed: ( ) + - * / The normal rules...
In C programming Language Write a version of the infix-to-postfix conversion algorithm. Write a program that converts an ordinary infix arithmetic expression (assume a valid expression is entered) with single-digit integers For Example: Infix expression (6 + 2) * 5 - 8 / 4 to a postfix expression is 62+5*84/- The program should read the expression into character array infix and use the stack functions implemented in this chapter to help create the postfix expression in character array postfix. The...
Total point: 15 Introduction: For this assignment you have to write a c program that will take an infix expression as input and display the postfix expression of the input. After converting the postfix expression, the program should evaluate the expression from the postfix and display the result. What should you submit? Write all the code in a single file and upload the .c file. Problem We as humans write math expression in infix notation, e.g. 5 + 2 (the...
Using ADT Stack: Evaluating infix expressions by converting them to postfix expressions Postfix notation: In a postfix expression, a binary operation follows its two opperands. The order of the operands in a infix expression is the same as in the corresponding postfix expression but the order of the operators might change based on the precedence of the operators and the existing of paranthses. Infix Postfix a + b a b + (a + b) * c a b + c...
Stacks are used by compilers to help in the process of
evaluating expressions and generating machine language code.In this
exercise, we investigate how compilers evaluate arithmetic
expressions consisting only of constants, operators and
parentheses. Humans generally write expressions like 3 + 4and 7 /
9in which the operator (+ or / here) is written between its
operands—this is called infix notation. Computers “prefer” postfix
notation in which the operator is written to the right of its two
operands. The preceding...
Question: Write a Java program for Evaluating Postfix Expression 1. Input a postfix expression from user. 2. Evaluate expression using double stack made of your own linked list. 3. Show the result of expression or error if incorrect. Evaluating Postfix Expression Input an expression: 2 10 + 9 6 - / Evaluating Postfix Expression Input an expression: 20 10 + 9 6 - 1 Evaluating Postfix Expression Input an expression: 2 10 + 9 - / Result = 4.0 Result...
JAVA, please You must write a robust program meaning that your program should not crash with any given data. Data validation must be done any time that user enters an input. Write a program that 1. Gets an infix expression form the user and evaluate the expression using stack ADT a. Finds the postfix equivalent of the given infix expression b. Evaluate the created postfix expression. c. Note: your program should not crash when you enter an invalid expression such...
Python Issue Postfix notation (also known as Reverse Polish Notation or RPN in short) is a mathematical notation in which operators follow all of its operands. It is different from infix notation in which operators are placed between its operands. The algorithm to evaluate any postfix expression is based on stack and is pretty simple: Initialize empty stack For every token in the postfix expression (scanned from left to right): If the token is an operand (number), push it on...
Write a java program for the following: Your program reads an infix expression represented by a string S from the standard input (the keyboard). Then your program converts the infix expression into a postfix expression P using the algorithm. Next, your program evaluates the postfix expression P to produce a single result R. At last, your program displays the original infix expression S, the corresponding postfix expression P and the final result R on the standard output ( the screen...
We as humans write math expression in infix notation, e.g. 5 + 2 (the operators are written in-between the operands). In a computer’s language, however, it is preferred to have the operators on the right side of the operands, i.e. 5 2 +. For more complex expressions that include parenthesis and multiple operators, a compiler has to convert the expression into postfix first and then evaluate the resulting postfix. Write a program that takes an “infix” expression as input, uses...