java 编写计算器 怎样实现连续运算?

[复制链接]
查看11 | 回复3 | 2010-6-13 09:40:49 | 显示全部楼层 |阅读模式
如 我先按1+2 后再按+3之后再按=
t:文本框
str=t.gettext();
double x,y;
if (e.getSource() == jia) {
x = Double.parseDouble(str);
t.setText(" ");
yunsuan = "+";

} else if (e.getSource() == jian) {
x = Double.parseDouble(str);
t.setText(" ");
yunsuan = "-";
} else if (e.getSource() == cheng) {
x = Double.parseDouble(str);
t.setText(" ");
yunsuan = "*";
} else if (e.getSource() == chu) {
x = Double.parseDouble(str);
t.setText(" ");
yunsuan = "/";
} else if (e.getSource() == deng) {
y = Double.parseDouble(str);
if (yunsuan == "+") {

//t.setText(String.valueOf(x + y));

y=x+y;

t.setText(""+y);
}
if (yunsuan == "-") {

//t.setText(String.valueOf(x - y));

y=x-y;

t.setText(""+y);
}
if (yunsuan == "*") {

//t.setText(String.valueOf(x * y));

y=x*y;

t.setText(""+y);
}
if (yunsuan == "/") {

//t.setText(String.valueOf(x / y));

y=x/y;

t.setText(""+y);
}
}

回复

使用道具 举报

千问 | 2010-6-13 09:40:49 | 显示全部楼层
简单方法是每按一个运算符就将前面的都计算出来 有些计算器就是这样的比较好的办法就是采用二叉树的结构 每个数字都是一个叶子节点 其他的是运算符 这个好处是能实现运算符的优先级 比如说a + b 便是节点 + 下面2节点a b再a + b × c 则b节点变成 × 下面2节点 b c若是a + b + c 则 +节点变成 + 下面 + c 两节点 a b 又是+的节点 然后按=的话就根据二叉树计算出最后结果
回复

使用道具 举报

千问 | 2010-6-13 09:40:49 | 显示全部楼层
http://hi.baidu.com/zpsailor/blog/item/6918f41696e1d9084b90a737.html这是我之前做的一个计算器程序,还是比较清晰明了,希望你看了可以解决你的问题。
回复

使用道具 举报

千问 | 2010-6-13 09:40:49 | 显示全部楼层
用递归,记录中间值啊!这是最笨的方法!也是最简单的!
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题

0

回帖

4882万

积分

论坛元老

Rank: 8Rank: 8

积分
48824836
热门排行