java新手练习:用Frame编写一个可以识别二级运算和括号优先运算的计算器
Posted 迷茫傻逼
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java新手练习:用Frame编写一个可以识别二级运算和括号优先运算的计算器相关的知识,希望对你有一定的参考价值。
废话少说直接上代码,希望看过的朋友能和我多交流,,谢谢
package Myjisuanqi;
import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.Panel;
import java.awt.TextField;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
public class mainFrame03 extends Frame implements MouseListener{
private String first;
private String second;
private String str = "";
private String operator;
private TextField input = new TextField();
private Button kuohao1 = new Button("(");
private Button kuohao2 = new Button(")");
private Button one = new Button("1");
private Button two = new Button("2");
private Button three = new Button("3");
private Button add = new Button("+");
private Button four = new Button("4");
private Button five = new Button("5");
private Button six = new Button("6");
private Button jian = new Button("-");
private Button seven = new Button("7");
private Button eight = new Button("8");
private Button nine = new Button("9");
private Button cheng = new Button("*");
private Button AC = new Button("清零");
private Button zero = new Button("0");
private Button equal = new Button("=");
private Button chu = new Button("/");
private Button dian = new Button(".");
public static void main(String args[]){
init();
}
private void init(){
Frame f = new Frame("梁洋洋制作的计算器3.0--基本可以实现简单计算器的所有功能,包括识别二级运算和括号");
f.add("North",input);
Panel keys = new Panel();
f.add("Center",keys);
keys.setLayout(new GridLayout(4,4));
keys.add(one);
keys.add(two);
keys.add(three);
keys.add(add);
keys.add(four);
keys.add(five);
keys.add(six);
keys.add(jian);
keys.add(seven);
keys.add(eight);
keys.add(nine);
keys.add(cheng);
keys.add(dian);
keys.add(zero);
keys.add(equal);
keys.add(chu);
Panel kuohao = new Panel();
kuohao.add(kuohao1);
kuohao.add(AC);
kuohao.add(kuohao2);
f.add("South", kuohao);
zero.addMouseListener(this);
one.addMouseListener(this);
two.addMouseListener(this);
three.addMouseListener(this);
four.addMouseListener(this);
five.addMouseListener(this);
six.addMouseListener(this);
seven.addMouseListener(this);
eight.addMouseListener(this);
nine.addMouseListener(this);
add.addMouseListener(this);
jian.addMouseListener(this);
cheng.addMouseListener(this);
chu.addMouseListener(this);
equal.addMouseListener(this);
dian.addMouseListener(this);
AC.addMouseListener(this);
kuohao1.addMouseListener(this);
kuohao2.addMouseListener(this);
f.setLocation(600,0);
f.setSize(300, 250);
f.setVisible(true);
}
@Override
public void mouseClicked(MouseEvent e) {
Button btn = (Button)e.getSource();
if(btn != equal){
str = str+btn.getLabel();
input.setText(str);
}
if(btn == equal){
try {
input.setText(opt(str)+"");
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
public static float opt(String s) throws Exception{
if(s == null || "".equals(s.trim())) {
return 0f;
}
int a1=s.indexOf("+");
int a2=s.indexOf("-");
int a3=s.indexOf("*");
int a4=s.indexOf("/");
int a5=s.indexOf("(");
if(a1==-1&&a2==-1&&a3==-1&&a4==-1){
if(s.trim()==null||"".equals(s.trim())){
throw new Exception("operate error");
}
return Float.parseFloat(s.trim());
}
if(a5!=-1){
int a6=s.indexOf(")");
if(a6==-1){
throw new Exception("括号不匹配");
}else{
float f=opt(s.substring(a5+1,a6).trim());
s=s.replace(s.substring(a5,a6+1), String.valueOf(f));
return opt(s);
}
}
if(a1!=-1){
return opt(s.substring(0,a1))+opt(s.substring(a1+1,s.length()));
}
if(a2!=-1){
return opt(s.substring(0,a2))-opt(s.substring(a2+1,s.length()));
}
if(a3!=-1){
return opt(s.substring(0,a3))*opt(s.substring(a3+1,s.length()));
}
if(a4!=-1){
return opt(s.substring(0,a4))/opt(s.substring(a4+1,s.length()));
}
return Integer.parseInt(s.trim());
}
@Override
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
}
2017-05-18
以上是关于java新手练习:用Frame编写一个可以识别二级运算和括号优先运算的计算器的主要内容,如果未能解决你的问题,请参考以下文章