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
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...
* the Algorithm: * write a java program to convert infix to postfix create(OpStk) OpStk.push("#") token...
* the Algorithm: * write a java program to convert infix to postfix create(OpStk) OpStk.push("#") token = nextToken() while (token != "#") if (token is an operand) append token to postfix expression else if (token is "(") // Left paren - Start of sub-expr OpStk.push( token ) else if (token is ")") // Right paren - End of sub-expr pop operators off the stack and append to the postfix expression - stop when you've popped a "(" else (token is...
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 program that creates an output file named rand_nums.txt. Open the file and write 100...
Write a program that creates an output file named rand_nums.txt. Open the file and write 100 random integers between -50 and +50 (inclusive) to the file. Be sure to handle any file IO exceptions. Remember to close the file. Write a program that opens rand_nums.txt for input. Create two output files pos.txt and neg.txt. Read through the input file, one line at a time, converting each line into an integer (no exception handling, yet). If the number is positive, write...
Please write a java program to write to a text file and to read from a...
Please write a java program to write to a text file and to read from a text file.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT