在我跳入我的問題之前我想說明的是,實際上我並不主要需要編程幫助,我剛纔一直在想這個問題好幾天,所以也許這是不完全是一個基於編程的問題。(Java)基於GUI的計算器的算法
經過大約2年的編程缺席之後,我決定在前幾天提高我的知識水平。我爲一個計算器編寫了一個基於swing的GUI,並且能夠爲操作+,*創建一個工作計算器,但只能用於單位數字。你可以看到下面的代碼。
我現在的問題是,你們有沒有人知道一個算法,可以用來使用相同的圖形用戶界面,因爲我有數字與更多數字的方程?
編寫代碼不應該是一個問題,但不幸的是我無法找出有用的東西。
我希望爲創造性的解決方案,甚至,如果它僅僅是一個基本的想法,我會很高興地加入其進一步發展:)
最大
代碼:
Main.java:
public class Main {
public static void main(String[] args) {
new CalculatorController();
}
}
CalculatorController.java:
public class CalculatorController {
private CalculatorView view;
private NumberAction nmbAction;
private OperatorAction opAction;
private CalcObject currentValue, savedValue;
CalculatorController() {
currentValue = new CalcObject();
savedValue = new CalcObject();
nmbAction = new NumberAction(this);
opAction = new OperatorAction(this);
view = new CalculatorView(this);
view.setSize(250, 220);
view.setVisible(true);
}
public void printResult(double result) {
view.getCALCULATION().setText(String.valueOf(result));
}
public CalculatorView getView() {
return view;
}
public NumberAction getNmbAction() {
return nmbAction;
}
public OperatorAction getOpAction() {
return opAction;
}
public CalcObject getCurrentValue() {
return currentValue;
}
public CalcObject getSavedValue() {
return savedValue;
}
public void setCurrentValue(CalcObject currentValue) {
String str = currentValue.getOperator();
this.currentValue.setOperator(str);
this.currentValue.setNumber(currentValue.getNumber());
}
public void setSavedValue(CalcObject stripeValue) {
String str = stripeValue.getOperator();
this.savedValue.setOperator(str);
this.savedValue.setNumber(stripeValue.getNumber());
}
}
CalculatorView.java:
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;
public class CalculatorView extends JFrame {
private String equation = "";
private CalcObject object = new CalcObject();
private JPanel pane;
private JPanel contentPane;
private JTextField CALCULATION;
private JButton NMB_0;
private JButton NMB_1;
private JButton NMB_2;
private JButton NMB_3;
private JButton NMB_4;
private JButton NMB_5;
private JButton NMB_6;
private JButton NMB_7;
private JButton NMB_8;
private JButton NMB_9;
private JButton CALC;
private JButton MULTIPLY;
private JButton DIVIDE;
private JButton ADD;
private JButton SUBTRACT;
public CalculatorView(CalculatorController controller) {
setDefaultCloseOperation(CalculatorView.EXIT_ON_CLOSE);
pane = new JPanel(new FlowLayout());
setContentPane(pane);
CALCULATION = new JTextField();
CALCULATION.setColumns(16);
pane.add(CALCULATION);
contentPane = new JPanel(new GridLayout(0, 3));
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
pane.add(contentPane);
NMB_1 = new JButton(controller.getNmbAction());
NMB_1.setText("1");
contentPane.add(NMB_1);
NMB_2 = new JButton(controller.getNmbAction());
NMB_2.setText("2");
contentPane.add(NMB_2);
NMB_3 = new JButton(controller.getNmbAction());
NMB_3.setText("3");
contentPane.add(NMB_3);
NMB_4 = new JButton(controller.getNmbAction());
NMB_4.setText("4");
contentPane.add(NMB_4);
NMB_5 = new JButton(controller.getNmbAction());
NMB_5.setText("5");
contentPane.add(NMB_5);
NMB_6 = new JButton(controller.getNmbAction());
NMB_6.setText("6");
contentPane.add(NMB_6);
NMB_7 = new JButton(controller.getNmbAction());
NMB_7.setText("7");
contentPane.add(NMB_7);
NMB_8 = new JButton(controller.getNmbAction());
NMB_8.setText("8");
contentPane.add(NMB_8);
NMB_9 = new JButton(controller.getNmbAction());
NMB_9.setText("9");
contentPane.add(NMB_9);
MULTIPLY = new JButton(controller.getOpAction());
MULTIPLY.setText("*");
contentPane.add(MULTIPLY);
NMB_0 = new JButton(controller.getNmbAction());
NMB_0.setText("0");
contentPane.add(NMB_0);
DIVIDE = new JButton(controller.getOpAction());
DIVIDE.setText("/");
contentPane.add(DIVIDE);
DIVIDE.setEnabled(false);
ADD = new JButton(controller.getOpAction());
ADD.setText("+");
contentPane.add(ADD);
CALC = new JButton(controller.getOpAction());
CALC.setText("=");
contentPane.add(CALC);
SUBTRACT = new JButton(controller.getOpAction());
SUBTRACT.setText("-");
contentPane.add(SUBTRACT);
SUBTRACT.setEnabled(false);
}
public String getEquation() { return equation; }
public void setEquation(String equation) { this.equation = equation; }
public JTextField getCALCULATION() {
return CALCULATION;
}
}
OperatorAction.java:
import javax.swing.*;
import java.awt.event.ActionEvent;
public class OperatorAction extends AbstractAction {
private String operator;
private CalculatorController controller;
OperatorAction(CalculatorController controller) {
this.controller = controller;
this.operator = "";
}
public void actionPerformed(ActionEvent e) {
JButton button = (JButton)e.getSource();
this.operator = button.getText();
controller.getView().setEquation(controller.getView().getEquation().concat(this.operator));
controller.getView().getCALCULATION().setText(controller.getView().getEquation());
controller.getCurrentValue().setOperator(operator);
if (controller.getCurrentValue().getOperator() == "=") {
if (controller.getSavedValue().getOperator() == "+") {
controller.printResult(Double.parseDouble(controller.getSavedValue().getNumber()) + Double.parseDouble(controller.getCurrentValue().getNumber()));
} else {
controller.printResult(Double.parseDouble(controller.getCurrentValue().getNumber()));
}
controller.getView().setEquation("");
controller.getCurrentValue().setNumber("0");
controller.getSavedValue().setNumber("0");
controller.getCurrentValue().setOperator("");
controller.getSavedValue().setOperator("");
}
}
}
NumberAction.java:
import javax.swing.*;
import java.awt.event.ActionEvent;
public class NumberAction extends AbstractAction {
private String number;
private CalculatorController controller;
NumberAction(CalculatorController controller) {
this.controller = controller;
this.number = "";
}
public void actionPerformed(ActionEvent e) {
JButton button = (JButton)e.getSource();
this.number = button.getText();
controller.getView().setEquation(controller.getView().getEquation().concat(this.number));
controller.getView().getCALCULATION().setText(controller.getView().getEquation());
if (controller.getSavedValue().getOperator() == "") {
if (controller.getCurrentValue().getOperator() == "") {
controller.getCurrentValue().setNumber(number);
}
if (controller.getCurrentValue().getOperator() == "*") {
controller.getCurrentValue().setNumber(String.valueOf(Double.parseDouble( controller.getCurrentValue().getNumber()) * Double.parseDouble(number)));
controller.setSavedValue(controller.getCurrentValue());
}
if (controller.getCurrentValue().getOperator() == "+") {
controller.setSavedValue(controller.getCurrentValue());
controller.getCurrentValue().setNumber(number);
}
} else {
if (controller.getCurrentValue().getOperator() == "*") {
controller.getCurrentValue().setNumber(String.valueOf(Double.parseDouble( controller.getCurrentValue().getNumber()) * Double.parseDouble(number)));
}
if (controller.getCurrentValue().getOperator() == "+") {
controller.getCurrentValue().setNumber(String.valueOf(Double.parseDouble(controller.getSavedValue().getNumber()) + Double.parseDouble(controller.getCurrentValue().getNumber()) + Double.parseDouble(number)));
controller.getSavedValue().setOperator("");
}
}
}
}
CalcObject.java:
public class CalcObject {
private String NUMBER;
private String OPERATOR;
CalcObject() {
NUMBER = "0";
OPERATOR = "";
}
CalcObject(String number, String operator) {
NUMBER = number;
OPERATOR = operator;
}
public void setNumber(String number) { NUMBER = number; }
public void setOperator(String operator) { OPERATOR = operator; }
public String getNumber() { return NUMBER; }
public String getOperator() { return OPERATOR; }
}
那麼,再次檢查程序時,我發現,單個數字的這個算法既不能正常工作,但我的問題仍然是一樣的。我也會對一些意見或想法感到高興,以使它更容易,更流暢或以更好的方式。
至於多位數字:如果您在啓動程序時設置當前號碼爲空字符串或輸入操作,然後點擊一個數字按鈕時應該只是成爲將所選擇的數字爲當前數量的問題(添加另外的數字後,這是一個字符串,直到你需要它進行計算)。 – Thomas
順便說一下,我會說你發佈的代碼對於SO來說有點太多了,而且它的大部分內容都與這個問題無關。關於要發佈什麼樣的代碼,請在[ask]和[mcve]上有一個掠奪。 – Thomas
另請參閱此[計算器示例](http://stackoverflow.com/a/7441804/418556)。它使用'ScriptEngine'來評估文本字段中的表達式。 –