Write a Java program which will input sequences of characters representing infix expressions involving the binary...
Fantastic news! We've Found the answer you've been seeking!
Question:
Transcribed Image Text:
Write a Java program which will input sequences of characters representing infix expressions involving the binary (dyadic) operators +, -, *, and plus parentheses and single digit operands. The program should output the equivalent postfix expressions AND the values of those expressions. For example: INPUT: 6+9* (5* (3+4)) OUTPUT: 6 9 5 3 4 + += 321 As discussed in class, Dijkstra's Shunting Algorithm can be employed to convert an infix expression into an equivalent postfix expression using a stack for operators. Furthermore, a postfix expression can be evaluated using a stack for operands. The logical design provided on the next page combines these two algorithms into one procedure. The procedure should be performed for each input expression. The Shunting Algorithm requires the use of two value-returning methods: input Priority and stackPriority. input Priority (with token as its actual parameter) returns the priority value of an operator when it is on the input string. stack Priority (with top element of operator stack as actual parameter) returns the priority value of an operator on the stack. Use the following for implementation of these two methods: OPERATOR INPUT PRIORITY + * NA 2 2 3 3 4 STACK PRIORITY 0 2 2 3 3 1 ( Since the algorithm requires two stacks (each of which comprised of different element types), an interface (i.e., general specification) for a stack abstract data type should be defined. The implementation of the operator stack class used in your program should reference data elements of type character; similarly, the implementation of the operand stack class should reference data elements of integer type. Both stack classes should be implemented using the reference-based linked model. Be sure to follow the techniques of good programming style and use extensive comments to provide for internal documentation of your source program, including author, date, purpose (s), pre-condition (s), and post-condition (s) for all methods. For evaluation of this lab assignment, you are required to separately provide your source program (.java) file(s) as well as your input file and output file (or screenshot of output) via Canvas submission. Please submit these deliverables on or before the assignment due date. class containing static methods: main() ... pseudocode provided, inputPriority(), stackPriority() interface for stacks: StackInterface (contains declarations for stack methods but no code therein... also no data members) class for operator stack (implements StackInterface): contains top data member (reference to char-type node) and full definitions of all methods declared in interface class for operand stack (implements StackInterface): contains top data member (reference to int-type node) and full definitions of all methods declared in interface class for char-type nodes: contains char data member (operator) and reference data member next to next char-type node - also standard node operations: constructor(s), gets, and sets class for int-type nodes: contains int data member (operand) and reference data member next to next int-type node - also standard node operations: constructor(s), gets, and sets Initialize the operator stack to contain a ';' (bottom of stack operator) Get the first token while the end of the expression is not reached if the token is an operand then Print the token followed by a space push the operand onto the operand stack else if the token is a ')' then else peek while the top of the operator stack is not equal to '(' pop the operator stack Print the operator followed by a space pop the operand stack twice Apply the designated operation to the two operands push the operation result onto the operand stack end while pop the '(' and discard it peek while inputPriority (token) stackPriority (top of operator stack) pop the operator stack Print the operator followed by a space pop the operand stack twice Apply the designated operation to the two operands push the operation result onto the operand stack end while push the token onto the operator stack Get the next token end while peek while the top of the operator stack is not equal to ';' pop the operator stack Print the operator followed by a space pop the operand stack twice Apply the designated operation to the two operands push the operation result onto the operand stack end while pop the operand stack, print an "=" sign followed by a space, and then print the result 5 = 5 3 5 - 5 3 4 = 2 + 73 * 56 = 35 * * +3 = 51 3 / + +6+ = 29 = 1 2 5 7 8 + 5 = . 5 3 4 5 * 9453 - * 1 2 3 * 4 * = 27 567 * : = 5040 = 12 2 48 34 + = 9 1 | - ||2 + 25 242 + 5 3 = 7 3 5 * * 19 6 - - * 3 = 42* = 3 9 7 1 15 * - 6 9 5 3 4 + * - 12 = 3 - 56 = 43 * 2 / 5 + = 8 + 12+ 3+ 4+ 5+ = 321 = 15 9 3/8 2 / + = 7 Output, +x+ 5 5-3 5* (3+4) 7*3+5*6 2+5* (7+8)/3 (5) 3+4*5+6 9-4 (5-3) 1*2*3*4*5*6*7 (2+4)* (8-6) 3+4 9-1-2-3 2*(((4+2))) (((((5))))) 3*7-4*2+5*6 3*5 (3* (9-7*1))/2+5 6+9 (5* (3+4)) 1+2+3+4+5 9/3+8/2 In Put.txt Write a Java program which will input sequences of characters representing infix expressions involving the binary (dyadic) operators +, -, *, and plus parentheses and single digit operands. The program should output the equivalent postfix expressions AND the values of those expressions. For example: INPUT: 6+9* (5* (3+4)) OUTPUT: 6 9 5 3 4 + += 321 As discussed in class, Dijkstra's Shunting Algorithm can be employed to convert an infix expression into an equivalent postfix expression using a stack for operators. Furthermore, a postfix expression can be evaluated using a stack for operands. The logical design provided on the next page combines these two algorithms into one procedure. The procedure should be performed for each input expression. The Shunting Algorithm requires the use of two value-returning methods: input Priority and stackPriority. input Priority (with token as its actual parameter) returns the priority value of an operator when it is on the input string. stack Priority (with top element of operator stack as actual parameter) returns the priority value of an operator on the stack. Use the following for implementation of these two methods: OPERATOR INPUT PRIORITY + * NA 2 2 3 3 4 STACK PRIORITY 0 2 2 3 3 1 ( Since the algorithm requires two stacks (each of which comprised of different element types), an interface (i.e., general specification) for a stack abstract data type should be defined. The implementation of the operator stack class used in your program should reference data elements of type character; similarly, the implementation of the operand stack class should reference data elements of integer type. Both stack classes should be implemented using the reference-based linked model. Be sure to follow the techniques of good programming style and use extensive comments to provide for internal documentation of your source program, including author, date, purpose (s), pre-condition (s), and post-condition (s) for all methods. For evaluation of this lab assignment, you are required to separately provide your source program (.java) file(s) as well as your input file and output file (or screenshot of output) via Canvas submission. Please submit these deliverables on or before the assignment due date. class containing static methods: main() ... pseudocode provided, inputPriority(), stackPriority() interface for stacks: StackInterface (contains declarations for stack methods but no code therein... also no data members) class for operator stack (implements StackInterface): contains top data member (reference to char-type node) and full definitions of all methods declared in interface class for operand stack (implements StackInterface): contains top data member (reference to int-type node) and full definitions of all methods declared in interface class for char-type nodes: contains char data member (operator) and reference data member next to next char-type node - also standard node operations: constructor(s), gets, and sets class for int-type nodes: contains int data member (operand) and reference data member next to next int-type node - also standard node operations: constructor(s), gets, and sets Initialize the operator stack to contain a ';' (bottom of stack operator) Get the first token while the end of the expression is not reached if the token is an operand then Print the token followed by a space push the operand onto the operand stack else if the token is a ')' then else peek while the top of the operator stack is not equal to '(' pop the operator stack Print the operator followed by a space pop the operand stack twice Apply the designated operation to the two operands push the operation result onto the operand stack end while pop the '(' and discard it peek while inputPriority (token) stackPriority (top of operator stack) pop the operator stack Print the operator followed by a space pop the operand stack twice Apply the designated operation to the two operands push the operation result onto the operand stack end while push the token onto the operator stack Get the next token end while peek while the top of the operator stack is not equal to ';' pop the operator stack Print the operator followed by a space pop the operand stack twice Apply the designated operation to the two operands push the operation result onto the operand stack end while pop the operand stack, print an "=" sign followed by a space, and then print the result 5 = 5 3 5 - 5 3 4 = 2 + 73 * 56 = 35 * * +3 = 51 3 / + +6+ = 29 = 1 2 5 7 8 + 5 = . 5 3 4 5 * 9453 - * 1 2 3 * 4 * = 27 567 * : = 5040 = 12 2 48 34 + = 9 1 | - ||2 + 25 242 + 5 3 = 7 3 5 * * 19 6 - - * 3 = 42* = 3 9 7 1 15 * - 6 9 5 3 4 + * - 12 = 3 - 56 = 43 * 2 / 5 + = 8 + 12+ 3+ 4+ 5+ = 321 = 15 9 3/8 2 / + = 7 Output, +x+ 5 5-3 5* (3+4) 7*3+5*6 2+5* (7+8)/3 (5) 3+4*5+6 9-4 (5-3) 1*2*3*4*5*6*7 (2+4)* (8-6) 3+4 9-1-2-3 2*(((4+2))) (((((5))))) 3*7-4*2+5*6 3*5 (3* (9-7*1))/2+5 6+9 (5* (3+4)) 1+2+3+4+5 9/3+8/2 In Put.txt
Expert Answer:
Related Book For
Java How To Program Late Objects Version
ISBN: 9780136123712
8th Edition
Authors: Paul Deitel, Deitel & Associates
Posted Date:
Students also viewed these programming questions
-
Design a Java class that represents a cache with a fixed size. It should support operations like add, retrieve, and remove, and it should evict the least recently used item when it reaches capacity.
-
"internet radios" for streaming audio, and personal video recorders and players. Describe design and evaluation processes that could be used by a start-up company to improve the usability of such...
-
March 31, 2014, adjusted trial balance for Brenner Climbing Adventures has been alphabetized as follows: Required Journalize the closing entries. No. Account Debit Credit $ 2,600 168 Accumulated...
-
Paulson Auto Components has an inventory of 490 obsolete remote entry keys that are carried in inventory at a manufacturing cost of $79,870. Production Supervisor Ricky Lewis must decide to do one of...
-
In problem, form a polynomial function whose real zeros and degree are given. Zeros: -1, multiplicity 1; 3, multiplicity 2; degree 3
-
On June 8, 2017, Eugene Weiner made a post on Isaac Aflalos Facebook page. The post read, Yurim and Isaac took advantage of a old 94plus sick man elder abuse [sic]. Alflalo took umbrage to the post...
-
Multiple Choice. Choose the best answer. 1. Which of the following is riot a fiduciary fund? a. Permanent fund. b. Agency fund. c. Investment trust fund. d. Pension trust fund. 2. Which of the...
-
(a) Use the method of direct Integration to find the solution of x2. (b) (i) dy -4x=6. (5%) dx Verify that y = ex, y = e3x and y = Aex + Be -3x (A and B are constants) are - 6y= 0. (10 %) solutions...
-
You are the new Vice President of Sales for Penske Motors, one of America's leading automotive dealership groups that operates over 100 individual dealerships across the country. While you are an...
-
Which concepts or definitions of an accounting system are likely to be the hardest to classify? Why?
-
How does the IFRS Foundation establish the independence of the IASB in setting accounting standards?
-
What are the benefits and limitations of using computer-based analysis of words in comparing narrative reports?
-
What are the features of mutual support that are common across the different regional groups of accountancy professions?
-
How does IESBA support the development of high-quality ethical standards worldwide?
-
Suppose that the marginal cost of a one-way airfare is $30. A. If the airline practices perfect price discrimination, how many customers will purchase one-way airfare? How much producer surplus is...
-
What is the difference between direct materials and indirect materials?
-
Phishing is a form of identity theft in which, in an e-mail, a sender posing as a trustworthy source attempts to acquire private information, such as your user names, passwords, credit-card numbers...
-
Modify Fig.7.13 to deal a five-card poker hand. Then modify class DeckOfCards of Fig.7.12 to include methods that determine whether a hand contains a) A pair b) Two pairs c) Three of a kind (e.g.,...
-
Modify Exercise 4.15 to combine your code from the four separate triangles of asterisks such that all four patterns print side by side. Make clever use of nested for loops. Exercise 4.15 Write an...
-
Portugal has a progressive personal income tax system. In 2016, tax rates on taxable income were \(14.5 \%\) on the first \( 7,035,21 \%\) on the next \( 13,065,37 \%\) on the next \( 20,100,45 \%\)...
-
In its 2016 International Tax Competitiveness Index report, the U.S.-based Tax Foundation ranked Estonia as having the most competitive tax system in the OECD, based in part on its \(20 \%\) flat tax...
-
A lump-sum tax is a fixed amount of tax per person. If a lump-sum tax, \(T\), raises the same amount of revenue for the government as a tax on earnings at the rate, \(t\), then \(t w H=T\), where...
Study smarter with the SolutionInn App