java 简单计算器设计_JAVA——简单科学计算器设计(示例代码)

packageCalculator_JCoder;import java.awt.*;importjava.awt.event.ActionEvent;importjava.awt.event.ActionListener;importjava.awt.event.KeyEvent;importjava.awt.event.KeyListener;importjava.text.DecimalFormat;import javax.swing.*;import java.util.*;import java.math.*;public class Calculator extends JFrame implementsActionListener {static Font F = new Font("宋体",Font.BOLD,25);static Font _F = new Font("宋体",Font.BOLD,40);static DecimalFormat DF = new DecimalFormat("0.0000000000");static String IN = "";static JTextField Text_Res = new JTextField("0");static JTextField Text_Now = new JTextField("");static JButton Keys[] = new JButton[21];static String KeysName[] ={"7", "8", "9", "4", "5", "6", "1", "2","3", "0", ".", "+", "-", "*", "/", "=","CE", "(", ")", "^", "C"};static int CMP(chara){if(a == ‘#‘){return 0;}if(a == ‘(‘){return 1;}if(a == ‘+‘ || a == ‘-‘){return 3;}if(a == ‘*‘ || a == ‘/‘){return 4;}if(a == ‘^‘){return 5;}if(a == ‘)‘){return 6;}return -1;

}staticString Change(String in){

in+= "#";int L =in.length();

String t= "";

String NPR= "";

Stack S = newStack();for(int i = 0;i < L;i ++){if(in.charAt(i) >= ‘0‘ && in.charAt(i) <= ‘9‘){t +=in.charAt(i);}else if(in.charAt(i) == ‘.‘){t +=in.charAt(i);}else if(in.charAt(i) == ‘#‘){if(t.length() != 0){NPR += t + " ";}

t= "";

}else if(in.charAt(i) == ‘+‘ || in.charAt(i) == ‘-‘ || in.charAt(i) == ‘*‘ || in.charAt(i) == ‘/‘ || in.charAt(i) == ‘^‘){if(t.length() != 0){NPR += t + " ";}

t= "";if(S.size() == 0){

S.push(in.charAt(i));

}else if(CMP(S.peek())

S.push(in.charAt(i));

}else if(CMP(S.peek()) >=CMP(in.charAt(i))){

NPR+= S.peek() + " ";

S.pop();

S.push(in.charAt(i));

}

}else if(in.charAt(i) == ‘(‘){if(t.length() != 0){NPR += t + " ";}

t= "";

S.push(in.charAt(i));

}else if(in.charAt(i) == ‘)‘){if(t.length() != 0){NPR += t + " ";}

t= "";while(S.peek() != ‘(‘){

NPR+= S.peek() + " ";

S.pop();

}

S.pop();

}

}while(S.size() != 0){

NPR+= S.peek() + " ";

S.pop();

}//System.out.println(NPR);

returnNPR;

}static double Solve(double a,double b,charc){double out = 0.0;if(c == ‘+‘){

out= a +b;

}else if(c == ‘-‘){

out= a -b;

}else if(c == ‘*‘){

out= a *b;

}else if(c == ‘/‘){

out= a /b;

}else if(c == ‘^‘){

out=Math.pow(a,b);

}returnout;

}static doubleCal(String now){

String Sp[]= now.split("\ ");

Stack S = newStack();for(int i = 0;i < Sp.length;i ++){if(Sp[i].length() == 0){continue;}if(Sp[i].charAt(0) <= ‘9‘ && Sp[i].charAt(0) >= ‘0‘){

S.push(Double.valueOf(Sp[i]));

}else{double b =S.peek();S.pop();double a =S.peek();S.pop();double c = Solve(a,b,Sp[i].charAt(0));

S.push(c);

}

}double ans =S.peek();returnans;

}static String SetS(intx){if(x == 1){return "+";}if(x == 2){return "-";}if(x == 3){return "*";}if(x == 4){return "/";}return "0";

}public voidinit() {

JPanel KeysP= newJPanel();

KeysP.setLayout(null);

KeysP.setSize(500,500);for(int i = 0;i <= 20;i ++){

Keys[i]= newJButton(KeysName[i]);

KeysP.add(Keys[i]);

Keys[i].setFont(F);

}

Keys[0].setBounds(20,20,60,60);

Keys[1].setBounds(85,20,60,60);

Keys[2].setBounds(150,20,60,60);

Keys[3].setBounds(20,85,60,60);

Keys[4].setBounds(85,85,60,60);

Keys[5].setBounds(150,85,60,60);

Keys[6].setBounds(20,150,60,60);

Keys[7].setBounds(85,150,60,60);

Keys[8].setBounds(150,150,60,60);

Keys[9].setBounds(20,215,125,60);

Keys[10].setBounds(150,215,60,60);

Keys[11].setBounds(215,20,60,60);

Keys[12].setBounds(280,20,60,60);

Keys[13].setBounds(215,85,60,60);

Keys[14].setBounds(280,85,60,60);

Keys[15].setBounds(215,150,125,60);

Keys[16].setBounds(215,215,125,60);

Keys[17].setBounds(345,20,60,60);

Keys[18].setBounds(345,85,60,60);

Keys[19].setBounds(345,150,60,60);

Keys[20].setBounds(345,215,60,60);

Text_Res.setHorizontalAlignment(JTextField.RIGHT);

Text_Now.setHorizontalAlignment(JTextField.RIGHT);

Text_Res.setEditable(false);

Text_Now.setEditable(false);

Text_Res.setBackground(Color.WHITE);

Text_Now.setBackground(Color.WHITE);

JPanel TextP= newJPanel();

TextP.setLayout(null);

TextP.setSize(500,100);

TextP.add(Text_Res);

Text_Res.setBounds(20, 60, 385, 60);

TextP.add(Text_Now);

Text_Now.setBounds(20, 20, 385, 40);

Text_Res.setFont(_F);

Text_Now.setFont(F);

JPanel BigP= newJPanel();

BigP.setSize(800,600);

BigP.setLayout(null);

BigP.add(KeysP);

BigP.add(TextP);

KeysP.setBounds(0, 120, 600, 600);

TextP.setBounds(0, 0, 500, 200);

getContentPane().setLayout(null);

getContentPane().add(BigP);for (int i = 0; i <= 20; i ++) {

Keys[i].addActionListener(this);

}

}public voidactionPerformed(ActionEvent e) {

String Get=e.getActionCommand();if("0123456789.+-*/^()".indexOf(Get) >= 0){

IN= IN +Get;

Text_Now.setText(IN);

}else if("=".indexOf(Get) >= 0){double show =Cal(Change(IN));//System.out.println(show);//System.out.println(show);

String t1 =String.valueOf(show);

String t2=String.valueOf(DF.format(show));

Text_Res.setText(t1);

IN=t2;

}else if("CE".compareTo(Get) == 0){int L =IN.length();if(L != 0){IN = IN.substring(0,L - 1);}

Text_Now.setText(IN);

}else if("C".compareTo(Get) == 0){

IN= "";

Text_Now.setText(IN);

Text_Res.setText("0");

}

}publicCalculator(){super();

init();this.setTitle("Calculator By-J_Coder");this.setResizable(false);this.setLocation(100,100);this.setSize(440,450);

}public static voidmain(String[] args) {

Calculator window= newCalculator();

window.setVisible(true);

window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值