Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

1. Modify the following program so that expressions will have two aditional operators: - (subtraction) and / (integer division, so 9/4 is 2 and 100/8

1. Modify the following program so that expressions will have two aditional operators: - (subtraction) and / (integer division, so 9/4 is 2 and 100/8 is 12 and 26/9 is 2). Of course, the first has the same operator precedence as + and the second has the same precedence as * and all of these binary operators are left-associative, namely, A+B+C is just like (A+B)+C. 2. Design and include a method to evaluate a postfix expression and return its int value. For example, if A=100, B=20, and C=30, "ABC*+A+" should return 800. 3. Create an input file to contain the following four input cases to be used by your program: (Note that the first three integers in each input line show the integer value of A, B and C, respectively) 20 200 30 (A+B)*C*A*B 20 200 30 (A+B)*C*A*(B-A-C) 50 200 40 A+(C*B-A-B) 50 200 40 A+(C*B-A-B)/(B/A) 4. Print out the following for each of the four input cases: a. input infix expression b. equivalent postfix expression your program computes. c. the value of the resulting postfix expression which, of course, must be the same as the value of the original input expression.

public class MyStack { // private instance variables: private static int MAX = 100; private static int opdCount = 3; // count of possible operands private int opdValues[] = new int [opdCount]; private int top = -1; // topmost position of Stack store[] private int top2 = -1; // topmost position of Stack postInt[] private int iSize = 0; // size of an input infix expression. private int pSize = 0; // postfix expression size private char store[] = new char[MAX]; // Stack storage of operands appearing in an input infix // expression during conversion process private char infix[] = new char[MAX]; // input infix exp. private char postfix[] = new char[MAX]; // output postfix exp private int postInt [] = new int [MAX]; // Stack storage of integer values of operands appearing // in a postfix expression during evaluation process. ///////////////////////////////// // public methods follow: public MyStack(Scanner inputStream) { // This one-argument constructor just calls input(inputStream) // in creating a stack object by taking the input // infix expression input(inputStream); } // end of one-argument constructor ////////////////////////////////////////////////////////////// public void input(Scanner inputStream) /* This method will be to 1. read three int values for A (or a), B (or b) and C (or c) and a string for an infix expression. 2. store the input String into infix[] as an array of chars and 3. set iSize to the length of input string. String inputt = JOptionPane.showInputDialog ("Enter an infix expression < 60"); */ { try { if (inputStream.hasNext()) { opdValues [0] = inputStream.nextInt(); opdValues [1] = inputStream.nextInt(); opdValues [2] = inputStream.nextInt(); String inputt = inputStream.next(); iSize = inputt.length(); if (iSize > MAX) { System.err.println ("ERROR:INPUT EXPRESSION TOO LONG: PROGRAM ABORTED."); System.exit (0); } inputt.getChars (0, iSize, infix, 0); /* Note that in the above method getChars() which converts a substring (proper or not) of the calling String, inputt in our case, into an array of characters, infix, in this case, the first two arguments are to specify the substring to be converted in terms of indexes of the calling string as follows: 1. first argument is the starting index of the calling string, inclusive 2. second argument is the ending index of the calling string, not inclusive. 3. third argument is the name of the array of characters being created. 4. last argument represents the starting index, subcript, of the array being constructed **/ } // end of hasNext() else throw new NoSuchElementException(); } // end of try block catch (NoSuchElementException ex) { System.err.println ("ERROR: END OF INPUT FILE REACHED PREMATURELY: PROGRAM ABORTED."); System.exit (0); } // end of catch block } // end of input() /////////////////////////////////////////////////// public void convert() // This method is the primary method for performing the conversion which amounts to // essentially defining the two instance variables, postfix[] and pSize. { initialize(); infix[iSize++] = ')'; pSize=0; char current; for (int index = 0; index < iSize; index++) { current = infix[index]; if (Character.isLetter(current)) insertPost(current); else // the current char is an operator including parentheses. { while (sPrio(store[top]) > iPrio(current)) insertPost (pop()); if (sPrio(store[top]) < iPrio(current)) push (current); else // they are the same and they are matching pair of parentheses pop(); } } // end of for loop infix [--iSize] = ' '; // to restore the original contents of infix[] } // end of convert() //////////////////////////////////////////////////////////////////////////// public int evaluate () /* This added method evaluates the postfix expression which convert() method calculates and stores in the instance variable, postfix[], an array of characters, for further processing. **/ { for (int index = 0; index <= pSize-1; index++) // loop to process each charater of postfix exp. { char ch = postfix [index]; switch (ch) {

case '+': top2--; postInt [top2] = postInt[top2] + postInt[top2+1]; break;

case '-': top2--; postInt [top2] = postInt[top2] - postInt[top2+1]; break;

case '*': top2--; postInt [top2] = postInt[top2] * postInt[top2+1]; break;

case '/': top2--; postInt [top2] = postInt[top2] / postInt[top2+1]; break;

case 'A': case 'a': postInt [++top2] = opdValues [0]; break;

case 'B': case 'b': postInt [++top2] = opdValues [1]; break;

case 'C': case 'c': postInt [++top2] = opdValues [2]; break;

default: { System.err.println("ERROR:::ILLEGAL SYMBOL IN POSTFIX EXPRESSION: PROGRAM ABORTED"); System.exit (0); } } // end of switch } // end of for loop to evaluate the entire postfix expression if (top2 != 0) // then something gone wrong with evaluation process. { System.err.println ("ERROR:TOO MANY SYMBOLS IN THE POSTFIX EXPRESSION: PROGRAM ABORTED."); System.exit (0); } else return postInt [0]; // otherwise Stack postInt[] has just one element left // which is the ultimate final evaluation value that should be returned. } // end of evaluate()

////////////////////////////////////////////////////////////////////////////// public static void main (String args[]) { int inputCount = 0; // count of input cases successfully read and processed int expValue = 0; // evaluated int value of an expression String output = ""; MyStack stack; Scanner inputStream = null; PrintWriter outputStream = null; try { inputStream = new Scanner (new FileInputStream ("input.txt")); outputStream = new PrintWriter (new FileOutputStream ("output.txt")); } // end of try block catch (FileNotFoundException ex) { System.err.println ("ERROR: FILE NOT FOUND OR CANNOT OPEN: PROGRAM ABORTED."); System.exit (0); } // end of catch block while (inputStream.hasNext()) { inputCount++; output = ""; stack = new MyStack(inputStream); // Above constructor will read one input case in initializing some instance variables // including infix[], iSize and opdValues[]. stack.convert(); output += " INPUT CASE:::" + inputCount; output += " \tINFIX EXPRESSION:::"; output += String.valueOf (stack.infix, 0, stack.iSize); output += " \tPOSTFIX EXPRESSION:"; output += String.valueOf (stack.postfix, 0, stack.pSize); // Note above is the exactly same as the following: // output += String.valueOf (stack.postfix); expValue = stack.evaluate(); output += " \tEVALUATED VALUE::::" + expValue; outputStream.println (output); } // end of for loop to process all input cases // time to finish outputStream.println (" THIS PROGRAM ENDS IN SUCCESS AFTER PROCESSING " + inputCount + " INPUT CASES."); outputStream.close(); inputStream.close(); System.exit(0); } } // end of class MyStack

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Professional Visual Basic 6 Databases

Authors: Charles Williams

1st Edition

1861002025, 978-1861002020

More Books

Students also viewed these Databases questions

Question

Describe Table Structures in RDMSs.

Answered: 1 week ago