Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- import java.util.*;
- public class RPN {
- /**
- * Get a filename from the user.
- *
- * @return User specified filename
- */
- public static String getFilename() {
- // Display message asking user for a filename.
- // Read in the filename and return the filename.
- Scanner keyboard = new Scanner(System.in);
- // Filename entered by user
- String fileName;
- System.out.println("Enter a filename.");
- fileName = keyboard.nextLine();
- // Close scanner object
- keyboard.close();
- return fileName;
- }
- /**
- * Create array to use in implementation of a stack.
- *
- * @param fileName User specified filename
- * @throws IOException
- */
- public static String[] createBackingArray(String fileName) throws IOException {
- // Object to read text file
- Scanner readTextFile = null;
- // Array to temporarily hold tokenized lines
- // as they are read from a text file.
- String[] tokens = null;
- // Read text tile in and tokenize each line.
- try {
- readTextFile = new Scanner(new File(fileName));
- while (readTextFile.hasNext()) {
- tokens = readTextFile.nextLine().trim().split(",");
- }
- // Display message to indicate file successfully read
- //System.out.println("File read.");
- }
- // Throw exception and terminate if file not found etc.
- catch (IOException ex) {
- System.out.println("File not found. Program will terminate.");
- System.exit(0);
- }
- // Returns array of tokenized lines from text file
- return tokens;
- }
- /**
- * Checks if a string is one of these operators( ^, +, -, *, /)
- * @param str String from tokenized array.
- * @return True if the string matches one of the operators in the conditional statement.
- */
- public static boolean checkIfOperator(String str) {
- return(str.equals("^") || str.equals("+") || str.equals("-") || str.equals("*") || str.equals("/"));
- }
- /**
- * Performs calculations based on operator and numbers provided.
- * @param numOne
- * @param numTwo
- * @param operator
- * @return The result of the calculation
- */
- public static int calculator(int numOne, int numTwo, String operator, Stack<String> numStack) {
- //Store result
- int result = 0;
- switch(operator) {
- case "+":
- numStack.push(Integer.toString(result = numOne + numTwo));
- break;
- case "-":
- numStack.push(Integer.toString(result = numOne - numTwo));
- break;
- case "*":
- numStack.push(Integer.toString(result = numOne * numTwo));
- break;
- case "/":
- numStack.push(Integer.toString(result = numOne / numTwo));
- break;
- case "^":
- numStack.push(Integer.toString(result = numOne ^ numTwo));
- break;
- }
- return result;
- }
- /**
- * Performs calculations using reverse polish notation.
- * @param tokens Array of operators and operands
- * @return Value on top of given stack
- * @throws IOException
- */
- public static String rpnCalculator(String [] tokens) throws IOException{
- //Check if array is empty before proceeding
- //If array is empty get filename from user and create array
- if(tokens == null || tokens.length == 0) {
- System.out.println("Error. Data has not been stored.");
- createBackingArray(getFilename());
- }
- //Store values as strings in stack
- Stack<String> numStack = new Stack<String>();
- //Result of calculations
- String result = null;
- int operandTwo = 0;
- int operandOne = 0;
- //Holds operators
- String operator = null;
- //If value is an operator pop two values from stack and perform calculations
- for(int index = 0; index < tokens.length; index++) {
- //Push values to stack
- numStack.push(tokens[index]);
- }
- for(int index = 0; index < tokens.length; index++) {
- if(checkIfOperator(tokens[index])){
- //If an operator, pop from stack to use in calculation
- operator = numStack.pop();
- //Convert values to ints
- //operandOne is on the left of operator, operandTwo is on the right
- operandTwo = Integer.parseInt(numStack.pop());
- operandOne = Integer.parseInt(numStack.pop());
- //Convert to string
- result = Integer.toString(calculator(operandOne, operandTwo, operator, numStack));
- }
- //System.out.println(numStack.peek());
- }
- return numStack.pop();
- }
- public static void main(String[] args) throws IOException {
- //Holds values from text file in an array
- String [] valuesFromFile = createBackingArray(getFilename());
- //Displays results of calculations to user
- System.out.println(rpnCalculator(valuesFromFile));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement