簡單混合運算的計算器
實現效果
程式碼
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 +
'}';
}
}
相關文章
- 簡單計算器(棧的應用)
- 位運算簡單操作
- 四則運算計算器
- [20211220]sqlplus簡單計算器.txtSQL
- Java簡單四則運算Java
- 16_簡單計算器實現
- JavaScript簡單計算器程式碼分析JavaScript
- Java實現一個簡單的計算器Java
- IOS開發 製作簡單的計算器iOS
- HDU_1237 一個簡單的計算器
- 0014---簡單的計算
- 【Flutter 實戰】簡約而不簡單的計算器Flutter
- NumPy 簡單算術:加減乘除及其他運算
- java方法練習之簡單計算器Java
- 7-20 簡單計算器 (20分)
- Python編寫一個簡單計算器Python
- 用java實現一個簡單的計算器Java
- 教你python tkinter實現簡單計算器功能Python
- python簡易計算器Python
- 簡單計算器 (關於棧的一種應用)
- 16進位制的簡單運算(迴圈)
- 運算器
- 2 簡單的計算機模型MARIE計算機模型
- 1、簡單工廠模式實現計算器功能模式
- 用JS點選事件做一個簡單的計算器JS事件
- 演算法(3)簡單四則運算演算法
- 計算返回單、雙位元組字元混合字串長度字元字串
- c語言關於陣列的簡單運算C語言陣列
- Mac計算器的計算過程怎麼看?教你一鍵檢視運算記錄!Mac
- 82. 落單的數 ( 位運算-統計 )
- 從0到1實現一個簡單計算器
- python——設計一個簡單的購房商貸月供計算器Python
- 位運算子的計算
- 計算機基礎:位運算計算機
- 【計算機組成原理】第6章 計算機的運算方法計算機
- 效能測試混合場景計算
- 【雲端計算】雲端計算六大優點簡單說明
- 雲端計算開發學習教程,簡單介紹雲端計算