Question

In: Computer Science

Write a program that performs the following two tasks in java Reads an arithmetic expression in...

Write a program that performs the following two tasks in java

  1. Reads an arithmetic expression in an infix form, stores it in a queue (infix queue) and converts it to a postfix form (saved in a postfix queue).
  2. Evaluates the postfix expression.

Use linked lists to implement the Queue and Stack ADTs. DO NOT USE BUILT IN JAVA CLASSES

Solutions

Expert Solution

code

solution

//output

/copyable code

// Driver.java

import java.util.Scanner;

public class Driver {

public static void main(String[] args) {

    

   

Scanner s1 = new Scanner(System.in);

String format = "";

do {

System.out.print("enter formula be in (infix/postfix):");

format = s1.nextLine().toLowerCase();

}while(!(format.equals("infix") || format.equals("postfix")));

System.out.print("please Enter your formula:");

String formula = s1.nextLine().replaceAll(" ", "");

String infix, postfix;

switch(format) {

case "infix":

infix = formula;

postfix = arthimetic.infix_To_Postfix(infix);

System.out.println("Infix:"+infix);

System.out.println("Postfix:"+postfix);

System.out.println("Answer:"+arthimetic.solvePostfix(postfix));

break;

case "postfix":

postfix = formula;

infix = arthimetic.postfixToInfix(postfix);

System.out.println("Infix:"+infix);

System.out.println("Postfix:"+postfix);

System.out.println("Answer:"+arthimetic.solvePostfix(postfix));

break;

}

}

}

//arithmetic.java

import java.util.Iterator;

import java.util.List;

import java.util.Scanner;

import java.util.Stack;

public class arthimetic {

   public static boolean precedence(char firstval, char secondval) {

       if (secondval == '(' || secondval == ')')

          return false;

       if ((firstval == '*' || firstval == '/') && (secondval == '+' || secondval == '-'))

           return false;

       else

           return true;

   }

   public static java.awt.Point highest_point( List<java.awt.Point> points){

       Iterator < java.awt.Point> pointIterator = points.iterator();

     

       java.awt.Point highest_point = null;

     

       while(pointIterator.hasNext()) {

           java.awt.Point p = pointIterator.next();

           if(highest_point == null)

               highest_point = p;

           else if(highest_point != null && p.getY() > highest_point.getY() )

               highest_point = p;

       }

     

     

       return highest_point;

   }

public class Outer{

   int value = 5;

   public void displayString() {

       class InnerClass1{

           int value2 = 10;

           public String getString() {

               return "Outer:" + value + ", Inner:" + value2;

           }

         

       }

     

       InnerClass1 ic = new InnerClass1();

       System.out.println(ic.getString());

     

   }

   int all_Even_Bits(int x) {

     

     

       int maskdata = 0xAAAAAAAA;

     

       int answer = x | maskdata ;

     

       return ~answer;

   }

   void Test(){

       displayString();

   }

}

public static String infix_To_Postfix(String infix) {

   char character[] = infix.toCharArray();

       Stack<Character> st = new Stack<Character>();

       for (int i = 0; i < character.length; i++) {

           if (character[i] == '(')

               st.push(character[i]);

           if (character[i] == ')') {

               if (st.isEmpty()) {

                   return "No matching open parenthesis error";

               }

               st.pop();

           }

       }

       if (!st.isEmpty())

           return "No matching error";

       // initialize

       String postfixString = "";

       // initialize the stack

       Stack<Character> stack1 = new Stack<Character>();

       // index

       for (int i = 0; i < infix.length(); i++) {

           char ch = infix.charAt(i);

           if (Character.isLetter(ch) || Character.isDigit(ch))

               postfixString = postfixString + ch;

           else if (ch == '(') {

               stack1.push(ch);

           } else if (ch == ')') {

               while (stack1.peek() != '(') {

                   postfixString = postfixString + stack1.pop();

               }

               stack1.pop();

           } else {

               while (!stack1.isEmpty() && !(stack1.peek() == '(') && precedence(ch, stack1.peek()))

                   postfixString = postfixString + stack1.pop();

               stack1.push(ch);

           }

       }

       while (!stack1.isEmpty())

           postfixString = postfixString + stack1.pop();

       return postfixString;

}

public static boolean isch(char ch) {

return (ch >= 'a' && ch <= 'z') ||

(ch>= 'A' && ch <= 'Z') || Character.isDigit(ch);

}

public static String postfixToInfix(String postfix) {

    

    

   Stack<String> stack = new Stack<String>();

    

   for (int i = 0; i < postfix.length(); i++)

   {

   if (isch(postfix.charAt(i)))

       stack.push(postfix.charAt(i) + "");

   else

   {

   String op1 = stack.peek();

   stack.pop();

   String op2 = stack.peek();

   stack.pop();

   stack.push("(" + op2 + postfix.charAt(i) +

           op1 + ")");

   }

   }

   return stack.peek();

}

public static double solvePostfix(String s) {

Stack<Double> postfix = new Stack<Double>();

       for (int i = 0; i < s.length(); i++) {

           char c = s.charAt(i);

           if (Character.isDigit(c))

               postfix.push((double) (c - '0'));

           else {

               double val1 = postfix.peek();

               postfix.pop();

               double val2 = postfix.peek();

               postfix.pop();

               switch (c) {

               case '+':

                   postfix.push(val2 + val1);

                   break;

               case '-':

                   postfix.push(val2 - val1);

                   break;

               case '/':

                   postfix.push(val2 / val1);

                   break;

               case '*':

                   postfix.push(val2 * val1);

                   break;

               }

           }

       }

       return postfix.peek();

}

}


Related Solutions

Write a program that performs the following two tasks: Reads an arithmetic expression in an infix...
Write a program that performs the following two tasks: Reads an arithmetic expression in an infix form, stores it in a queue (infix queue) and converts it to a postfix form (saved in a postfix queue). Evaluates the postfix expression. Use the algorithms described in class/ lecture notes. Use linked lists to implement the Queue and Stack ADTs. Using Java built-in classes will result in 0 points. You must use your own Stack and Queue classes (my code is a...
In Java: Problem 1. Write a program that performs the following two tasks: 1. Reads an...
In Java: Problem 1. Write a program that performs the following two tasks: 1. Reads an arithmetic expression in an infix form, stores it in a queue (infix queue) and converts it to a postfix form (saved in a postfix queue). 2. Evaluates the postfix expression. Use the algorithms described in class/ lecture notes. Use linked lists to implement the Queue and Stack ADTs. Using Java built-in classes will result in 0 points. You must use your own Stack and...
Arithmetic Expression Evaluation Write a program that reads an infix expression (that contains only A, B...
Arithmetic Expression Evaluation Write a program that reads an infix expression (that contains only A, B and/or C as operands) from the user and prints out the value of the expression as an output. Implement the following methods: o String infixToPostfix(String expr) Converts the given infix expression expr and returns the corresponding postfix notation of expr. o Integer evaluatePostfixExpr(String expr) Evaluates and returns the value of the given postfix expression expr. Sample Input/Output #1 Enter your arithmetic expression: A+B-C*A Enter...
Write a program (preferably in Java) that, given an arithmetic expression, first transforms it to a...
Write a program (preferably in Java) that, given an arithmetic expression, first transforms it to a postfix form, and then computes its value (by using stack-based algorithms). Assume that all the numbers in the arithmetic expression are one-digit numbers, i.e., each of these numbers is either 0, or 1, or 2, ..., or 9. For example, your program should correctly process expressions like 2+3*4, but there is no need to process expressions like 11+22.
Using Java 8. Write a program that reads an expression in postfix notation, builds the expression...
Using Java 8. Write a program that reads an expression in postfix notation, builds the expression tree and prints the expression in prefix and infix notation and evaluates the expression. (Hint use a stack)
Write a Java program which takes a String representing an arithmetic expression as an input and...
Write a Java program which takes a String representing an arithmetic expression as an input and displays whether or not the expression is balanced. If the expression is not balanced (i.e. the wrong # of parentheses or in the wrong order) it will display an error message. For Example: Input an expression: ( ( 2 + 4 ) * 2 ) Expression is balanced Input an expression: ( 5 * 7 – 6 ) ) Error: Expression is not balanced.......
Write a JAVA program that reads a text file into RAM efficiently, takes a regular expression...
Write a JAVA program that reads a text file into RAM efficiently, takes a regular expression from the user, and then prints every line that matches the RE.
Write a Java program that will use a two-dimensional array to solve the following tasks: 1....
Write a Java program that will use a two-dimensional array to solve the following tasks: 1. Create a method to generate a 2-dimensional array (random numbers, range 0 - 500). The array has ROW rows and COL columns, where ROW and COL are class constants. 2. Create a method to print the array. 3. Create a method to find the largest element in the array 4. Create a method to find the smallest element in the array 5. Create a...
You should write a small C++ program that performs the following tasks: First, your program should...
You should write a small C++ program that performs the following tasks: First, your program should read in a set of 5 integers from “standard in” (remember cin). The numbers that you read in will be either zero or positive. If at least one of the five numbers entered is non-zero then your program should calculate the sum of the numbers and report that back to the user using “standard output” (remember cout). Then your program should be ready to...
Write a Java program that reads two integers on the keyboard and displays them on the...
Write a Java program that reads two integers on the keyboard and displays them on the screen.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT