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 (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 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...
: In this assignment you will write a C++ program that evaluates an arithmetic expression (represented...
: In this assignment you will write a C++ program that evaluates an arithmetic expression (represented with an infix notation), then outputs this expression in prefix form and also outputs the result of the calculation. The program will first convert the input infix expression to a prefix expression (using the Stack ADT) and then calculate the result (again, using the Stack ADT). The details are provided in the following sections.
In Java, write class InfixToPostfixConverter co convert an ordinary infix arithmetic expression (assume a valid expression...
In Java, write class InfixToPostfixConverter co convert an ordinary infix arithmetic expression (assume a valid expression is entered) with single-digit integers (to make things easier) such as (6 + 2) • 5 - 8 / 4 to a postfix expression. The postfix version (no parentheses are needed) of this infix expression is 6 2 + 5 * 8 4 / - The program should read the expression into StringBuilder or String infix and use the Stack and Stack Interface class...
Write a Java program that reads a list of integers into an array. The program should...
Write a Java program that reads a list of integers into an array. The program should read this array from the file “input.txt”. You may assume that there are fewer than 50 entries in the array. Your program determines how many entries there are. The output is a two-column list. The first column is the list of the distinct array elements; the second column is the number of occurrences of each element. The list should be sorted on entries in...
Write a JAVA program that reads in a string from standard input and determines the following:...
Write a JAVA program that reads in a string from standard input and determines the following: - How many vowels are in the string (FOR THE PURPOSE OF THIS PROGRAM 'Y' is NOT considered a vowel)? - How many upper case characters are in the string? - How many digits are in the string? - How many white space characters are in the string? - Modify the program to indicate which vowel occurs the most. In the case of a...
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.
Write a program to perform the following two tasks: 1. The program will accept a string...
Write a program to perform the following two tasks: 1. The program will accept a string as input in which all of the words are run together, but the first character of each word is uppercase. Convert the string to a string in which the words are separated by spaces and only the first word starts with an uppercase letter. For example, the string "StopAndSmellTheRose" would be converted to "Stop and smell the rose". Display the result string. 2. Then...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT