-
科学计算器<!--red_end--> 科学型 计算器——电子计算器的一种。可进行乘方、开方、指数、对数、三角函数、统计等方面的运算,又称函数计算器。目前的<!--red_beg-->计算器有<!--red_end-->标准型和科学型。 《用正则表达式处理字符串,只提取其中的数字和运算符,并转换成列。
-
科学计算器<!--red_end--> 科学型 计算器——电子计算器的一种。可进行乘方、开方、指数、对数、三角函数、统计等方面的运算,又称函数计算器。目前的<!--red_beg-->计算器有<!--red_end-->标准型和科学型。 《用正则表达式处理字符串,只提取其中的数字和运算符,并转换成列。转载于:https://www.cnblogs.com/taolishuang/p/7494997.html
展开全部
package example;
import java.awt.*;
import java.awt.event.*;
public class Calculator extends Frame {
/**
* 本实例实现功能如下 1.普通加减乘除运算 2.小数点的情况已经32313133353236313431303231363533e59b9ee7ad9431333332626130解决 3.开始按0已经解决 4.消去键可以起作用
*
*/
private static final long serialVersionUID = 1L;
private String name[] = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9",
".", "=", "+", "-", "*", "/", "Backspace", "", "", "C" };
private Button button[] = new Button[name.length];
private TextField textfield = new TextField("0.");
// 设置2个字符A1,A2用于存放点击运算符号之前的String数据
private String A1 = null, A2 = null;
// 设置2个字符B1,B2用于存放点击运算符号之后的String数据
private String B1 = null, B2 = null;
// 存放运算符号前后的数据,douuble类型进行运算
private double A, B;
// s存放为哪种运算符号,Result存放最后的运行结果
private String Result="0", s;
// 判断这个数字是否为小数,小数的时为true不是时为false
private boolean Decimal=false;
// 构造器,显示在标题栏
public Calculator() {
super("TEST:Caculator");
}
// 计算器的基本布局,在一个BorderLayout上面放置了一个GridLayout一个BorderLayout
public void init() {
setLayout(new BorderLayout(2, 2));
// 设置2个Panel
Panel p0 = new Panel();
Panel p1 = new Panel();
// p0上添加所有按扭
p0.setLayout(new GridLayout(5, 4, 1, 1));
// 不同的按扭采用不同的监听事件0-9和"."采用ButtonListener()
for (int i = 0; i < 11; i++) {
button = new Button(name);
// 设置字体颜色为蓝色
button.setForeground(Color.blue);
p0.add(button);
button.addActionListener(new ButtonListener());
}
// 其余的运算符号采取ButtonListener2()另一监听事件
for (int i = 11; i < name.length; i++) {
button = new Button(name);
// 设置字体颜色为红色
button.setForeground(Color.RED);
p0.add(button);
button.addActionListener(new ButtonListener2());
}
// 设置中间2个按扭不可见,增加美观,对称
button[17].setVisible(false);
button[18].setVisible(false);
// p1上添加文本输出区域
p1.setLayout(new BorderLayout(5, 5));
p1.add(textfield);
// 不可以向文本框里随便写东西
textfield.setEditable(false);
// 设置文本框背景为白色
textfield.setBackground(Color.WHITE);
add(p0, BorderLayout.SOUTH);
add(p1, BorderLayout.NORTH);
pack();
// 设置打开窗口在显示器的中间显示
setLocationRelativeTo(null);
// 关闭按扭,适配器模式
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
// 调整可见
setVisible(true);
}
public class ButtonListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
System.out.print(e.getActionCommand() + " ");
// 当A2和B2同时为null时候表示程序第一次运行或者开始一次新的运行,即按"="之后,将按键获取的值赋给A2--setp1
if (A2 == null && B2 == null) {
// 所按的数字按扭不是"0"的时候对A2进行赋值,否则给A2值不变,但是让textfield恢复初始值"0."
if (!"0".equals(e.getActionCommand())) {
// 考虑第一次按小数点的情况,按小数点后将boolean类型的Decimal定义为true
if (".".equals(e.getActionCommand())) {
A2 = "0.";
Decimal = true;
textfield.setText(A2);
} else {
A2 = e.getActionCommand();
textfield.setText(A2);
}
} else {
if ("0".equals(e.getActionCommand())) {
} else {
A2 = e.getActionCommand();
textfield.setText("0");
}
}
// 当A2不等于null,B2和A为null,表示还没有按运算符号,仍然对A2进行赋值
} else if (A2 != null && A1 == null && B2 == null) {
// 已经是小数的在点小数点不做任何动作,不是小数的在按"."的时候追加赋值并且设置Decimal为true
if (".".equals(e.getActionCommand())) {
if (Decimal) {
} else {
Decimal = true;
A2 += e.getActionCommand();
textfield.setText(A2);
}
} else {
A2 += e.getActionCommand();
textfield.setText(A2);
}
// 当A,A2不等于null,B2为null,表示刚按过运算符号,开始对B2进行赋值
// 仍要考虑"0"的情况,但要考虑0做为被减数,乘数和加数,此处B2可以等于0
// B2也可以是小数将Decimal设置回false
} else if (A2 != null && A1 != null && B2 == null) {
Decimal = false;
if (!"0".equals(e.getActionCommand())) {
// 不为"0"但为"."的情况,原理同上
if (".".equals(e.getActionCommand())) {
Decimal = true;
if (Decimal) {
B2 = "0.";
} else {
Decimal = true;
B2 = e.getActionCommand();
textfield.setText(B2);
}
} else {
B2 = e.getActionCommand();
textfield.setText(B2);
}
} else {
B2 = "0";
textfield.setText(B2);
}
// 当A,A2,B2都不为null的时候表示B2已经被赋值,继续按数字按扭对B2继续赋值
// 当B2等于0的时候不进行追加直接赋值,仅当B2不等于0的时候才进行追加
} else if (A2 != null && A1 != null && B2 != null) {
if ("0".equals(B2)) {
// 考虑用户连续2次按"0"的情况
if ("0".equals(e.getActionCommand())) {
B2 = "0";
textfield.setText(B2);
} else {
B2 = e.getActionCommand();
textfield.setText(B2);
}
} else {
// 不为"0"但为"."的情况,原理同上
if (".".equals(e.getActionCommand())) {
if (Decimal) {
} else {
Decimal = true;
B2 += e.getActionCommand();
textfield.setText(B2);
}
} else {
B2 += e.getActionCommand();
textfield.setText(B2);
}
}
}
}
}
public class ButtonListener2 implements ActionListener {
public void actionPerformed(ActionEvent e) {
System.out.print(e.getActionCommand() + " ");
// 一旦按了运算符号"+-*/"A2赋值给A,使得A和A2都不为null,但此时B2还为null在按数字键的时候便会给B2赋值
// 给s--运算符号赋值
if ("+".equals(e.getActionCommand())) {
if (A2 == null) {
A2 = "0";
}
A1 = A2;
s = "+";
} else if ("-".equals(e.getActionCommand())) {
if (A2 == null) {
A2 = "0";
}
A1 = A2;
s = "-";
} else if ("*".equals(e.getActionCommand())) {
if (A2 == null) {
A2 = "0";
}
A1 = A2;
s = "*";
} else if ("/".equals(e.getActionCommand())) {
if (A2 == null) {
A2 = "0";
}
A1 = A2;
s = "/";
// 等号的时候把B2赋值给B,进行最后的运算
} else if ("=".equals(e.getActionCommand())) {
if (A2 == null) {
A2 = "0";
}
if (B2 == null) {
B2 = "0";
}
A1 = A2;
B1 = B2;
// 将String类型转换为double进行运算
A = Double.parseDouble(A1);
B = Double.parseDouble(B1);
if ("+".equals(s)) {
Result = String.valueOf(A + B);
System.out.println();
System.out.println(A + "+" + B + "=" + Result);
} else if ("-".equals(s)) {
Result = String.valueOf(A - B);
System.out.println();
System.out.println(A + "-" + B + "=" + Result);
} else if ("*".equals(s)) {
Result = String.valueOf(A * B);
System.out.println();
System.out.println(A + "*" + B + "=" + Result);
}
textfield.setText(Result);
A1 = null;
A2 = null;
B1 = null;
B2 = null;
Decimal=false;
} else if ("Backspace".equals(e.getActionCommand())) {
String text = "0";
if (A2 == null && A1 == null) {
} else if (A2 != null && A1 == null && B2 == null) {
int t = A2.length();
text = A2;
if (t == 1) {
text = "0";
} else {
text = text.substring(0, t - 1);
}
A2 = text;
} else if (A2 != null && A1 != null && B2 == null) {
} else {
int t = B2.length();
text = B2;
if (t == 1) {
text = "0";
} else {
text = text.substring(0, t - 1);
}
B2 = text;
}
textfield.setText(text);
System.out.println();
System.out.println("text=" + text + " ");
System.out.println(A1 + ":" + A2 + ":" + B1 + ":" + B2);
// 选择"C"的时候将计算器重置,即恢复到开始的状态
} else if ("C".equals(e.getActionCommand())) {
textfield.setText("0.");
A1 = null;
A2 = null;
B1 = null;
B2 = null;
Decimal=false;
}
}
}
public static void main(String[] args) {
Calculator calculator = new Calculator();
calculator.init();
}
} 下面还有
已赞过
已踩过<
你对这个回答的评价是?
评论
收起
科学计算器 科学型 计算器——电子计算器的一种。可进行乘方、开方、指数、对数、三角函数、统计等方面的运算,又称函数计算器。目前的计算器有标准型和科学型。
《用正则表达式处理字符串,只提取其中的数字和运算符,并转换成列表
编写一个函数,处理没有括号的基本运算的基本表达式
再写一个函数递归处理带有括号的函数,先计算最内部括号中的表达式, 然后将最内部的括号替换为计算后的结果, 在递归外部一层的, 最后返回的就是所需的结果》
转载于:https://www.cnblogs.com/xiaoluziwoaini/p/7486802.html
引言:计算器 abacus 2 完成几个月了,越来越不满足于 abacus 的结构逻辑,总觉得好多地方不够完美,早就有全部重写的打算,苦于工作繁忙,一直没有机会,趁着清明节小假的机会,准备把 abacus 3 的设计给做了。
1.简介
abacus 3 是一个小型简易计算器,可以用它作为计算机上的日常计算器使用,比系统自带计算器强大得多,比如它支持用户自定义运算符或者函数,支持使用变量和符号常量等。
2.总体描述
2.1 产品目标
abacus 3 的目标是打造一个日常使用的计算器,用于代替系统自带计算器,并提供更多实用的数学功能。
2.2 产品功能
abacus 3 需要具备如下功能:
3.详细需求
- 支持四则混合运算
- 支持常用数学函数
- 支持符号常量
- 支持用户自定义变量并参与运算
- 支持用户自定义运算符与函数
3.1 界面需求
abacus 3 暂不使用 GUI 界面,以命令行的方式运行程序。程序启动后,用户逐条输入计算命令,并等待计算器输出结果。
3.2 功能需求
- 支持四则混合运算,详细的运算符列表见下表,还需支持表达式的嵌套,嵌套括号限定为小括号,但允许任意层嵌套。
运算符形式 功能描述 x + y 求 x 与 y 的和 x - y 求 x 减去 y 的差 x * y 求 x 与 y 的积 x / y 求 x 除以 y 的商 x % y 求 x 除以 y 的余数 - x 求 x 的相反数 x ^ y 求 x 的 y 次方 x! 求 x 的阶乘 x!! 求 x 的双阶乘
- 支持常用数学函数,需要支持三角函数、指对函数等常用的数学函数,函数的详细列表见下表。
函数形式 功能描述 abs(x) 求 x 的绝对值 pow(x, y) 求 x 的 y 次方 pow10(x) 求 10 的 x 次方 exp(x) 求 e 的 x 次方,e 是自然对数的底数 log(x, y) 求 x 的以 y 为底的对数 log10(x) 求 x 的以 10 为底的对数 ln(x) 求 x 的以 e 为底的对数,e 是自然对数的底数 sqrt(x) 求 x 的算术平方根 sin(x) 求 x 的正弦,x 采用弧度制 cos(x) 求 x 的余弦,x 采用弧度制 tan(x) 求 x 的正切,x 采用弧度制 arcsin(x) 求 x 的反正弦 arccos(x) 求 x 的反余弦 arctan(x) 求 x 的反正切 sinh(x) 求 x 的双曲正弦 cosh(x) 求 x 的双曲余弦 tanh(x) 求 x 的双曲正切 max(x1, x2, ..., xn) 求 x1, x2, ..., xn 中的最大值 min(x1, x2, ..., xn) 求 x1, x2, ..., xn 中的最小值 ceil(x) 求不小于 x 的最小整数 floor(x) 求不大于 x 的最大整数
- 支持符号常量,在表达式中可以使用圆周率、自然对数的底数等符号常量,例如求半径为 2.3 的圆的面积可以输入表达式 PI * 2.3 * 2.3,用户可以定义自己的符号常量并永久保存并使用,计算器内置符号常量列表如下:
标识符 描述 值 PI 圆周率 3.141592654 E 自然对数的底数 2.718281828
- 支持用户自定义变量并参与运算,要求如下:
- 变量的名称中只能包含字母和数字,并且必须以字母开头
- 允许一次定义多个变量
- 定义变量的时候可以指定值也可以不指定
- 需有变量赋值的功能
- 如果使用未经赋值的变量参与运算,程序应当给出出错提示
- 允许用户定义符号常量并永久存储使用
- 支持用户自定义运算符与函数,要求如下:
- 函数名称只能包含字母和数字,并且必须以字母开头
- 允许一次定义多个运算符或函数,运算符需指定优先级
- 定义运算符或函数时必须指定完成计算的表达式
- 自定义的运算符或函数不得与内置运算符或函数同名冲突
- 提供自定义运算符或函数的永久存储使用
3.3 非功能需求
- 程序应该跨平台
- 运算响应速度应得到保证
- 应具有简短易理解的错误提示
转载于:https://my.oschina.net/zhcosin/blog/119886