i'm aware there better ways make id happy long worked, whenever operation 4+4 correct if 4+4*2 equal 32
package calc; import java.awt.flowlayout; import java.awt.event.actionevent; import java.awt.event.actionlistener; import javax.swing.jbutton; import javax.swing.jframe; import javax.swing.jtextfield; /** * * @author ben */ public class gui extends jframe { int response, operator; double num1, num2, total = 0; string operation, answer, num, testnum; private jbutton one, two, three, four, five, six, seven, eight, nine, zero, multiply, divide, subtract, add, equals, clear; private jtextfield display, fakedisplay; public gui() { super("calculator"); setlayout(new flowlayout()); fakedisplay = new jtextfield(10); display = new jtextfield(10);; add(display); 1 = new jbutton("1"); add(one); 2 = new jbutton("2"); add(two); 3 = new jbutton("3"); add(three); 4 = new jbutton("4"); add(four); 5 = new jbutton("5"); add(five); 6 = new jbutton("6"); add(six); 7 = new jbutton("7"); add(seven); 8 = new jbutton("8"); add(eight); 9 = new jbutton("9"); add(nine); 0 = new jbutton("0"); add(zero); multiply = new jbutton("*"); add(multiply); divide = new jbutton("/"); add(divide); subtract = new jbutton("-"); add(subtract); add = new jbutton("+"); add(add); equals = new jbutton("="); add(equals); clear = new jbutton("clear"); add(clear); handler handle = new handler(); one.addactionlistener(handle); two.addactionlistener(handle); three.addactionlistener(handle); four.addactionlistener(handle); five.addactionlistener(handle); six.addactionlistener(handle); seven.addactionlistener(handle); eight.addactionlistener(handle); nine.addactionlistener(handle); zero.addactionlistener(handle); multiply.addactionlistener(handle); divide.addactionlistener(handle); subtract.addactionlistener(handle); add.addactionlistener(handle); equals.addactionlistener(handle); clear.addactionlistener(handle); } private class handler implements actionlistener { @ override public void actionperformed(actionevent e) { if(e.getsource() == one) { response = 1; display.settext(display.gettext() + 1); fakedisplay.settext(fakedisplay.gettext() + 1); } else if(e.getsource() == two) { response = 2; display.settext(display.gettext() + 2); fakedisplay.settext(fakedisplay.gettext() + 2); } else if(e.getsource() == three) { response = 3; display.settext(display.gettext() + 3); fakedisplay.settext(fakedisplay.gettext() + 3); } else if(e.getsource() == four) { response = 4; display.settext(display.gettext() + 4); fakedisplay.settext(fakedisplay.gettext() + 4); } else if(e.getsource() == five) { response = 5; display.settext(display.gettext() + 5); fakedisplay.settext(fakedisplay.gettext() + 5); } else if(e.getsource() == six) { response = 6; display.settext(display.gettext() + 6); fakedisplay.settext(fakedisplay.gettext() + 6); } else if(e.getsource() == seven) { response = 7; display.settext(display.gettext() + 7); fakedisplay.settext(fakedisplay.gettext() + 7); } else if(e.getsource() == eight) { response = 8; display.settext(display.gettext() + 8); fakedisplay.settext(fakedisplay.gettext() + 8); } else if(e.getsource() == nine) { response = 9; display.settext(display.gettext() + 9); fakedisplay.settext(fakedisplay.gettext() + 9); } else if(e.getsource() == zero) { response = 0; display.settext(display.gettext() + 0); fakedisplay.settext(fakedisplay.gettext() + 0); } else if(e.getsource() == multiply) { if(operator == 0 && num1 == 0) { num1 = double.parsedouble(display.gettext()); operator = 3; fakedisplay.settext(""); } else if(operator != 0) { operator = 3; num2 = double.parsedouble(fakedisplay.gettext()); fakedisplay.settext(""); if(operator == 1) { total = num1 + num2; display.settext(display.gettext() + operation + total); num1 = total; } if(operator == 2) { total = num1 - num2; display.settext(display.gettext() + operation + total); num1 = total; } if(operator == 3) { total = num1 * num2; display.settext(display.gettext() + operation + total); num1 = total; } if(operator == 4) { total = num1 / num2 + num1 % num2; display.settext(display.gettext() + operation + total); num1 = total; } num2 = 0; total = 0; fakedisplay.settext(""); operator = 0; } operation = "*"; display.settext(display.gettext() + operation); } else if(e.getsource() == divide) { if(operator == 0 && num1 == 0) { num1 = double.parsedouble(display.gettext()); operator = 4; fakedisplay.settext(""); } else if(total == 0) { operator = 3; } else if(operator != 0) { operator = 4; num2 = double.parsedouble(fakedisplay.gettext()); fakedisplay.settext(""); if(operator == 1) { total = num1 + num2; display.settext(display.gettext() + operation + total); num1 = total; } if(operator == 2) { total = num1 - num2; display.settext(display.gettext() + operation + total); num1 = total; } if(operator == 3) { total = num1 * num2; display.settext(display.gettext() + operation + total); num1 = total; } if(operator == 4) { total = num1 / num2 + num1 % num2; display.settext(display.gettext() + operation + total); num1 = total; } num2 = 0; total = 0; fakedisplay.settext(""); operator = 0; } operation = "/"; display.settext(display.gettext() + operation); } else if(e.getsource() == add) { if(operator == 0 && num1 == 0) { num1 = double.parsedouble(display.gettext()); operator = 1; fakedisplay.settext(""); } else if(operator != 0) { operator = 1; num2 = double.parsedouble(fakedisplay.gettext()); fakedisplay.settext(""); if(operator == 1) { total = num1 + num2; display.settext(display.gettext() + operation + total); num1 = total; } if(operator == 2) { total = num1 - num2; display.settext(display.gettext() + operation + total); num1 = total; } if(operator == 3) { total = num1 * num2; display.settext(display.gettext() + operation + total); num1 = total; } if(operator == 4) { total = num1 / num2 + num1 % num2; display.settext(display.gettext() + operation + total); num1 = total; } num2 = 0; total = 0; fakedisplay.settext(""); operator = 0; } operation = "+"; display.settext(display.gettext() + operation); } else if(e.getsource() == subtract) { if(operator == 0 && num1 == 0) { num1 = double.parsedouble(display.gettext()); operator = 2; fakedisplay.settext(""); } else if(operator != 0) { operator = 2; num2 = double.parsedouble(fakedisplay.gettext()); fakedisplay.settext(""); if(operator == 1) { total = num1 + num2; display.settext(display.gettext() + operation + total); num1 = total; } if(operator == 2) { total = num1 - num2; display.settext(display.gettext() + operation + total); num1 = total; } if(operator == 3) { total = num1 * num2; display.settext(display.gettext() + operation + total); num1 = total; } if(operator == 4) { total = num1 / num2 + num1 % num2; display.settext(display.gettext() + operation + total); num1 = total; } num2 = 0; total = 0; fakedisplay.settext(""); operator = 0; } operation = "-"; display.settext(display.gettext() + operation); } else if(e.getsource() == equals) { if(operator == 0) { display.settext("error"); } else if(operator != 0) { num2 = double.parsedouble(fakedisplay.gettext()); fakedisplay.settext(""); if(operator == 1) { total = num1 + num2; display.settext(display.gettext() + "=" + total); } if(operator == 2) { total = num1 - num2; display.settext(display.gettext() + "=" + total); } if(operator == 3) { total = num1 * num2; display.settext(display.gettext() + "=" + total); } if(operator == 4) { total = num1 / num2 + num1 % num2; display.settext(display.gettext() + "=" + total); } } operation = "="; } else if(e.getsource() == clear) { display.settext(""); fakedisplay.settext(""); operator = 0; total = 0; num1 = 0; num2 = 0; } system.out.println("num1: " + num1 + " num2: " + num2 + "\ndisplay: " + display.gettext() + " fakedisplay: " + fakedisplay.gettext() + "\nresponse: " + response + "\noperator: " + operator + "\ntotal: " + total + "\n=========================="); } } }
i can guide online information, need work on parsing. put each token(+-*/) of final command string in list(maybe binary tree works) , sort list tell calculator division , multiplication has highest priority in mean time addition , subtraction has lower priority.
you sort command array in such way there priority of leftside against rightside. starts calculating left right while selecting div/mult first.
http://www.slideshare.net/dabeaz/writing-parsers-and-compilers-with-ply
http://parsingintro.sourceforge.net/
http://www.dreamincode.net/forums/topic/268945-an-introduction-to-compiler-design-part-ii-parsing/
https://en.wikipedia.org/wiki/parsing
http://arantxa.ii.uam.es/~modonnel/compilers/03_1_parsing_intro.pdf
if tired, can use script engine of java, suggestion user "andrew thompson".
Comments
Post a Comment