简易计算器两位数加减乘除
时间: 2024-06-19 11:02:16 浏览: 11
简易计算器可以实现两个数的加、减、乘、除运算,以下是实现方式:
1. 加法:将两个数相加即可得到结果。
例如: 12 + 13 = 25
2. 减法:将第二个数从第一个数中减去即可得到结果。
例如: 20 - 8 = 12
3. 乘法:将两个数相乘即可得到结果。
例如: 6 * 7 = 42
4. 除法:将第一个数除以第二个数即可得到结果。
注意,如果第二个数为0,则除法无法进行。
例如: 45 / 5 = 9
相关问题
数码管显示简易计算器
根据提供的两个引用,可以得知这是一个基于数码管的简易计算器,可以进行整数的加减乘除运算。下面是一个简单的实现过程:
1. 定义变量a、b、op和result,分别表示第一个数、第二个数、运算符和结果。
2. 通过按键输入数字,将数字存入a或b中。具体实现方式是:根据KeyValue值判断当前输入的是数字还是运算符,如果是数字,则根据当前输入的位数(个位、十位、百位等)将数字存入a或b中;如果是运算符,则将当前输入的运算符存入op中,并将数码管清零。
3. 当输入完第二个数后,根据op的值进行相应的运算,并将结果存入result中。
4. 将result在数码管上显示出来。
下面是一个简单的代码实现:
```python
# 定义变量
a = 0
b = 0
op = ''
result = 0
# 循环读取按键输入
while True:
# 读取KeyValue值
KeyValue = readKeyValue()
# 判断当前输入的是数字还是运算符
if KeyValue in range(10):
# 输入的是数字,根据当前输入的位数将数字存入a或b中
if op == '':
a = a * 10 + KeyValue
display(a)
else:
b = b * 10 + KeyValue
display(b)
elif KeyValue == 12:
# 输入的是加号
op = '+'
clearDisplay()
elif KeyValue == 13:
# 输入的是减号
op = '-'
clearDisplay()
elif KeyValue == 14:
# 输入的是乘号
op = '*'
clearDisplay()
elif KeyValue == 15:
# 输入的是除号
op = '/'
clearDisplay()
elif KeyValue == 16:
# 清零
a = 0
b = 0
op = ''
result = 0
clearDisplay()
elif KeyValue == 17:
# 等于号
if op == '+':
result = a + b
elif op == '-':
result = a - b
elif op == '*':
result = a * b
elif op == '/':
result = a / b
display(result)
```
帮我写一个编写简易计算器程序Calculator.java,能进行加减乘除的操作。用gul,界面类似苹果的计算器,并且注释详细点,运算结果需要精准
好的,以下是一个简单的计算器程序Calculator.java的示例代码,使用了Swing GUI库来实现界面:
```java
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class Calculator extends JFrame implements ActionListener {
private static final long serialVersionUID = 1L;
private JTextField display;
private String operator;
private double number1, number2;
public Calculator() {
super("Calculator");
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(4, 4));
display = new JTextField();
add(display, BorderLayout.NORTH);
JButton button1 = new JButton("1");
button1.addActionListener(this);
panel.add(button1);
JButton button2 = new JButton("2");
button2.addActionListener(this);
panel.add(button2);
JButton button3 = new JButton("3");
button3.addActionListener(this);
panel.add(button3);
JButton buttonAdd = new JButton("+");
buttonAdd.addActionListener(this);
panel.add(buttonAdd);
JButton button4 = new JButton("4");
button4.addActionListener(this);
panel.add(button4);
JButton button5 = new JButton("5");
button5.addActionListener(this);
panel.add(button5);
JButton button6 = new JButton("6");
button6.addActionListener(this);
panel.add(button6);
JButton buttonSubtract = new JButton("-");
buttonSubtract.addActionListener(this);
panel.add(buttonSubtract);
JButton button7 = new JButton("7");
button7.addActionListener(this);
panel.add(button7);
JButton button8 = new JButton("8");
button8.addActionListener(this);
panel.add(button8);
JButton button9 = new JButton("9");
button9.addActionListener(this);
panel.add(button9);
JButton buttonMultiply = new JButton("*");
buttonMultiply.addActionListener(this);
panel.add(buttonMultiply);
JButton buttonClear = new JButton("C");
buttonClear.addActionListener(this);
panel.add(buttonClear);
JButton button0 = new JButton("0");
button0.addActionListener(this);
panel.add(button0);
JButton buttonEquals = new JButton("=");
buttonEquals.addActionListener(this);
panel.add(buttonEquals);
JButton buttonDivide = new JButton("/");
buttonDivide.addActionListener(this);
panel.add(buttonDivide);
add(panel);
setSize(300, 300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
public static void main(String[] args) {
new Calculator();
}
public void actionPerformed(ActionEvent event) {
String command = event.getActionCommand();
if (command.charAt(0) >= '0' && command.charAt(0) <= '9' || command.equals(".")) {
if (!display.getText().contains(".")) {
display.setText(display.getText() + command);
}
} else if (command.equals("+") || command.equals("-") || command.equals("*") || command.equals("/")) {
operator = command;
number1 = Double.parseDouble(display.getText());
display.setText("");
} else if (command.equals("C")) {
display.setText("");
} else if (command.equals("=")) {
number2 = Double.parseDouble(display.getText());
switch (operator) {
case "+":
display.setText(String.format("%.2f", number1 + number2));
break;
case "-":
display.setText(String.format("%.2f", number1 - number2));
break;
case "*":
display.setText(String.format("%.2f", number1 * number2));
break;
case "/":
display.setText(String.format("%.2f", number1 / number2));
break;
}
}
}
}
```
该程序使用了一个JTextField来显示输入和结果,使用JButton来实现数字和操作符的输入,使用ActionListener来处理按钮的点击事件。
当用户点击数字按钮时,程序会将数字添加到显示文本框的末尾。当用户点击操作符按钮时,程序会将操作符保存在变量operator中,并将当前显示的数字保存在变量number1中,并清空显示文本框。当用户点击“=”按钮时,程序会将当前显示的数字保存在变量number2中,并根据操作符进行相应的计算,并将结果显示在文本框中。当用户点击“C”按钮时,程序会将文本框清空。
在计算过程中,程序使用了Java的内置数学函数来进行精确计算,并使用String.format()方法来将结果格式化为两位小数。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![py](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)