gpt4 book ai didi

java - 我怎样才能解决一个简单计算器的字符串版本的计算?

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:42:42 25 4
gpt4 key购买 nike

<分区>

这是一个简单的计算器,用户可以在其中输入计算结果并按 enter,计算器将确定它是否是有效计算。如果是有效计算,则进行计算。否则,错误消息将写入屏幕。

计算已执行部分未完成。

有人可以针对 getAnswer() 方法提出解决方案吗。

非常感谢。

import java.awt.ComponentOrientation;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;


@SuppressWarnings("serial")
public class Calculator extends JFrame{

private interface CalculatorInterface {
public void writeToScreen(String text);
public void clearScreen();
public String getScreenText();
}

private class CalculatorPanel extends JPanel implements CalculatorInterface {

private class NumberPanel extends JPanel implements CalculatorInterface {

private static final int NUMTOTAL = 10;

private CalculatorPanel calcPanel;
private JButton[] numButtons;

public NumberPanel(CalculatorPanel calcPanel) {
this.calcPanel = calcPanel;
buildLayout();
addButtons();
}
private void buildLayout() {
this.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);

GridLayout layout = new GridLayout(4,3);
layout.setHgap(1);
layout.setVgap(1);

this.setLayout(new GridLayout(4,3));

}
private void addButtons() {
numButtons = new JButton[NUMTOTAL];

for(int i = numButtons.length -1; i >= 0 ; i--) {
numButtons[i] = new JButton("" + i);
numButtons[i].setPreferredSize(new Dimension(60,40));
numButtons[i].setFont(new Font("Sans serif", Font.PLAIN, 18));
numButtons[i].addActionListener(
new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String text = ((JButton)e.getSource()).getText().trim();
if(getScreenText().equals("Invalid Calc")) {
clearScreen();
writeToScreen(text);
}else {
writeToScreen(text);
}
}
});
this.add(numButtons[i]);
}
}
@Override
public void writeToScreen(String text) {
calcPanel.writeToScreen(text);
}
@Override
public void clearScreen() {
calcPanel.clearScreen();

}
@Override
public String getScreenText() {
return calcPanel.getScreenText();
}

}

private class OperPanel extends JPanel implements CalculatorInterface {

private static final int ADD = 0;
private static final int SUB = 1;
private static final int MULT = 2;
private static final int DIV = 3;
private static final int OPENB = 4;
private static final int CLOSEB = 5;
private static final int CLEAR = 6;
private static final int EQL = 7;


private static final int OPERTOTAL = 8;

private CalculatorPanel calcPanel;
private JButton[] operButtons;

public OperPanel(CalculatorPanel calcPanel) {
this.calcPanel = calcPanel;
buildLayout();
addButtons();
}

private void buildLayout() {
GridLayout layout = new GridLayout(4,1);
layout.setHgap(1);
layout.setVgap(1);

this.setLayout(new GridLayout(4,1));
}

private void addButtons() {
operButtons = new JButton[OPERTOTAL];

operButtons[ADD] = makeButton(ADD, "+");
operButtons[SUB] = makeButton(SUB, "-");
operButtons[MULT] = makeButton(MULT, "*");
operButtons[DIV] = makeButton(DIV, "/");
operButtons[CLEAR] = makeButton(CLEAR, "CL");
operButtons[EQL] = makeButton(EQL, "=");
operButtons[OPENB] = makeButton(OPENB, "(");
operButtons[CLOSEB] = makeButton(CLOSEB, ")");

for(JButton button: operButtons) {
this.add(button);
}
}

private JButton makeButton(int index, String label) {

operButtons[index] = new JButton(label);
operButtons[index].addActionListener(
new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String text = ((JButton)e.getSource()).getText();
if(text.equals("=")) {
String screenText = getScreenText();
clearScreen();
try {
writeToScreen(getAnswer(screenText));
}catch(Exception excep) {
writeToScreen("Invalid Calc");
}
}else if(text.equals("CL")) {
clearScreen();
}else {
writeToScreen(text);
}
}
});

return operButtons[index];
}


private String getAnswer(String text) throws Exception {
/*I'm trying to solve for any input by the user e.g
*(the stuff in square brackets represents what is displayed
* on the screen:.
*[1+1] (hits equals) [2]
*[1+2-3] (hits equals) [0]
*[1+2*3] (hits equals) [7]
*[10*(14+1/2)] (hits equals) [145]
*/
throw new Exception();
}
@Override
public String getScreenText() {
return calcPanel.getScreenText();
}

@Override
public void clearScreen() {
calcPanel.clearScreen();

}

@Override
public void writeToScreen(String text) {
calcPanel.writeToScreen(text);

}

}


private NumberPanel numPanel;

private OperPanel operPanel;
private JTextField calcScreen;

public CalculatorPanel(JTextField calcScreen) {
this.calcScreen = calcScreen;

buildNumPanel();
buildOperPanel();

buildCalcPanel();


}
private void buildNumPanel() {
this.numPanel = new NumberPanel(this);
}

private void buildOperPanel() {
this.operPanel = new OperPanel(this);
}

private void buildCalcPanel() {
this.setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
this.add(numPanel);
this.add(operPanel);
}


@Override
public void writeToScreen(String text) {
calcScreen.setText(getScreenText() + text);
}

@Override
public String getScreenText() {
return calcScreen.getText();
}

@Override
public void clearScreen() {
calcScreen.setText("");
}
}

private JPanel mainPanel;
private JTextField calcScreen;

private CalculatorPanel calcPanel;
public Calculator() {
buildScreen();

buildCalcPanel();

buildMainPanel();

buildCalculator();
}

private void buildScreen() {
this.calcScreen = new JTextField();
this.calcScreen.setPreferredSize(new Dimension(150,50));
this.calcScreen.setHorizontalAlignment(JTextField.CENTER);
this.calcScreen.setFont(new Font("Sans serif", Font.PLAIN, 30));

}

private void buildCalcPanel() {
this.calcPanel = new CalculatorPanel(this.calcScreen);
}

private void buildMainPanel() {
this.mainPanel = new JPanel();
this.mainPanel.setBorder(new EmptyBorder(10,10,10,10));
this.mainPanel.setLayout(new BoxLayout(this.mainPanel, BoxLayout.Y_AXIS));

this.mainPanel.add(calcScreen);
this.mainPanel.add(calcPanel);
}

private void buildCalculator() {

this.add(mainPanel);
this.setTitle("Calculator");
this.pack();
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}

public static void main(String[] args) {
@SuppressWarnings("unused")
Calculator calc = new Calculator();
}

}

如何检查字符串是否是简单计算器的有效计算?

编辑 1:修复了 makeButton() 方法中的一个愚蠢错误,即我传入要验证的按钮文本而不是屏幕上的文本。 (我是个白痴。)

编辑 2:从代码中删除了 isValid(String text) 并使其成为 getAnswer() 方法,以便在输入不是有效计算时抛出异常。

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com