Topic: 最近编计算器又遇到了括号的问题了,

  Print this page

1.最近编计算器又遇到了括号的问题了, Copy to clipboard
Posted by: xuxiang84
Posted on: 2004-10-10 22:57

不知道你们有没有好的,又简单的方法,我的想法是用树来做,,不过很麻烦,具体还不知道怎么实现,就先做了一个只能实现一层括号的,,有好的算法还请告之,,谢谢先了

2.Re:最近编计算器又遇到了括号的问题了, [Re: xuxiang84] Copy to clipboard
Posted by: archonLing
Posted on: 2004-10-11 07:26

There are textbook examples of using Stack to solve calculator problems. Just look it up.

3.Re:最近编计算器又遇到了括号的问题了, [Re: xuxiang84] Copy to clipboard
Posted by: xuxiang84
Posted on: 2004-10-11 19:19

上面的大大。。我找了下没有找到,,你可以给我一个链接吗??

4.Re:最近编计算器又遇到了括号的问题了, [Re: xuxiang84] Copy to clipboard
Posted by: archonLing
Posted on: 2004-10-12 11:34

http://www.cs.dartmouth.edu/~cs68/03w/hw1.html

http://gladstone.uoregon.edu/~smahmulj/Calculator.java

5.Re:最近编计算器又遇到了括号的问题了, [Re: xuxiang84] Copy to clipboard
Posted by: xuxiang84
Posted on: 2004-10-12 21:39

谢谢,,今天老师来检查了,,我猜发觉我的做的好垃圾啊?!哎,,好郁闷。真不知道怎么办。。人 一下就崩溃了。

6.Re:最近编计算器又遇到了括号的问题了, [Re: xuxiang84] Copy to clipboard
Posted by: beckham
Posted on: 2004-10-12 23:25

就是哦 最近也做这个

7.Re:最近编计算器又遇到了括号的问题了, [Re: xuxiang84] Copy to clipboard
Posted by: why
Posted on: 2004-10-13 01:37

xuxiang84 wrote:
谢谢,,今天老师来检查了,,我猜发觉我的做的好垃圾啊?!哎,,好郁闷。真不知道怎么办。。人 一下就崩溃了。

坦白说:这么容易说崩溃,阁下实在不宜走IT的路。
资海无涯,回头是岸。

8.Re:最近编计算器又遇到了括号的问题了, [Re: xuxiang84] Copy to clipboard
Posted by: yamakasy
Posted on: 2004-10-13 11:32

这种东西一般用堆栈处理,你可以看看JEP和有关表达式计算的话题。

9.Re:最近编计算器又遇到了括号的问题了, [Re: xuxiang84] Copy to clipboard
Posted by: beckham
Posted on: 2004-10-13 21:14

函数怎么用比较好
是math类用
还是直接用math.函数名???
多谢

10.Re:最近编计算器又遇到了括号的问题了, [Re: xuxiang84] Copy to clipboard
Posted by: HW升
Posted on: 2004-10-14 21:23


//本程序段设计一个计算器图形界面,实现简单的加、减、乘、除运算。
//能够实现多项式的运算,如:30+2*3=
//但没有考虑到括号问题

//思想:根据实际推理,只要至多保存三个操作数,两个运算符,就能确定操作顺序!

import java.awt.*;
import java.awt.event.*;

public class Calculator extends Frame implements ActionListener
{
TextField tResult; //结果文本字段
Panel faceCalculator,panel; //计数器面板
Button button0,button1,button2,button3,button4;
Button button5,button6,button7,button8,button9;
Button buttonPoint,buttonAdd,buttonMul;
Button buttonResult,buttonDiv,buttonSub;

TextArea tFormula; //表达式输出文本域

Menu menu; //清零菜单
MenuItem menuItem;


float first,second,third,n;
char oper1,oper2;
boolean readyoper1,readyoper2,point;

int count; //记录小数点后的位数!


public Calculator(){ //构造函数
point=false;
readyoper1=readyoper2=false;
count=1;
init(); //初始化计数器面板
}
void init(){
MenuBar menuBar=new MenuBar();
panel=new Panel();
tResult=new TextField("0",40);
this.setLayout(new BorderLayout());
this.setTitle("计数器-何文升");
panel.add(tResult);
this.add(panel,"North");
faceCalculator=new Panel();
faceCalculator.setLayout(new GridLayout(4,4));
button0=new Button("0");button0.addActionListener(this);
button1=new Button("1");button1.addActionListener(this);
button2=new Button("2");button2.addActionListener(this);
button3=new Button("3");button3.addActionListener(this);
button4=new Button("4");button4.addActionListener(this);
button5=new Button("5");button5.addActionListener(this);
button6=new Button("6");button6.addActionListener(this);
button7=new Button("7");button7.addActionListener(this);
button8=new Button("8");button8.addActionListener(this);
button9=new Button("9");button9.addActionListener(this);
buttonPoint=new Button(".");buttonPoint.addActionListener(this);
buttonAdd=new Button("+");buttonAdd.addActionListener(this);
buttonSub=new Button("-");buttonSub.addActionListener(this);
buttonMul=new Button("*");buttonMul.addActionListener(this);
buttonDiv=new Button("/");buttonDiv.addActionListener(this);
buttonResult=new Button("=");buttonResult.addActionListener(this);
faceCalculator.add(button7);
faceCalculator.add(button8);
faceCalculator.add(button9);
faceCalculator.add(buttonDiv);
faceCalculator.add(button4);
faceCalculator.add(button5);
faceCalculator.add(button6);
faceCalculator.add(buttonMul);
faceCalculator.add(button1);
faceCalculator.add(button2);
faceCalculator.add(button3);
faceCalculator.add(buttonSub);
faceCalculator.add(button0);
faceCalculator.add(buttonPoint);
faceCalculator.add(buttonResult);
faceCalculator.add(buttonAdd);
this.add(faceCalculator,"Center");
tFormula=new TextArea(3,50);
this.add(tFormula,"South");

menu=new Menu("控制");
menuItem=new MenuItem("清零");
menuItem.addActionListener(this);
menu.add(menuItem);
menuBar.add(menu);
this.setMenuBar(menuBar);


this.setSize(300,300);
this.setResizable(false);
this.show();
}

public void addNum(float ch){ //输入多位数
if(!point){
n=n*10+ch;
}
else{
for(int i=0;i<count;i++) ch=ch/10;
n=n+ch;
count++;
}
}

public float sum(float a,float b,char oper){//根据运算符计算两个操作数
switch(oper){
case '+':a=a+b;break;
case '-':a=a-b;break;
case '*':a=a*b;break;
case '/':a=a/b;break;

}
return a;
}


public void actionPerformed(ActionEvent e){//事件处理器
int a=0;
if(e.getSource()==button0) {a=0;addNum(a);}
else if(e.getSource()==button1) {a=1;addNum(a);}
else if(e.getSource()==button2) {a=2;addNum(a);}
else if(e.getSource()==button3) {a=3;addNum(a);}
else if(e.getSource()==button4) {a=4;addNum(a);}
else if(e.getSource()==button5) {a=5;addNum(a);}
else if(e.getSource()==button6) {a=6;addNum(a);}
else if(e.getSource()==button7) {a=7;addNum(a);}
else if(e.getSource()==button8) {a=8;addNum(a);}
else if(e.getSource()==button9) {a=9;addNum(a);}
else if(e.getSource()==buttonPoint) {a='.';point=true;}
else {
point=false;
count=1;
if(readyoper2){
third=n;
second=sum(second,third,oper2);
readyoper2=false;
}
else if(readyoper1){
second=n;
}
else first=n;
n=0;
}
if(e.getSource()==buttonAdd) {
a='+';
if(readyoper1)
first=sum(first,second,oper1);
oper1='+';
readyoper1=true;
}
else if(e.getSource()==buttonSub) {
a='-';
if(readyoper1)
first=sum(first,second,oper1);
oper1='-';
readyoper1=true;
}
else if(e.getSource()==buttonMul)
{
a='*';
if(readyoper1){
if(oper1=='+'||oper1=='-') {readyoper2=true; oper2='*';}
else {first=sum(first,second,oper1);oper1='*';}
}
else{
readyoper1=true;
oper1='*';
}
}else if(e.getSource()==buttonDiv){
a='/';
if(readyoper1){
if(oper1=='+'||oper1=='-') {readyoper2=true; oper2='/';}
else {first=sum(first,second,oper1);oper1='/';}
}
else{
readyoper1=true;
oper1='/';
}

}else if(e.getSource()==buttonResult){
a='=';
first=sum(first,second,oper1);
tResult.setText("="+first);
}
if(e.getSource()==menuItem){
readyoper1=readyoper2=false;
point=false;
count=1;
tResult.setText("0");
tFormula.setText("");
}
else if(a>9)
tFormula.append(""+(char)a);
else
tFormula.append(""+a);
if(a=='=')tFormula.append(""+first);
}


public static void main(String[] args){
Calculator calculator=new Calculator();
calculator.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
}
}

11.Re:最近编计算器又遇到了括号的问题了, [Re: xuxiang84] Copy to clipboard
Posted by: beckham
Posted on: 2004-10-15 17:47

好象没有调试吧 有错哦

12.Re:最近编计算器又遇到了括号的问题了, [Re: xuxiang84] Copy to clipboard
Posted by: HW升
Posted on: 2004-10-16 07:09

我让老师表扬的题目!

13.Re:最近编计算器又遇到了括号的问题了, [Re: xuxiang84] Copy to clipboard
Posted by: xuxiang84
Posted on: 2004-10-16 21:01

我们班一个女生写了近3K行的 代码。。哎汗颜啊,,基本的我做出来了,现在在考虑括号和4则运算的问题,希望大家还是一样的关心哈,,有好的实现方法还请赐教

14.Re:最近编计算器又遇到了括号的问题了, [Re: HW升] Copy to clipboard
Posted by: xuxiang84
Posted on: 2004-10-16 21:12

HW升 wrote:
我让老师表扬的题目!

if(e.getSource()==button0) {a=0;addNum;}
else if(e.getSource()==button1) {a=1;addNum;}
else if(e.getSource()==button2) {a=2;addNum;}
else if(e.getSource()==button3) {a=3;addNum;}
else if(e.getSource()==button4) {a=4;addNum;}
else if(e.getSource()==button5) {a=5;addNum;}
else if(e.getSource()==button6) {a=6;addNum;}
else if(e.getSource()==button7) {a=7;addNum;}
else if(e.getSource()==button8) {a=8;addNum;}
else if(e.getSource()==button9) {a=9;addNum;}

中的addNUM是实例函数,,应该改为
addNum(0)
addNum(1)
addNum(2)
addNum(3)
addNum(4)
addNum(5)
addNum(6)
..........

是不是这样的!?!?

15.Re:最近编计算器又遇到了括号的问题了, [Re: xuxiang84] Copy to clipboard
Posted by: HW升
Posted on: 2004-10-16 22:18

这还用说啊!晕!
应该是系统的错吧,发上去就变样了,否则我的程序也不会运行成功的!

16.Re:最近编计算器又遇到了括号的问题了, [Re: xuxiang84] Copy to clipboard
Posted by: chengbd
Posted on: 2004-10-16 23:14

http://free-vz.htnet.hr/nstepan/W3Eval/W3Eval.html
这是我发现的一个比较好的代码,你参考一下,网上应该还有更多,如果知道可以交流一下。学会别人的优秀代码,进步更快。

17.Re:最近编计算器又遇到了括号的问题了, [Re: xuxiang84] Copy to clipboard
Posted by: beckham
Posted on: 2004-10-17 19:16

加括号和参数 调了一下就出来了

18.Re:最近编计算器又遇到了括号的问题了, [Re: chengbd] Copy to clipboard
Posted by: xuxiang84
Posted on: 2004-10-18 08:59

chengbd wrote:
http://free-vz.htnet.hr/nstepan/W3Eval/W3Eval.html
这是我发现的一个比较好的代码,你参考一下,网上应该还有更多,如果知道可以交流一下。学会别人的优秀代码,进步更快。


谢谢斑竹了,,不过是E文的,偶看起有点吃力,,

19.Re:最近编计算器又遇到了括号的问题了, [Re: xuxiang84] Copy to clipboard
Posted by: beckham
Posted on: 2004-10-18 10:57

dui o


   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923