java 计算器

来源:互联网 发布:lua ide vscode 编辑:程序博客网 时间:2024/06/01 08:01

突然想到了小学的时候使用的简单计算器 ,而且刚刚做了nyoj35 表达式求值这道题 

就想着用java实现一下。

main函数

package calculator;public class Main {public static void main(String args[]){new Fram();}}

界面+事件:

package calculator;import java.awt.*;import java.awt.event.*;import javax.swing.*;public class Fram extends JFrame implements ActionListener{JTextField show;JButton button[];JPanel panel;Cal_str cal;boolean flag=false;int x=-60,y=0;String anniu1[]={"(",")","清除","退格"};String anniu2="789/456*123-.0=+";Fram(){button=new JButton[26];panel=new JPanel();panel.setLayout(null);for(int i=0;i<20;i++){if(i==4||i==8||i==12||i==16){x=-60;y+=35;}if(i<4){button[i]=new JButton(anniu1[i]);button[i].setBounds(x+=62,y,60,35);}else{char ch=anniu2.charAt(i-4);button[i]=new JButton(String.valueOf(ch));button[i].setBounds(x+=62,y,60,35);}button[i].addActionListener(this);panel.add(button[i]);}add(panel,BorderLayout.CENTER);show=new JTextField(5);add(new JScrollPane(show),BorderLayout.NORTH);setResizable(false);setVisible(true);setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);setBounds(340,270,256,230);setTitle("计算器");}@Overridepublic void actionPerformed(ActionEvent e) {// TODO Auto-generated method stubJButton anniu=(JButton)e.getSource();if(flag){show.setText(null);flag=false;}if(anniu.getLabel().equals("=")){show.setText(show.getText()+anniu.getLabel());char []str=show.getText().toCharArray();cal=new Cal_str(str,show);flag=true;}else if(anniu.getLabel().equals("清除")){show.setText(null);}else if(anniu.getLabel().equals("退格")){char []str=show.getText().toCharArray();if(str.length>0)show.setText(new String(str,0,str.length-1));}else{show.setText(show.getText()+anniu.getLabel());}}}
计算:

package calculator;import java.util.*;import java.awt.*;import javax.swing.*;public class  Cal_str{Cal_str(char[] str,JTextField show){char []temp = new char[100];Stack<Character> optr=new Stack<Character>();Stack<Double> opnd=new Stack<Double>();int k=0,test=-1,count=0;optr.clear();opnd.clear();optr.add('=');try{for(int i=0;i<str.length;){test=i;if(i==test)count++;if(count>=20){break;}if(Character.isDigit(str[i])||str[i]=='.'){temp[k++]=str[i];i++;continue;}if(k!=0){opnd.add(Double.parseDouble(new String(temp,0,k)));k=0;}switch(compare(optr.peek(),str[i])){case 60:optr.add(str[i]);i++;break;case 61:optr.pop();i++;break;case 62:opnd.add(result(opnd.pop(),opnd.pop(),optr.pop()));break;}}if(count<20)show.setText(opnd.pop().toString());elseJOptionPane.showMessageDialog(show, "输入错误,请重新输入!");}catch(Exception e){JOptionPane.showMessageDialog(show, "输入错误,请重新输入!");}}private static Double result(Double num1, Double num2, char ch) {//计算// TODO Auto-generated method stubif(ch=='+')return num2+num1;if(ch=='-')return num2-num1;if(ch=='*')return num2*num1;if(ch=='/')return num2/num1;return null;}private static int compare(char ch1,char ch2){//返回运算符间关系String oper[]={">><<<>>",">><<<>>",">>>><>>",">>>><>>","<<<<<= ",">>>> >>","<<<<< ="};int x,y;x=to_num(ch1);y=to_num(ch2);return (int)(oper[x].charAt(y));}private static int to_num(char ch) {if(ch=='+')return 0;else if(ch=='-')return 1;else if(ch=='*')return 2;else if(ch=='/')return 3;else if(ch=='(')return 4;else if(ch==')')return 5;else if(ch=='=')return 6;else return -1;}}


刚刚又优化了一下 在计算时候如果循环20次不运行就会判错。

已经在nyoj35AC请放心。。



1 0