This is not the exact method, there are some modifications made but it does converts Infix expression to the postfix expression.
CODE:
import java.util.Stack;
import java.util.*;
class InfixtoPostfix
{
static int Prec(char ch) {
switch (ch) {
case '+':
case '-':
return 1;
case '*':
case '/':
return 2;
case '^':
return 3;
}
return -1; }
// The main method that converts given infix expression
// to postfix expression.
static String WritetoPostfix(String exp) {
// initializing empty String for result
String result = new String("");
// initializing empty stack
Stack<Character> stack = new Stack<>();
for (int i = 0; i<exp.length(); ++i) {
char c = exp.charAt(i);
// If the scanned character is an operand, add it to
output.
if (Character.isLetterOrDigit(c))
result += c;
// If the scanned character is an '(', push it to the stack.
else if (c == '(')
stack.push(c);
// If the scanned character is an ')', pop and output from the
stack
// until an '(' is encountered.
else if (c == ')') {
while (!stack.isEmpty() && stack.peek() != '(')
result += stack.pop();
if (!stack.isEmpty() && stack.peek() !=
'(')
return "Invalid Expression"; // invalid expression
else
stack.pop(); }
else {
while (!stack.isEmpty() && Prec(c) <=
Prec(stack.peek()))
result += stack.pop();
stack.push(c);
} }
// pop all the operators from the stack
while (!stack.isEmpty())
result += stack.pop();
return result; }
// Main Method
public static void main(String[] args)
{
String exp = "-(2*i+3)*5";
System.out.println(WritetoPostfix(exp));
}
}
Output :

I was wondering if anyone can help me with the following method. I am not sure implement it. prot...
In Java syntax
I was wondering if anyone can help me with the following method. I am not sure implement it. protected void writeToPostfix(char c) // Check the order settings on this based on the operator precedence settings while(operatorStack.) this.postfixExpression- this.postfixExpression C; Additional information: One example of this is for example during a conversion of an infix expression-( 2 +-3 ) * 5 when outputing in the console as a postfix expression it should return 21 3+5* instead of-2+3*9
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...
i want similar for this code to solve two questions : 1- Write a program to convert a postfix expression to infix expression 2-Write a program to convert an infix expression to prefix expression each question in separate code ( so will be two codes ) #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;...
Help me to fix this code in C language. This code converts infix expressions to postfix and then evaluate the expression. Right now, it works with single digits. I need to modify it so that it can evaluate expressions with also 2 digits, example: (60+82)%72. Additionally I need to display an error when the parenthesis don't match like (89+8(. I have muted some line that would print the postfix expression with a space like: 22 8 + when the input...
Infix Expression Evaluator For this project, write a C program that will evaluate an infix expression. The algorithm REQUIRED for this program will use two stacks, an operator stack and a value stack. Both stacks MUST be implemented using a linked list. For this program, you are to write functions for the linked list stacks with the following names: int isEmpty (stack); void push (stack, data); data top (stack); void pop (stack); // return TRUE if the stack has no...
Infix Expression Evaluator For this project, write a C program that will evaluate an infix expression. The algorithm REQUIRED for this program will use two stacks, an operator stack and a value stack. Both stacks MUST be implemented using a linked list. For this program, you are to write functions for the linked list stacks with the following names: int isEmpty (stack); void push (stack, data); data top (stack); void pop (stack); // return TRUE if the stack has no...
I need assistance with this code. Is there any way I can create
this stack class (dealing with infix to postfix then postfix
evaluation) without utilizing <stdio.h> and
<math.h>?
____________________________________________________________________________________________
C++ Program:
#include <iostream>
#include <string>
#include <stdio.h>
#include <math.h>
using namespace std;
//Stack class
class STACK
{
private:
char *str;
int N;
public:
//Constructor
STACK(int maxN)
{
str = new char[maxN];
N = -1;
}
//Function that checks for empty
int empty()
{
return (N == -1);
}
//Push...
For this project you will implement a simple calculator. Your calculator is going to parse infix algebraic expressions, create the corresponding postfix expressions and then evaluate the postfix expressions. The operators it recognizes are: +, -, * and /. The operands are integers. Your program will either evaluate individual expressions or read from an input file that contains a sequence of infix expressions (one expression per line). When reading from an input file, the output will consist of two files:...
Convert infix to postfix, and evaluate postfix using custom Stack created using a singly linked list. This is only supposed to use THAT method, calling a normal Stack will give me a zero. I do have the conversion to postfix, but there may be error in there. But the main problem currently is the evaluation of postfix. I keep getting an error that I made for an empty stack, which I will include. For testing it is only supposed to...
I NEED SAMPLE PRINT OUT AS WELL AS CODE PLEASE!!!! Objectives: To gain experience with stacks. Documentation: Explain the purpose of the program as detail as possible - 8%. Develop a solution for the problem and mention algorithms to be used -12% List data structures to be used in solution. - 5%. Give a description of how to use the program and expected input/output - 5% Explain the purpose of each class you develop in the program. - 5%. Programming:...