怎么让计算器有一定的编程能力
比方说 我在文本框输入 (2+3)/3
其会读取并计算!

解决方案 »

  1.   

    Add ActionListener to your text field...
      

  2.   

    很简单.用MVEL实现.或者Java的EVAL
      

  3.   

    Someone will show u direct source codes. But everyone here will share their algorithm with u. It's will help u a lot...
      

  4.   

    建议你搜一下论坛,很多相关帖子.http://topic.csdn.net/u/20080709/12/c92cc549-3dc3-4a0b-b6e8-7c5fe7c4c9d5.html
      

  5.   

    package calculator;
    import java.awt.*; //引入包java.awt中所有的类  
    import java.awt.event.*; //引入包java.awt.event中所有的类public class Calculator extends WindowAdapter implements ActionListener{ //创建Calculator类,实现ActionListener接口
    private double result=0,data1=0,radixPointDepth=1; //定义变量
    private boolean radixPointIndicate=false,resultIndicate=false; 
    private char prec='+'; //创建优先默认字符"+" 
    private Frame f; //创建窗口 
    private TextField tf;//创建文本框
    private Button b1,b2,b3,b4,b5,b6,b7,b8,b9,b10,b11,b12,b13,b14,b15,b16,b17; //创建按钮 
    private Panel p; //创建/面板容器
    public static void main(String[] args){ //main方法,创建calGUI(图形用户界面),完成初始化
    //构造器
    Calculator de=new Calculator();//创建构造方法   
    de.go();
    }
    public void go(){
     
    f=new Frame("Calculator"); 
    p=new Panel(); //运算界面p 
    p.setLayout(new GridLayout(4,4));  // 设置p的布局为GridLayout,四行四列 

    tf=new TextField(30); 
    //实例化按钮 
    b1=new Button("7"); 
    b2=new Button("8"); 
    b3=new Button("9"); 
    b4=new Button("+"); 
    b5=new Button("4"); 
    b6=new Button("5"); 
    b7=new Button("6"); 
    b8=new Button("-"); 
    b9=new Button("1"); 
    b10=new Button("2"); 
    b11=new Button("3"); 
    b12=new Button("*"); 
    b13=new Button("0"); 
    b14=new Button("."); 
    b15=new Button("="); 
    b16=new Button("/"); 
    b17=new Button("Reset"); 
    f.add(tf,"North"); //把文本区域添加到框架的上方 
    f.add(p,"Center"); //把面板加到框架的中间
    f.add(b17,"South"); //把按钮(Reset)添加到框架的下方 
    //把按钮添加到面版上
    p.add(b1); 
    p.add(b2); 
    p.add(b3); 
    p.add(b4); 
    p.add(b5); 
    p.add(b6); 
    p.add(b7); 
    p.add(b8); 
    p.add(b9); 
    p.add(b10); 
    p.add(b11); 
    p.add(b12); 
    p.add(b13); 
    p.add(b14); 
    p.add(b15); 
    p.add(b16); 
    //为按钮添加监听
    b1.addActionListener(this); 
    b2.addActionListener(this); 
    b3.addActionListener(this); 
    b4.addActionListener(this); 
    b5.addActionListener(this); 
    b6.addActionListener(this); 
    b7.addActionListener(this); 
    b8.addActionListener(this); 
    b9.addActionListener(this); 
    b10.addActionListener(this); 
    b11.addActionListener(this); 
    b12.addActionListener(this); 
    b13.addActionListener(this); 
    b14.addActionListener(this); 
    b15.addActionListener(this); 
    b16.addActionListener(this); 
    b17.addActionListener(this);
    f.addWindowListener(this); //为框架添加监听  
    f.setSize(300,200); //设置框架的大小
    f.setVisible(true); //设置框架为可见
    }  
    //监听程序
    public void actionPerformed(ActionEvent e){ 
    String s; 
    s=e.getActionCommand(); 
    //Returns the command string associated with this action.;
    //SWITCH开关 
    switch(s.charAt(0)){ //Returns the char value at the specified index.
    case '0': case '1': case '2': case '3': case '4': case '5': case '6': case 
    '7': case '8': case '9': //按了“0-9”,就执行下面
    if(resultIndicate){ 
    result=0; 
    data1=0; 
    prec='+'; 

    Integer Int1=new Integer(s); 
    if(radixPointIndicate){ 
    radixPointDepth=radixPointDepth/10; 
    data1=data1+(Int1.intValue())*radixPointDepth; 
    }
    else{ 
    data1=data1*10+(Int1.intValue()); 

    Double displayNumber=new Double(data1); 
    tf.setText(displayNumber.toString()); 
    resultIndicate=false; 
    break; 
    case '+': case '-':case '*':case '/':case '=': //按了“+、-、*、/”,就执行下面 
    if(s.charAt(0)!='='&&resultIndicate){ 
    prec=s.charAt(0); 
    resultIndicate=false; 
    }
    else{ 
    //用SWITCH开关运算出执行了“+、-、*、/”的结果
    switch(prec){ 
    case '+': 
    result=result+data1; 
    break; 
    case '-': 
    result=result-data1; 
    break; 
    case '*': 
    result=result*data1; 
    break; 
    case '/': 
    result=result/data1; 
    break; 


    radixPointIndicate=false; 
    radixPointDepth=1; 
    displayNumber=new Double(result); 
    tf.setText(displayNumber.toString()); 
    //监听是否按了“=” 
    if(s.charAt(0)!='='){ 
    data1=0; 
    prec=s.charAt(0); 
    }
    else{ 
    resultIndicate=true; 

    break; 
    case '.': 
    radixPointIndicate=true; 
    break; 

    //监听是否按了为“Reset”,是则对各数据Reset
    if(s.equals("Reset")) 

    result=0; 
    data1=0; 
    radixPointDepth=1; 
    tf.setText(""); 


    public void windowClosing(WindowEvent e){ 
    System.exit(0); 
    }
    }看看是不是你想要的
      

  6.   

    通过监听进行,java编写的计算器小例子
    http://blog.csdn.net/gaoweijie/archive/2007/12/18/1946245.aspx
      

  7.   


    package calculator;
    import java.awt.*; //引入包java.awt中所有的类  
    import java.awt.event.*; //引入包java.awt.event中所有的类public class Calculator extends WindowAdapter implements ActionListener{ //创建Calculator类,实现ActionListener接口
    private double result=0,data1=0,radixPointDepth=1; //定义变量
    private boolean radixPointIndicate=false,resultIndicate=false; 
    private char prec='+'; //创建优先默认字符"+" 
    private Frame f; //创建窗口 
    private TextField tf;//创建文本框
    private Button b1,b2,b3,b4,b5,b6,b7,b8,b9,b10,b11,b12,b13,b14,b15,b16,b17; //创建按钮 
    private Panel p; //创建/面板容器
    public static void main(String[] args){ //main方法,创建calGUI(图形用户界面),完成初始化
    //构造器
    Calculator de=new Calculator();//创建构造方法   
    de.go();
    }
    public void go(){
     
    f=new Frame("Calculator"); 
    p=new Panel(); //运算界面p 
    p.setLayout(new GridLayout(4,4));  // 设置p的布局为GridLayout,四行四列 

    tf=new TextField(30); 
    //实例化按钮 
    b1=new Button("7"); 
    b2=new Button("8"); 
    b3=new Button("9"); 
    b4=new Button("+"); 
    b5=new Button("4"); 
    b6=new Button("5"); 
    b7=new Button("6"); 
    b8=new Button("-"); 
    b9=new Button("1"); 
    b10=new Button("2"); 
    b11=new Button("3"); 
    b12=new Button("*"); 
    b13=new Button("0"); 
    b14=new Button("."); 
    b15=new Button("="); 
    b16=new Button("/"); 
    b17=new Button("Reset"); 
    f.add(tf,"North"); //把文本区域添加到框架的上方 
    f.add(p,"Center"); //把面板加到框架的中间
    f.add(b17,"South"); //把按钮(Reset)添加到框架的下方 
    //把按钮添加到面版上
    p.add(b1); 
    p.add(b2); 
    p.add(b3); 
    p.add(b4); 
    p.add(b5); 
    p.add(b6); 
    p.add(b7); 
    p.add(b8); 
    p.add(b9); 
    p.add(b10); 
    p.add(b11); 
    p.add(b12); 
    p.add(b13); 
    p.add(b14); 
    p.add(b15); 
    p.add(b16); 
    //为按钮添加监听
    b1.addActionListener(this); 
    b2.addActionListener(this); 
    b3.addActionListener(this); 
    b4.addActionListener(this); 
    b5.addActionListener(this); 
    b6.addActionListener(this); 
    b7.addActionListener(this); 
    b8.addActionListener(this); 
    b9.addActionListener(this); 
    b10.addActionListener(this); 
    b11.addActionListener(this); 
    b12.addActionListener(this); 
    b13.addActionListener(this); 
    b14.addActionListener(this); 
    b15.addActionListener(this); 
    b16.addActionListener(this); 
    b17.addActionListener(this);
    f.addWindowListener(this); //为框架添加监听  
    f.setSize(300,200); //设置框架的大小
    f.setVisible(true); //设置框架为可见
    }  
    //监听程序
    public void actionPerformed(ActionEvent e){ 
    String s; 
    s=e.getActionCommand(); 
    //Returns the command string associated with this action.;
    //SWITCH开关 
    switch(s.charAt(0)){ //Returns the char value at the specified index.
    case '0': case '1': case '2': case '3': case '4': case '5': case '6': case 
    '7': case '8': case '9': //按了“0-9”,就执行下面
    if(resultIndicate){ 
    result=0; 
    data1=0; 
    prec='+'; 

    Integer Int1=new Integer(s); 
    if(radixPointIndicate){ 
    radixPointDepth=radixPointDepth/10; 
    data1=data1+(Int1.intValue())*radixPointDepth; 
    }
    else{ 
    data1=data1*10+(Int1.intValue()); 

    Double displayNumber=new Double(data1); 
    tf.setText(displayNumber.toString()); 
    resultIndicate=false; 
    break; 
    case '+': case '-':case '*':case '/':case '=': //按了“+、-、*、/”,就执行下面 
    if(s.charAt(0)!='='&&resultIndicate){ 
    prec=s.charAt(0); 
    resultIndicate=false; 
    }
    else{ 
    //用SWITCH开关运算出执行了“+、-、*、/”的结果
    switch(prec){ 
    case '+': 
    result=result+data1; 
    break; 
    case '-': 
    result=result-data1; 
    break; 
    case '*': 
    result=result*data1; 
    break; 
    case '/': 
    result=result/data1; 
    break; 


    radixPointIndicate=false; 
    radixPointDepth=1; 
    displayNumber=new Double(result); 
    tf.setText(displayNumber.toString()); 
    //监听是否按了“=” 
    if(s.charAt(0)!='='){ 
    data1=0; 
    prec=s.charAt(0); 
    }
    else{ 
    resultIndicate=true; 

    break; 
    case '.': 
    radixPointIndicate=true; 
    break; 

    //监听是否按了为“Reset”,是则对各数据Reset
    if(s.equals("Reset")) 

    result=0; 
    data1=0; 
    radixPointDepth=1; 
    tf.setText(""); 


    public void windowClosing(WindowEvent e){ 
    System.exit(0); 
    }
    }
      

  8.   


    package calculator;
    import java.awt.*; //引入包java.awt中所有的类  
    import java.awt.event.*; //引入包java.awt.event中所有的类public class Calculator extends WindowAdapter implements ActionListener{ //创建Calculator类,实现ActionListener接口
    private double result=0,data1=0,radixPointDepth=1; //定义变量
    private boolean radixPointIndicate=false,resultIndicate=false; 
    private char prec='+'; //创建优先默认字符"+" 
    private Frame f; //创建窗口 
    private TextField tf;//创建文本框
    private Button b1,b2,b3,b4,b5,b6,b7,b8,b9,b10,b11,b12,b13,b14,b15,b16,b17; //创建按钮 
    private Panel p; //创建/面板容器
    public static void main(String[] args){ //main方法,创建calGUI(图形用户界面),完成初始化
    //构造器
    Calculator de=new Calculator();//创建构造方法   
    de.go();
    }
    public void go(){
     
    f=new Frame("Calculator"); 
    p=new Panel(); //运算界面p 
    p.setLayout(new GridLayout(4,4));  // 设置p的布局为GridLayout,四行四列 

    tf=new TextField(30); 
    //实例化按钮 
    b1=new Button("7"); 
    b2=new Button("8"); 
    b3=new Button("9"); 
    b4=new Button("+"); 
    b5=new Button("4"); 
    b6=new Button("5"); 
    b7=new Button("6"); 
    b8=new Button("-"); 
    b9=new Button("1"); 
    b10=new Button("2"); 
    b11=new Button("3"); 
    b12=new Button("*"); 
    b13=new Button("0"); 
    b14=new Button("."); 
    b15=new Button("="); 
    b16=new Button("/"); 
    b17=new Button("Reset"); 
    f.add(tf,"North"); //把文本区域添加到框架的上方 
    f.add(p,"Center"); //把面板加到框架的中间
    f.add(b17,"South"); //把按钮(Reset)添加到框架的下方 
    //把按钮添加到面版上
    p.add(b1); 
    p.add(b2); 
    p.add(b3); 
    p.add(b4); 
    p.add(b5); 
    p.add(b6); 
    p.add(b7); 
    p.add(b8); 
    p.add(b9); 
    p.add(b10); 
    p.add(b11); 
    p.add(b12); 
    p.add(b13); 
    p.add(b14); 
    p.add(b15); 
    p.add(b16); 
    //为按钮添加监听
    b1.addActionListener(this); 
    b2.addActionListener(this); 
    b3.addActionListener(this); 
    b4.addActionListener(this); 
    b5.addActionListener(this); 
    b6.addActionListener(this); 
    b7.addActionListener(this); 
    b8.addActionListener(this); 
    b9.addActionListener(this); 
    b10.addActionListener(this); 
    b11.addActionListener(this); 
    b12.addActionListener(this); 
    b13.addActionListener(this); 
    b14.addActionListener(this); 
    b15.addActionListener(this); 
    b16.addActionListener(this); 
    b17.addActionListener(this);
    f.addWindowListener(this); //为框架添加监听  
    f.setSize(300,200); //设置框架的大小
    f.setVisible(true); //设置框架为可见
    }  
    //监听程序
    public void actionPerformed(ActionEvent e){ 
    String s; 
    s=e.getActionCommand(); 
    //Returns the command string associated with this action.;
    //SWITCH开关 
    switch(s.charAt(0)){ //Returns the char value at the specified index.
    case '0': case '1': case '2': case '3': case '4': case '5': case '6': case 
    '7': case '8': case '9': //按了“0-9”,就执行下面
    if(resultIndicate){ 
    result=0; 
    data1=0; 
    prec='+'; 

    Integer Int1=new Integer(s); 
    if(radixPointIndicate){ 
    radixPointDepth=radixPointDepth/10; 
    data1=data1+(Int1.intValue())*radixPointDepth; 
    }
    else{ 
    data1=data1*10+(Int1.intValue()); 

    Double displayNumber=new Double(data1); 
    tf.setText(displayNumber.toString()); 
    resultIndicate=false; 
    break; 
    case '+': case '-':case '*':case '/':case '=': //按了“+、-、*、/”,就执行下面 
    if(s.charAt(0)!='='&&resultIndicate){ 
    prec=s.charAt(0); 
    resultIndicate=false; 
    }
    else{ 
    //用SWITCH开关运算出执行了“+、-、*、/”的结果
    switch(prec){ 
    case '+': 
    result=result+data1; 
    break; 
    case '-': 
    result=result-data1; 
    break; 
    case '*': 
    result=result*data1; 
    break; 
    case '/': 
    result=result/data1; 
    break; 


    radixPointIndicate=false; 
    radixPointDepth=1; 
    displayNumber=new Double(result); 
    tf.setText(displayNumber.toString()); 
    //监听是否按了“=” 
    if(s.charAt(0)!='='){ 
    data1=0; 
    prec=s.charAt(0); 
    }
    else{ 
    resultIndicate=true; 

    break; 
    case '.': 
    radixPointIndicate=true; 
    break; 

    //监听是否按了为“Reset”,是则对各数据Reset
    if(s.equals("Reset")) 

    result=0; 
    data1=0; 
    radixPointDepth=1; 
    tf.setText(""); 


    public void windowClosing(WindowEvent e){ 
    System.exit(0); 
    }
    }
      

  9.   

    "比方说 我在文本框输入 (2+3)/3 
    其会读取并计算!"
    按编译原理处理,我说一下大体思路:
    1、用一个String接收这个输入进文本框的串
    2、分解成单个字符,具体可以用很多种方法。例如串转字符数组。
    3、依次扫描处理这些单个字符,假如出现括号,那么压栈配对,是计算符号,那么和前面的操作数计算……
      

  10.   

    我们自己写的话会有很多特殊情况考虑不到,建议楼主采用JFormula试试看。
    JFormula是一组数学表达式API,由JAPISoft开发,用于轻松进行各种不同的数学表达式计算。楼主可以把那个包下载下来研究一下!
    官方下载:http://www.japisoft.com/formula/
      

  11.   

    自己实现比较麻烦
    如果弄个表达式sin,cos怎么办
    以前我是调用JavaScript中的eval函数实现的
      

  12.   

    sin,cos用幂级数展开式来计算就可以了,就是麻烦点
    java提供了javax.script.AbstractScriptEngine
    里面有eval方法,可以参考。
      

  13.   

    sin,cos用幂级数展开式来计算就可以了,就是麻烦点
    =====================
    那你就幂级数展开吧,个个特殊处理