簡單混合運算的計算器
實現效果
程式碼
Main
package calc;
public class Main {
public static void main(String[] args) {
new MyJFrame();
}
}
MyJFrame
package calc;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class MyJFrame extends JFrame implements ActionListener {
/*結果文字框和輸入文字框*/
private JTextField resultText;
private JTextField input;
/*退格、清空*/
private JPanel middlePanel;
private JButton backspaceBtn;
private JButton clearBtn;
private JButton unknownBtn;
/*數字皮膚*/
private JPanel mainPanel;
/*運算式*/
private Expression expression;
/*記錄點選的數字,方便多位數的存入*/
private String tempNum;
/*計算結果*/
private String result;
public MyJFrame() {
this.setTitle("計算器");
this.setLayout(new FlowLayout());
init();
this.setBounds(200, 200, 350, 350);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void init() {
expression = new Expression();
tempNum = "";
result = "";
resultText = new JTextField(25);
resultText.setBorder(null);
resultText.setHorizontalAlignment(JTextField.RIGHT);
input = new JTextField(25);
input.setHorizontalAlignment(JTextField.RIGHT);
/*Border lineBorder = BorderFactory.createLineBorder(Color.blue);
resultText.setBorder(lineBorder);*/
middlePanel = new JPanel();
backspaceBtn = new JButton("backspace");
clearBtn = new JButton("clear");
unknownBtn = new JButton("(");
backspaceBtn.addActionListener(this);
clearBtn.addActionListener(this);
unknownBtn.addActionListener(this);
middlePanel.add(backspaceBtn);
middlePanel.add(clearBtn);
middlePanel.add(unknownBtn);
mainPanel = new JPanel();
mainPanel.setLayout(new GridLayout(4, 4, 30, 20));
JButton btn = null;
int n = 7;
for (int i = 1; i <= 16; i++) {
if (i == 4) {
btn = new JButton("/");
n -= 6;
} else if (i == 8) {
btn = new JButton("*");
n -= 6;
} else if (i == 12) {
btn = new JButton("-");
n -= 6;
} else if (i == 13) {
btn = new JButton("0");
} else if (i == 14) {
btn = new JButton(".");
} else if (i == 15) {
btn = new JButton("=");
} else if (i == 16) {
btn = new JButton("+");
} else {
btn = new JButton(String.valueOf(n++));
}
btn.addActionListener(this);
mainPanel.add(btn);
}
this.add(resultText);
this.add(input);
this.add(middlePanel);
this.add(mainPanel);
}
@Override
public void actionPerformed(ActionEvent e) {
JButton button=(JButton)e.getSource();
String click = button.getText();
if (click.equals("1") || click.equals("2") || click.equals("3") || click.equals("4") || click.equals("5")
|| click.equals("6") || click.equals("7") || click.equals("8") || click.equals("9") || click.equals("0")
|| click.equals(".")) {
this.tempNum += click;
}
if (click.equals("+") || click.equals("-") || click.equals("*") || click.equals("/") || click.equals("=")) {
expression.add(new NumSign(this.input.getText(), click.charAt(0)));
this.tempNum = "";
}
if (click.equals("backspace")) {
this.tempNum = this.tempNum.substring(0, this.tempNum.length() - 1);
}
if (click.equals("clear")) {
this.expression.clear();
this.input.setText("");
this.tempNum = "";
}
// 顯示輸入框內容
this.input.setText(tempNum);
// 顯示結果框內容
result = expression.toString();
this.resultText.setText(result);
// 計算結果
if (click.equals("=")) {
expression.calculate();
this.resultText.setText(result + this.expression.get(0).getStrNum());
this.expression.clear();
this.result = "";
this.tempNum = "";
}
}
}
Expression
package calc;
import java.util.LinkedList;
public class Expression extends LinkedList<NumSign> {
@Override
public String toString() {
String str = "";
for (NumSign numSign : this) {
str += numSign.getStrNum() + numSign.getSign();
}
/*if (this.size() != 0) {
return this.get(0).getStrNum();
}*/
return str;
}
public double calculate() {
this.calMulAndDiv();
this.calAddAndSub();
return this.get(0).getNum();
}
private void calMulAndDiv() {
for (int i = 0; i < this.size(); i++) {
if (this.size() == 1) return;
if (this.get(i).getSign() == '*') {
double temp = this.get(i).getNum() * this.get(i + 1).getNum();
this.get(i + 1).setNum(temp);
this.remove(i);
i = -1;
continue;
}
if (this.get(i).getSign() == '/') {
double temp = this.get(i).getNum() / this.get(i + 1).getNum();
this.get(i + 1).setNum(temp);
this.remove(i);
i = -1;
continue;
}
}
}
private void calAddAndSub() {
for (int i = 0; i < this.size(); i++) {
if (this.size() == 1) return;
if (this.get(i).getSign() == '+') {
double temp = this.get(i).getNum() + this.get(i + 1).getNum();
this.get(i + 1).setNum(temp);
this.remove(i);
i = -1;
continue;
}
if (this.get(i).getSign() == '-') {
double temp = this.get(i).getNum() - this.get(i + 1).getNum();
this.get(i + 1).setNum(temp);
this.remove(i);
i = -1;
continue;
}
}
}
}
NumSign
package calc;
public class NumSign {
private String strNum;
private char sign;
public NumSign() {
}
public NumSign(String strNum, char sign) {
this.strNum = strNum;
this.sign = sign;
}
public String getStrNum() {
return strNum;
}
public void setStrNum(String strNum) {
this.strNum = strNum;
}
public char getSign() {
return sign;
}
public void setSign(char sign) {
this.sign = sign;
}
public double getNum() {
return Double.parseDouble(this.strNum);
}
public void setNum(double num) {
this.strNum = String.valueOf(num);
}
@Override
public String toString() {
return "NumSign{" +
"strNum='" + strNum + '\'' +
", sign=" + sign +
'}';
}
}
相關文章
- 簡單版計算器
- MFC簡單計算器
- PHP簡單計算器PHP
- Java語言編寫計算器(簡單的計算器)Java
- 利用ANTLR4實現一個簡單的四則運算計算器
- JavaScript 簡單計算器效果JavaScript
- 簡單計算器(棧的應用)
- 一個最簡單的計算器
- 四則運算計算器
- 位運算簡單操作
- PyQt4(簡單計算器)QT
- 混合雲端計算vs霧計算
- Java簡單四則運算Java
- JavaScript簡單計算器程式碼分析JavaScript
- 16_簡單計算器實現
- IOS開發 製作簡單的計算器iOS
- Java實現一個簡單的計算器Java
- 【Flutter 實戰】簡約而不簡單的計算器Flutter
- 棧的應用——計算器的四則運算
- NumPy 簡單算術:加減乘除及其他運算
- javascript除法運算簡單介紹JavaScript
- js短路運算簡單介紹JS
- 7-20 簡單計算器 (20分)
- java方法練習之簡單計算器Java
- Object-C,四則運算計算器Object
- ACM 16進位制的簡單運算ACM
- Groovy簡單公式計算公式
- 用java實現一個簡單的計算器Java
- 運算器
- javascript求餘運算簡單介紹JavaScript
- javascript取模運算簡單介紹JavaScript
- Python編寫一個簡單計算器Python
- hdu 1237 Java 簡單計算器Java
- 16進位制的簡單運算(迴圈)
- 簡單計算器 (關於棧的一種應用)
- 0014---簡單的計算
- 2 簡單的計算機模型MARIE計算機模型
- 演算法(3)簡單四則運算演算法