/************************************************** * Author: Morrison * Date: 12 Apr 202021 **************************************************/ import javafx.application.Application; import javafx.application.Platform; import javafx.stage.Stage; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.control.TextField; import javafx.scene.layout.BorderPane; import javafx.scene.layout.GridPane; import java.util.function.DoubleBinaryOperator; public class RPN extends Application { private final TextField display; private final TextField stackShow; private NumberButton[] digits; private NumberButton decimalPoint; private NumberButton exponent; private Button clearButton; Button clearEntryButton; Button enterButton; IStack theStack; private OpButton plus; private OpButton times; public RPN() { display = new TextField(); stackShow = new TextField(); digits = new NumberButton[10]; for(int k = 0; k < 10; k++) { digits[k] = new NumberButton((char)(0x30 + k)); } decimalPoint = new NumberButton('.'); clearButton = new Button("C"); clearEntryButton = new Button("CE"); enterButton = new Button("ENTER"); exponent = new NumberButton('E'); theStack = new AStack<>(); plus = new OpButton( (x,y) -> x + y , "+"); times = new OpButton( (x,y) -> x*y, "*"); } @Override public void init() { } @Override public void start(Stage primary) { BorderPane bp = new BorderPane(); GridPane gp = new GridPane(); gp.add(digits[7], 0, 0); gp.add(digits[8], 1, 0); gp.add(digits[9], 2, 0); gp.add(digits[4], 0, 1); gp.add(digits[5], 1, 1); gp.add(digits[6], 2, 1); gp.add(digits[1], 0, 2); gp.add(digits[2], 1, 2); gp.add(digits[3], 2, 2); gp.add(decimalPoint, 0, 3); gp.add(digits[0], 1, 3); gp.add(exponent, 2, 3); gp.add(plus, 3, 0); gp.add(times, 3, 1); gp.add(enterButton, 0, 4); enterButton.setOnAction( e -> { //contenst of display go to the stack. //TODO: Handle end-user stupidity double contents = Double.parseDouble(display.getText()); //push on stack theStack.push(contents); stackShow.setText(theStack.toString()); }); bp.setTop(display); bp.setBottom(stackShow); bp.setCenter(gp); Scene scene = new Scene(bp, 700, 500); primary.setScene(scene); primary.show(); } @Override public void stop() { } /********************** Inner Classes ********************/ class NumberButton extends Button { private final char num; public NumberButton(char num) { super("" + num); this.num = num; setOnAction( e -> { display.setText(display.getText() + num); }); } } class OpButton extends Button { private DoubleBinaryOperator op; public OpButton(DoubleBinaryOperator op, String symbol) { super(symbol); this.op = op; setOnAction( e -> { double second = Double.parseDouble(display.getText()); double first = theStack.pop(); double result = op.applyAsDouble(first, second); theStack.push(result); display.setText("" + result); stackShow.setText(theStack.toString()); }); } } class FunctionButton extends Button { } }