Question

Develop an Expression Manager that can do the following operations: Balanced Symbols Check • Read a...

Develop an Expression Manager that can do the following operations: Balanced Symbols Check

• Read a mathematical expression from the user.

• Check and report whether the expression is balanced.

• {, }, (, ), [, ] are the only symbols considered for the check.

All other characters can be ignored.

Infix to Postfix Conversion

• Read an infix expression from the user.

• Perform the Balanced Parentheses Check on the expression read.

• If the expression fails the Balanced Parentheses Check, report a message to the user that the expression is invalid.

• If the expression passes the Balanced Parentheses Check, convert the infix expression into a postfix expression and display it to the user.

• Operators to be considered are +, –, *, /, %

. Postfix to Infix Conversion

• Read a postfix expression from the user.

• Convert the postfix expression into an infix expression and display it to the user.

• Display an appropriate message if the postfix expression is not valid.

• Operators to be considered are +, –, *, /, %.

Evaluating a Postfix Expression

• Read the postfix expression from the user. • Evaluate the postfix expression and display the result.

• Display an appropriate message if the postfix expression is not valid.

• Operators to be considered are +, –, *, /, %.

• Operands should be only integers. Implementation

• Design a menu that has buttons or requests user input to select from all the aforementioned operations.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

import java.util.Stack;
import java.util.Scanner;

public class StackProblem {

   public static void main(String[] args) {
      
       Scanner s=new Scanner(System.in);
      
       System.out.println("enter your choice ");
       System.out.println("1. to check balanced parenthesis");
       System.out.println("2. convert infix to postfix");
       System.out.println("3. convert postfix to infix");
       System.out.println("4. evaluate postfix");
       System.out.println("5 or any other digit to exit");
      
      

       while(true)
       {
           int choice=s.nextInt();
           s.nextLine();
   switch(choice)
   {
   case 1:
   {
       String input=s.nextLine();
       if(checkBalanced(input))
           System.out.println("balanced");
       else
           System.out.println("not balanced");
       break;
   }
     
     
   case 2:
   {
       String input=s.nextLine();
       if(checkBalanced(input))
       {
       String res=infixToPostfix(input);
       System.out.println(res);
       }
       else
           System.out.println("not balanced");
       break;
   }
         
   case 3:
   {
       String input=s.nextLine();
       String ans=getInfix(input);
       System.out.println(ans);
       break;
         
   }
     
   case 4:
   {
       String input=s.nextLine();
       int total=evaluatePostfix(input);
       System.out.println(total);
       break;
   }
     
   default:
       return;
   }
      
       }
   }
  
  
  
   static int Prec(char ch)
{
switch (ch)
{
case '+':
case '-':
return 1;

case '%':
case '*':
case '/':
return 2;

  
}
return -1;
}

  
static String infixToPostfix(String exp)
{
  
String result = new String("");
  
  
Stack<Character> stack = new Stack<>();
  
for (int i = 0; i<exp.length(); ++i)
{
char c = exp.charAt(i);
  

if (Character.isLetterOrDigit(c))
result += c;

  
else if (c == '(')
stack.push(c);
  
  
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())){
if(stack.peek() == '(')
return "Invalid Expression";
result += stack.pop();
}
stack.push(c);
}

}

  
while (!stack.isEmpty()){
if(stack.peek() == '(')
return "Invalid Expression";
result += stack.pop();
}
return result;
}
  
  
static boolean isOperand(char x)
{
return (x >= 48 && x <= 57);
  
}
  
  
static String getInfix(String exp)
{
Stack<String> s = new Stack<String>();
  
for (int i = 0; i < exp.length(); i++)
{
  
if (isOperand(exp.charAt(i)))
{
s.push(exp.charAt(i) + "");
}
  
  
else
{
String op1 = s.peek();
s.pop();
String op2 = s.peek();
s.pop();
s.push("(" + op2 + exp.charAt(i) +
op1 + ")");
}
}
  
  
return s.peek();
}
  
  
  
static int evaluatePostfix(String exp)
{
  
Stack<Integer> stack=new Stack<>();
  
  
for(int i=0;i<exp.length();i++)
{
char c=exp.charAt(i);
  
  
if(Character.isDigit(c))
stack.push(c - '0');
  
  
else
{
int val1 = stack.pop();
int val2 = stack.pop();
  
switch(c)
{
case '+':
stack.push(val2+val1);
break;
  
case '-':
stack.push(val2- val1);
break;
  
case '/':
stack.push(val2/val1);
break;
  
case '*':
stack.push(val2*val1);
break;
  
case '%':
   stack.push(val2%val1);
}
}
}
return stack.pop();   
}
  
  
  
   public static boolean checkBalanced(String exp) {
  
int count=0;
for(int i=0;i<exp.length();i++)
{
if(exp.charAt(i)=='{' || exp.charAt(i)=='(' || exp.charAt(i)=='[' || exp.charAt(i)=='}' || exp.charAt(i)==')' || exp.charAt(i)==']')
count++;
}
  
if(count%2==1)
return false;
      
  
Stack<Character> s1=new Stack<Character>();
for(int i=0;i<exp.length();i++)
{
if(exp.charAt(i)=='{' || exp.charAt(i)=='(' || exp.charAt(i)=='[')
{
// char symbol=exp.charAt(i);
s1.push(exp.charAt(i));
}
else if(exp.charAt(i)=='}')
{
char elem=(char)s1.peek();
if(elem!='{')
return false;
s1.pop();
}
  
else if(exp.charAt(i)==')')
{
char elem=(char)s1.peek();
if(elem!='(')
return false;
s1.pop();
}
  
else if(exp.charAt(i)==']')
{
char elem=(char)s1.peek();
if(elem!='[')
return false;
s1.pop();
}
  
  
}
  
return s1.isEmpty();

   }

}

2.OUTPUT->


Add a comment
Know the answer?
Add Answer to:
Develop an Expression Manager that can do the following operations: Balanced Symbols Check • Read a...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • This programming assignment needs to be written so that it can do infix expression to postfix...

    This programming assignment needs to be written so that it can do infix expression to postfix expression (explained in the direction below). When the infix expression is translated into postfix, the parser needs to check if the operators and operands are accepted or rejected. It must look like the what is in the first page, asking the user to continue or end the translation.

  • write a program in c++ Read an infix expression from an input file and convert to...

    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...

  • You are to write a program name expressionTree.java that evaluates an infix expression entered by the...

    You are to write a program name expressionTree.java that evaluates an infix expression entered by the user. The expression may contain the following tokens: (1) Integer constants (a series of decimal digits). (2)   One alphabetic character - "x" (representing a value to be supplied later). (3)   Binary operators (+, -, *, / and % (modulo)). (4)   Parentheses          You will parse the input expression creating an expression tree with the tokens, then use the postOrder tree traversal algorithm to extract...

  • Stacks are used by compilers to help in the process of evaluating expressions and generating machine...

    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 NEED SAMPLE PRINT OUT AS WELL AS CODE PLEASE!!!! Objectives: To gain experience with stacks....

    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:...

  • We as humans write math expression in infix notation, e.g. 5 + 2 (the operators are...

    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...

  • This project is designed to practice with OOP, stack data structure, its applications, and C++/Java programming...

    This project is designed to practice with OOP, stack data structure, its applications, and C++/Java programming language. You will write a program that reads an infix expression, converts it to a postfix expression, evaluates the postfix expression, and prints out the answer. You must define and implement your own Stack class and a Calculator class. Your Stack class supports standard basic stack operations and you can implement it with an array or a linked list. You should create a class...

  • For this project you will implement a simple calculator. Your calculator is going to parse infix...

    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:...

  • Total point: 15 Introduction: For this assignment you have to write a c program that will...

    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...

  • Write a java program for the following: Your program reads an infix expression represented by a...

    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...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT