Question

In: Computer Science

write a java program tht evaluates postfix expressions. args[0] = the output file the answer will...

write a java program tht evaluates postfix expressions. args[0] = the output file the answer will print to

Solutions

Expert Solution

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Stack;

public class Main {
   public 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;
               }
           }
       }
       return stack.pop();  
   }

   public static void main(String[] args) throws IOException {
       if (args.length != 1) {
           System.out.println("Invalid command line arguments....");
       } else {
           String fileName = args[0];
           String exp = "231*+9-";
           BufferedWriter writer = new BufferedWriter(new FileWriter(fileName));
            writer.write("Postfix evaluation of " + exp + " is : " + evaluatePostfix(exp));
            writer.close();
       }
   }
}


Related Solutions

Write a program that converts prefix expressions to postfix and postfix expressions to prefix. (java) The...
Write a program that converts prefix expressions to postfix and postfix expressions to prefix. (java) The program for this project should consist of three classes. -The main class should create a GUI that allows the user to input an expression in either prefix or postfix and convert it to postfix or prefix, respectively. -The other class should contain the code to perform the conversions. --->The pseudocode for prefix to postfix, which requires two stacks, is shown below: tokenize the string...
Write a class Lab7 that reads and evaluates postfix expressions contained in a file. Each line...
Write a class Lab7 that reads and evaluates postfix expressions contained in a file. Each line of the file contains a postfix expression with integers as operands and ‘+’, ‘-’, ‘*’ and ‘/’ as operators. Consecutive operands and operators are separated by spaces. Note the following requirements for the lab: • The main method in Lab7.java should do the following. 1. ask the user for the name of the file containing the expressions, 2. for each line of the file,...
Postfix Evaluation (JAVA PROGRAMMING) Write class PostfixEva1uator that evaluates a postfix expression such as 6 2...
Postfix Evaluation (JAVA PROGRAMMING) Write class PostfixEva1uator that evaluates a postfix expression such as 6 2 + 5 * 8 4 / - The program should read a postfix expression consisting of single digits and operators into a StringBuilder, The program should read the expression and evaluate it (assume it's valid). The algorithm to evaluate a postfix expression is shown below. Use +, -, *, /, and ^. ^ is the exponent. Append a right parenthesis ') ' to the...
C++ Data Structure Write a program to change following infix expressions to postfix expressions using a...
C++ Data Structure Write a program to change following infix expressions to postfix expressions using a stack a) D-B+C b) C*D+A*B c) (A*B)*C+D*F-C d) (A-4*(B-C)-D/E)*F
Lab 7. Boolean Expressions a) Write a program that evaluates the following expressions. Assign reasonable values...
Lab 7. Boolean Expressions a) Write a program that evaluates the following expressions. Assign reasonable values to the variables. Print the results. a<b≥c , √a−7 b2 ≠c , d∨e∧f , a<b∨¬d ∧means and, ∨means inclusive or, ¬ means not. b) Write a program that asks a user whether he or she wants to become a Java programmer and determines if the user typed “yes” (Print true if yes and false otherwise.) Don't use the if statement here
Write in Java Postfix notation is a way of writing expressions without using parentheses. For example,...
Write in Java Postfix notation is a way of writing expressions without using parentheses. For example, the expression (1 + 2) * 3 would be written as 1 2 + 3 *. A postfix expression is evaluated using a stack. Scan a postfix expression from left to right. A variable or constant is pushed into the stack. When an operator is encountered, apply the operator with the top two operands in the stack and replace the two operands with the...
C++ OOP •Write a program that evaluates a postfix expression (assume it’s valid) such as 6...
C++ OOP •Write a program that evaluates a postfix expression (assume it’s valid) such as 6 2 + 5 * 8 4 / - •The program should read a postfix expression consisting of digits and operators into a string. •The algorithm is as follows: 1.While you have not reached the end of the string, read the expression from left to right. –If the current character is a digit, Push its integer value onto the stack (the integer value of a...
Write a java program: Write a program that creates a text file. Write to the file...
Write a java program: Write a program that creates a text file. Write to the file three lines each line having a person's name. In the same program Append to the file one line of  'Kean University'.  In the same program then Read the file and print the four lines without lines between.
IN JAVA PLZ follow all directions SHOW OUPUT! Write class PostfixEvaluator that evaluates a postfix expression...
IN JAVA PLZ follow all directions SHOW OUPUT! Write class PostfixEvaluator that evaluates a postfix expression such as 6 2 + 5 * 8 4 / - The program should read a postfix expression consisting of single digits and operators into a StringBuilder, The program should read the expression and evaluate it (assume it's valid). The algorithm to evaluate a postfix expression is shown below. Use +, -, *, /, and ^. ^ is the exponent. Append a right parenthesis...
Submit a java file. Write a program where you input 4 numbers and the output generates...
Submit a java file. Write a program where you input 4 numbers and the output generates the average of the 4 numbers. The output may not be negative and <= 100. If any value is negative or >100, it should be replaced by the value of 30. For all such values, you will replace them with a value of 10. The program should produce the following output: Today's average amount is: $xx.xx.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT