设为首页
收藏本站
开启辅助访问
切换到窄版
登录
立即注册
中问网首页
我的收藏
站长博客
搜索
搜索
本版
帖子
用户
第一问答网
»
论坛
›
中问网
›
问答
›
java 编写计算器 怎样实现连续运算?
返回列表
发新帖
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
|
显示全部楼层
用递归,记录中间值啊!这是最笨的方法!也是最简单的!
回复
使用道具
举报
返回列表
发新帖
高级模式
B
Color
Image
Link
Quote
Code
Smilies
您需要登录后才可以回帖
登录
|
立即注册
本版积分规则
发表回复
回帖后跳转到最后一页
千问
主题
0
回帖
4882万
积分
论坛元老
论坛元老, 积分 48824836, 距离下一级还需 -38824837 积分
论坛元老, 积分 48824836, 距离下一级还需 -38824837 积分
积分
48824836
加好友
发消息
回复楼主
返回列表
问答
热门排行