当前位置:文档之家› 科学计算器代码

科学计算器代码

Calculate.java/*这是一个计算类,用于普通运算中的各种运算,如2,8,10,16进制的符合运算,复合运算其原理用后缀表达式来实现*/import java.util.*;import ng.Math;class Calculate {//两个内部类来实现数据的存储,如同数据结构中的栈class operator{ char data[];int top;operator(){data = new char [50];top = -1;}};class operator1{ double data[];int top;operator1(){data = new double [20];top = -1;}};public static double jiecheng(double number)//阶乘运算{int number1 = (int)number;double j=1;for(int i=1;i<=number1;i++)j=j*i;return j;}String trans (char exp[],char postexp[])//后缀表达式的翻译函数{int i=0,j=0;boolean m=true;//m用来监视表达式是不有非法的运算符连着输入operator op = new operator();while(exp[j]!='\0'){ switch (exp[j]){case '(':op.top++;op.data[op.top]=exp[j];j++;m= false;break;case ')':while (op.data[op.top]!='('){ postexp[i++]=op.data[op.top];op.top--;}op.top--;j++;break;case '+':case '-':if(!m)return "+或者-附近出现多个运算符在一起的现象!";while (op.top!=-1 && op.data[op.top]!='('){postexp[i++]=op.data[op.top];op.top--;}op.top++;op.data[op.top]=exp[j];j++;m=false;break;case '*':case '/':if(!m)return "*或/附近出现多个运算符在一起的现象!";while(op.top!=-1 && op.data[op.top]!='('&& op.data[op.top]!='+'&& op.data[op.top]!='-'){postexp[i++]=op.data[op.top];op.top--;}op.top++;op.data[op.top]=exp[j];j++;m = false;break;case '^':if(!m)return "^(冥运算符号)附近出现多个运算符在一起的现象!";case 's':case 'c':case 't':case 'l':case '$':case '`':op.top++;op.data[op.top]=exp[j];j++;m = false;break;case '!':op.top++;op.data[op.top]=exp[j];j++;break;case ' ':break;default:while (exp[j]>='0' && exp[j]<='9'||exp[j]=='.'){postexp[i++]=exp[j];j++;}postexp[i++]='#';m = true;}}while (op.top!=-1){postexp[i++]=op.data[op.top];op.top--;}postexp[i]='\0';if(exp[j-1]=='+'||exp[j-1]=='-'||exp[j-1]=='*'||exp[j-1]=='/'||exp[j-1]=='^') return "运算式后缀出现非正常运算符!";return "ok";}String compvalue (char postexp[],int flag)//后缀表达式运算,flag为进制{String ex = new String(postexp);StringTokenizer analy = new StringTokenizer(ex,"#+-`*/sclt!^$");operator1 st = new operator1();double d,d1,d2,a,b,c;int j=0;st.top=-1;while (postexp[j]!='\0'){switch (postexp[j]){case '+':a=st.data[st.top];st.top--;b=st.data[st.top];c=a+b;st.data[st.top]=c;break;case '-':a=st.data[st.top];st.top--;b=st.data[st.top];c=b-a;st.data[st.top]=c;break;case '*':a=st.data[st.top];st.top--;b=st.data[st.top];c=a*b;st.data[st.top]=c;break;case '/':a=st.data[st.top];st.top--;b=st.data[st.top];st.top--;if (a!=0){ c=b/a;st.top++;st.data[st.top]=c;}else{ return "输入错误,除数不能是零!请重新输入!";}break;case 's':a=st.data[st.top];c=Math.sin(Math.toRadians(a));st.data[st.top]=c;break;case 'c':a=st.data[st.top];c=Math.cos(Math.toRadians(a));st.data[st.top]=c;break;case 't':a=st.data[st.top];if(a%180 == 90||(-a)%180==90)return "tan函数值有误!";c=Math.tan(Math.toRadians(a));st.data[st.top]=c;break;case '!':a=st.data[st.top];c=jiecheng(a);st.data[st.top]=c;break;case 'l':a=st.data[st.top];if(a<=0)return "log的真数不能小于等于0";c=Math.log(a);st.data[st.top]=c;break;case '^':a=st.data[st.top];st.top--;b=st.data[st.top];c=Math.pow(b,a);st.data[st.top]=c;break;case '$':a=st.data[st.top];if(a<0)return "开方数不能小于0!";c=Math.sqrt(a);st.data[st.top]=c;break;case '`':a=st.data[st.top];c=0-a;st.data[st.top]=c;break;default:String str =analy.nextToken();//获取后缀表达式的数据if(flag == 2)d = btod(str);else if(flag == 8)d = otod(str);else if(flag == 10)d=Double.parseDouble(str);elsed = htod(str);while (postexp[j]>='0' && postexp[j]<='9'||postexp[j]=='.'){j++;}st.top++;st.data[st.top]=d;break;}j++;}return (String.valueOf(st.data[st.top]));}String check(char c[])//检查函数,用来检查运算式中的低级错误{int k=0,i=0;if(c[0]=='+'||c[0]=='-'||c[0]=='^'||c[0]=='*'||c[0]=='/')return "表达式开始位置不对";while(c[k]!='\0'){if((c[k]>=39 && c[k]<='9'&&c[k]!=',') ||c[k]=='$'||c[k]=='!'||c[k]==' '||c[k]=='c'||c[k]=='s'||c[k]=='t'||c[k]=='^'||c[k]=='l'||c[k]=='`') {}else{return "输入含有非法的字符,请重新输入正确的数学表达式!";}if(c[k]=='(')i++;else if(c[k]==')')i--;k++ ;}if(i!=0){return "输入错误,没有相匹配的括弧!请重新输入!";}return "ok";}double btod(String str)//二进制到十进制的转换{int k = str.indexOf("."),m=0;double p=0.0;if(k ==-1)k =str.length();for(int i =k-1;i>=0;i--){char c = str.charAt(i);int a =Integer.parseInt(""+c);p=p+(a*Math.pow(2, m));m++;}m=-1;for(int i =k+1;i<str.length();i++){char c = str.charAt(i);int a =Integer.parseInt(""+c);p=p+(a*Math.pow(2, m));m--;}return p;}double otod(String str)//八进制到十进制的转换{int k = str.indexOf("."),m=0;double p=0.0;if(k ==-1)k =str.length();for(int i =k-1;i>=0;i--){char c = str.charAt(i);int a =Integer.parseInt(""+c);p=p+(a*Math.pow(8, m));m++;}m=-1;for(int i =k+1;i<str.length();i++){char c = str.charAt(i);int a =Integer.parseInt(""+c);p=p+(a*Math.pow(8, m));m--;}return p;}double htod(String str)//十六进制到十进制的转换{int k = str.indexOf("."),m=0;double p=0.0;if(k ==-1)k =str.length();for(int i =k-1;i>=0;i--){char c = str.charAt(i);int a =Integer.parseInt(""+c);p=p+(a*Math.pow(16, m));m++;}m=-1;for(int i =k+1;i<str.length();i++){char c = str.charAt(i);int a =Integer.parseInt(""+c);p=p+(a*Math.pow(16, m));m--;}return p;}}Main.java//主类public class Main {public static void main(String[] args) {new WinFrame("计算器");}}MyDialog.javaimport java.awt.event.*;import java.awt.*;//帮助文档的对话框类class MyDialog extends Dialog implements ActionListener //建立对话框类{ static final int YES=1,NO=0;int message=-1; Button yes,no;TextArea area;String help;MyDialog(Frame f,String s,boolean b) //构造方法{ super(f,s,b);yes=new Button("确定"); yes.addActionListener(this);no=new Button("取消"); no.addActionListener(this);setLayout(new FlowLayout());help = "使用帮助:\n";help +="本计算器运算方式采用输入整个运算式后进行运算的方式.\n";help +="但由于作者能力有限,对有些运算符进行了重定义,现列出以下运算符:\n";help +="s代表sin,c代表cos,t代表tan,!代表阶乘,l代表log\n";help +="`代表负号,如果要用键盘输入的时候,请正确输入\n";help +="本计算器有4大主要功能:\n";help +="1.进行符合运算,可以用键盘进行输入,执行结果可以按回车获得\n";help +="2.进行2,8,10,16进制的各种运算以及相互转换,16进制没有实现a,b,c,d,e,f的输入\n";help +="3.进行大整数运算\n4.进行批量运算";area = new TextArea(help,10,50,3);//area.setEnabled(false);area.setForeground(Color.BLUE);area.setFont(new Font("宋体",Font.BOLD,16));add(area);add(yes); add(no);yes.setPreferredSize(new Dimension(100,25));no.setPreferredSize(new Dimension(100,25));setBounds(300,300,500,300);addWindowListener(new WindowAdapter(){ public void windowClosing(WindowEvent e){ message=-1;setVisible(false);}});}public void actionPerformed(ActionEvent e){ if(e.getSource()==yes){ message=YES;setVisible(false);}else if(e.getSource()==no){ message=NO;setVisible(false);}}public int getMessage(){ return message;}}Mypanel.javaimport java.util.*;import java.awt.*;import javax.swing.*;class mypanel extends Panel//派生一个面板,用来实现不同面板的布局{GridLayout grid;JButton btu[];mypanel(String []btuName,int row,int col,Color c){btu = new JButton [btuName.length];grid = new GridLayout(row,col,1,1);setLayout(grid);for(int i=0;i<row;i++)for(int j=0;j<col;j++){if(i*col+j<btuName.length){btu[i*col+j] = new JButton(btuName[i*col+j]);btu[i*col+j].setForeground(c);add(btu[i*col+j]);btu[i*col+j].setPreferredSize(new Dimension(60,25));}elsebreak;}}}WinFrame.javaimport java.util.*;import java.awt.*;import java.awt.event.*;import javax.swing.*;import ng.Math;import java.math.*;class WinFrame extends Frame implements ActionListener,KeyListener,ItemListener {mypanel pane1,pane2,pane3,pane4,pane5;Label label1,label2;short j_flag=10;//进制运算的标示,初始化为十进制String strAsk="",strAnswer="";boolean b_flag = true;boolean p_flag = true;//批处理开关TextArea text1;//大整数输出专用域MenuBar menubar;Menu menu1,menu2,menu3;MenuItem item1,item2;MyDialog dialog;//帮助文档对话框CheckboxMenuItem menuitem1,menuitem2,menuitem3;String a[] = {"1","2","3",//用数组进行按钮的命名和消息内容的获取"4","5","6","7","8","9","0",".","+/-"};String a1[] = {"1","2","3","4","5","6","7","8","9","0",".","`"};String b[] ={"/","C","*","→","+","=","-","CE"};String c[] ={"sin","cos","tan","!","pow","sprt","log","pi"};String c1[] ={"s","c","t","!","^","$","l","p"};String d[] ={"( ",")"};String d1[] ={"(",")"};String e[] = {" A"," B"," C"," D"," E","F"};JTextField ask ,answer;char exp[];char postexp[];Checkbox box1,box2,box3,box4,box5;CheckboxGroup jin;String data = "0";//大整数储存数据TextArea text2,text3;int opear;CardLayout mycard;String answer1 = "";// 进制转换时的十进制答案储存Long answer2 = (long)0;//其他进制答案储存Panel pCenter;//添加一个容器装专用数据域void area()//初始化布局,只是不想在构造方法中的东西太多,看起来不方便{mycard = new CardLayout();pCenter = new Panel();pCenter.setLayout(mycard);setLayout(new FlowLayout());setBounds(60,60,450,290);menu1 = new Menu("查看");menu2 = new Menu("系统");menu3 = new Menu("帮助");menuitem1 = new CheckboxMenuItem("普通",true);menuitem2 = new CheckboxMenuItem("批处理",false);menuitem3 = new CheckboxMenuItem("大整数",false);item1 = new MenuItem("退出");item2 = new MenuItem("帮助");menuitem1.addItemListener(this);menuitem2.addItemListener(this);menuitem3.addItemListener(this);item1.addActionListener(this);item2.addActionListener(this);menubar = new MenuBar();menu1.add(menuitem1);menu1.add(menuitem2);menu1.addSeparator();//分隔符menu1.add(menuitem3);menu2.add(item1);menu3.add(item2);menuitem1.setShortcut(new MenuShortcut(KeyEvent.VK_P));menuitem2.setShortcut(new MenuShortcut(KeyEvent.VK_D)); menuitem3.setShortcut(new MenuShortcut(KeyEvent.VK_B)); item1.setShortcut(new MenuShortcut(KeyEvent.VK_Q)); item2.setShortcut(new MenuShortcut(KeyEvent.VK_F1));menubar.add(menu2);menubar.add(menu1);menubar.add(menu3);setMenuBar(menubar);dialog = new MyDialog(this,"帮助文档",true);pane1 = new mypanel(a,4,3,Color.red);pane3 = new mypanel(c,4,2,Color.black);pane2 = new mypanel(d,1,2,Color.black);pane4 = new mypanel(b,4,2,Color.BLUE);pane5 = new mypanel(e,1,6,Color.red);jin = new CheckboxGroup();box1 = new Checkbox("二进制",false,jin);box2 = new Checkbox("八进制",false,jin);box3 = new Checkbox("十进制",true,jin);box4 = new Checkbox("十六进制",false,jin);box5 = new Checkbox("大整数运算");pane3 = new mypanel(c,4,2,Color.black);for(int i=0;i<8;i++)pane3.btu[i].addActionListener(this);box1.addItemListener(this);box2.addItemListener(this);box3.addItemListener(this);box4.addItemListener(this);box5.addItemListener(this);for(int i=0;i<12;i++)pane1.btu[i].addActionListener(this);for(int i=0;i<2;i++)pane2.btu[i].addActionListener(this);for(int i=0;i<8;i++)pane4.btu[i].addActionListener(this);ask = new JTextField(38);ask.setHorizontalAlignment(JTextField.RIGHT);answer = new JTextField(38);ask.addKeyListener(this);answer.setHorizontalAlignment(JTextField.RIGHT); answer.setEditable(false);add(ask);add(answer);add(box5);add(box1);add(box2);add(box3);add(box4);text1= new TextArea("",6,10,3);text1.setForeground(Color.BLUE);pCenter.add("1",pane3);pCenter.add("2",text1);add(pCenter);add(pane1);add(pane4);add(pane2);add(pane5);label1 = new Label("请按格式输入批处理的数据,按空格输入下一个,回车输出答案。

相关主题