Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I'm attempting to make a calculator program that acts like a more advanced one, in that after answering an equation, if a new one is

I'm attempting to make a calculator program that acts like a more advanced one, in that after answering an equation, if a new one is started it carries over the previous answer. If someone cold help me complete the following code, that'd be great

For the processPostfix() function, it calculates the final result according to the input postfix queue. Here you need to create a stack variable of MyStack to store all the operands. As we dequeue an item from the postfix queue, we check if it is an operand or operator (i.e., +, -, or *). If it is an operand, push it onto an operand-stack. If it is an operator (e.g., *), pop two items from the operand-stack, do the calculation, and then push the result onto the operandstack. See below for illustration. postfixqueue: 5, 2, 3, *, +, 6, , 18, + 5 After three dequeue(), operand-stack contain: 5, 2, 3 (left is bottom and right means top).

 /*Convert an input infix queue into A postfix queue, which is the output of the function */ private MyQueue infix2postfix(MyQueue infix_queue) { //Declare queue to store postfix MyQueue postfix_queue = new MyQueue(); //store the conversion result from infix_queue //Go through the infix_queue and convert it to a postfix_queue MyStack operator_stack = new MyStack(); while(!infix_queue.isEmpty()) { String current = (String) infix_queue.dequeue(); //get the top token //if the current token is an operator if(current.equals("+") || current.equals("*") || current.equals("-")) { if(operator_stack.isEmpty()) { operator_stack.pushNode(new StackNode(current)); } else { String top = (String)operator_stack.getTopandPop().getData(); if(greaterThan(current, top)) //if the current operator has higher priority { operator_stack.pushNode(new StackNode(top)); //push the just popped top node back to the stack operator_stack.pushNode(new StackNode(current)); } else { //Pop the stack until you find a symbol of lower priority number than the current one while(!greaterThan(current, top)) { //add the top to the queue postfix_queue.enqueue((String)top); StackNode top_node = operator_stack.getTopandPop(); //keep popping if(top_node == null) //if the stack is empty, stop popping the queue { top = null; operator_stack.pushNode(new StackNode(current)); break; } else top = top_node.getData(); } if(top != null) //the last popped element has lower priority, so push it back { operator_stack.pushNode(new StackNode(top)); //push the just popped top node back to the stack operator_stack.pushNode(new StackNode(current)); } } } } else //if the current token is an operand { postfix_queue.enqueue((String)current); } } // Check operator_stack if it is not empty, put them into while (!operator_stack.isEmpty()) { StackNode node = operator_stack.getTopandPop(); postfix_queue.enqueue(node.getData()); } //Debug: see what are stored in the postfix_stack MyQueue copy_postfix_queue = new MyQueue(); while(!postfix_queue.isEmpty()) { String token = (String) postfix_queue.dequeue(); System.out.println(token); copy_postfix_queue.enqueue(token); } postfix_queue = copy_postfix_queue; return postfix_queue; } /*A priority comparison function between two operators opt1 and opt2*/ private boolean greaterThan(String opt1, String opt2) { if(opt1.equals("*") && !opt2.equals("*")) { return true; } else { return false; } } /* Process the postfix expression to compute the final value */ private String processPostfix(MyQueue postfix) { //Implementation here... //Below is just a start, you need to fill the values for final_value String final_value = ""; return final_value; } 

Afer 4th dequeue(), we get *. So pop two operands from the operand-stack 3 and 2. The new value is 6. Push 6 onto the operand-stack. Now the operand-stack has: 5, 6 (top). After 5th dequeue(), we get +. Pop two items from stack, 6, 5. Do the calculation. The new value is 11. Push 11 onto the operand-stack. Now the operand-stack contains: 11 Keep doing this while the postfix queue is NOT empty. To get the final result, simply pop it from the operand-stack.

 

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

Intelligent Information And Database Systems Asian Conference Aciids 2012 Kaohsiung Taiwan March 19 21 2012 Proceedings Part 3 Lnai 7198

Authors: Jeng-Shyang Pan ,Shyi-Ming Chen ,Ngoc-Thanh Nguyen

2012th Edition

3642284922, 978-3642284922

More Books

Students also viewed these Databases questions