java编写加减乘除 优先级_跪求大神帮忙,怎样在java 计算器中实现,四则运算优先级;...

该楼层疑似违规已被系统折叠 隐藏此楼查看此楼

代码如下,跪求大神补充

import java.awt.*;

import java.awt.event.*;

import java.awt.image.BufferedImage;

import java.io.File;

import java.io.IOException;

import javax.imageio.ImageIO;

import javax.swing.JFrame;

import javax.swing.JOptionPane;

public class Cal {

public static void main(String[] args) {

CalFrame f = new CalFrame();

}

}

class CalFrame extends Frame {

double d1, d2;

int op = -1;

TextField tf;

CalPanelL p1;

CalPanelR p2;

BufferedImage bfimage=null;

// Constructor

CalFrame() {

super("Small Calculator");

setLayout(new FlowLayout(FlowLayout.CENTER,8,10));

setBackground(new Color(100,150,150));

setForeground(Color.white);

setResizable(false);

setSize(300,200);

try{

bfimage=ImageIO.read(new File("Blue hills.jpg"));

}

catch(IOException e){

e.printStackTrace();

}

tf = new TextField(22);

tf.setEditable(false);

tf.setBackground(new Color(108,118,103));

tf.setForeground(Color.white);

tf.setFont(new Font("Arial",Font.BOLD,16));

add(tf);

p1 = new CalPanelL();

p2 = new CalPanelR();

add(p1);

add(p2);

setVisible(true);

addWindowListener(new Wclose());

}

public void paint(Graphics g){

g.drawImage(bfimage,0,0,this);

}

// inner class:CalButton

class CalButton extends Button {

CalButton(String s){

super(s);

setBackground(Color.gray);

}

}

// inner class: CalPanelL

class CalPanelL extends Panel {

CalButton b0, b1, b2, b3,

b4, b5, b6, b7,

b8, b9, bPN, bPoint;

CalPanelL() {

setLayout(new GridLayout(4,3));

setFont(new Font("TimesRoman",Font.BOLD,16));

b0 = new CalButton("0");

b1 = new CalButton("1");

b2 = new CalButton("2");

b3 = new CalButton("3");

b4 = new CalButton("4");

b5 = new CalButton("5");

b6 = new CalButton("6");

b7 = new CalButton("7");

b8 = new CalButton("8");

b9 = new CalButton("9");

bPN = new CalButton("+/-");

bPoint = new CalButton(".");

// 加入按钮

add(b7); b7.addActionListener(new PressB7());

add(b8); b8.addActionListener(new PressB8());

add(b9); b9.addActionListener(new PressB9());

add(b4); b4.addActionListener(new PressB4());

add(b5); b5.addActionListener(new PressB5());

add(b6); b6.addActionListener(new PressB6());

add(b1); b1.addActionListener(new PressB1());

add(b2); b2.addActionListener(new PressB2());

add(b3); b3.addActionListener(new PressB3());

add(b0); b0.addActionListener(new PressB0());

add(bPN); bPN.addActionListener(new PressBPN()); ;

add(bPoint); bPoint.addActionListener(new PressBPoint());

}

}

class CalPanelR extends Panel {

CalButton bAdd, bSub, bMul, bDiv,

bSqrt, b1chux, bFac, bYx,

bLn, bEqual, bCE, bBack;

CalPanelR() {

setLayout(new GridLayout(4,3));

setFont(new Font("TimesRoman",Font.BOLD,16));

bAdd = new CalButton("+");

bSub = new CalButton("-");

bMul = new CalButton("*");

bDiv = new CalButton("/");

bSqrt = new CalButton("sqrt");

b1chux = new CalButton("1/x");

bFac = new CalButton("!");

bYx = new CalButton("y^x");

bLn = new CalButton("ln");

bEqual = new CalButton("=");

bCE = new CalButton("CE");

bBack = new CalButton("

add(bDiv); bDiv.addActionListener(new PressBDiv());

add(bSqrt); bSqrt.addActionListener(new PressBSqrt());

add(bLn); bLn.addActionListener(new PressBLn());

add(bMul); bMul.addActionListener(new PressBMul());

add(b1chux); b1chux.addActionListener(new PressB1chux());

add(bBack); bBack.addActionListener(new PressBBack());

add(bSub); bSub.addActionListener(new PressBSub());

add(bFac); bFac.addActionListener(new PressBFac());

add(bCE); bCE.addActionListener(new PressBCE());

add(bAdd); bAdd.addActionListener(new PressBAdd());

add(bYx); bYx.addActionListener(new PressBYx());

add(bEqual); bEqual.addActionListener(new PressBEqual());

}

}

class PressBAdd implements ActionListener {

public void actionPerformed(ActionEvent e) {

try {

d1 = Double.parseDouble(tf.getText());

op = 0;

tf.setText("");

} catch(Exception ee) {}

}

}

class PressBSub implements ActionListener {

public void actionPerformed(ActionEvent e) {

try {

d1 = Double.parseDouble(tf.getText());

op = 1;

tf.setText("");

} catch(Exception ee) {}

}

}

class PressBMul implements ActionListener {

public void actionPerformed(ActionEvent e) {

try {

d1 = Double.parseDouble(tf.getText());

op = 2;

tf.setText("");

} catch(Exception ee) {}

}

}

class PressBDiv implements ActionListener {

public void actionPerformed(ActionEvent e) {

try {

d1 = Double.parseDouble(tf.getText());

op = 3;

tf.setText("");

} catch(Exception ee) {}

}

}

class PressBYx implements ActionListener {

public void actionPerformed(ActionEvent e) {

try {

d1 = Double.parseDouble(tf.getText());

op = 4;

tf.setText("");

} catch(Exception ee) {}

}

}

class PressBEqual implements ActionListener {

public void actionPerformed(ActionEvent e) {

try {

double result = 0;

d2 = Double.parseDouble(tf.getText());

switch(op) {

case 0:

result = d1 + d2;

break;

case 1:

result = d1 - d2;

break;

case 2:

result = d1 * d2;

break;

case 3:

result = d1 / d2;

break;

case 4:

result = Math.pow(d1,d2);

break;

default:

}

if(d1!=0) tf.setText(String.valueOf(result));

else result=d2;

}catch(Exception ee) {}

}

}

class PressBSqrt implements ActionListener {

public void actionPerformed(ActionEvent e) {

try {

double x = Double.parseDouble(tf.getText());

double y;

y = Math.sqrt(x);

tf.setText(y+"");

} catch(Exception ee) {}

}

}

class PressBLn implements ActionListener {

public void actionPerformed(ActionEvent e) {

try {

double x = Double.parseDouble(tf.getText());

double y;

y = Math.log(x);

tf.setText(y+"");

} catch(Exception ee) {}

}

}

class PressB1chux implements ActionListener {

public void actionPerformed(ActionEvent e) {

try {

double x = Double.parseDouble(tf.getText());

double y;

if(x!=0)

{

y=1.0/x;

tf.setText(y+"");

}

else

{

tf.setText("输入数据等于零不能求倒数");

}

} catch(Exception ee) {}

}

}

class PressBFac implements ActionListener {

public void actionPerformed(ActionEvent e) {

try {

double x = Double.parseDouble(tf.getText());

if(x>0){

x=(int)x;

int i=1;

long sum=1;

for(i=1;i<=x;i++){

sum=sum*i;

}

tf.setText(sum+"");

}

else if(x==0){

long sum=1;

tf.setText(sum+"");

}

else{

tf.setText("输入数据小于零不能求阶乘");

}

} catch(Exception ee) {}

}

}

class PressBBack implements ActionListener {

public void actionPerformed(ActionEvent e) {

try {

String text = tf.getText();

text = text.substring(0,text.length()-1);

tf.setText(text);

} catch(Exception ee) {}

}

}

class PressBCE implements ActionListener {

public void actionPerformed(ActionEvent e) {

tf.setText("");

}

}

class PressBPN implements ActionListener {

public void actionPerformed(ActionEvent e) {

try {

String text = tf.getText();

if (text != "") {

if(text.charAt(0) == *-*)

tf.setText(text.substring(1));

else if(text.charAt(0) >= *0* && text.charAt(0) <= *9*)

tf.setText("-"+text.substring(0));

else if(text.charAt(0) == *.*)

tf.setText("-0"+text.substring(0));

}

} catch(Exception ee) { }

}

}

class PressBPoint implements ActionListener {

public void actionPerformed(ActionEvent e) {

String text = tf.getText();

if(text.lastIndexOf(".") == -1)

tf.setText(text+".");

}

}

class PressB0 implements ActionListener {

public void actionPerformed(ActionEvent e) {

String text = tf.getText();

tf.setText(text+"0");

}

}

class PressB1 implements ActionListener {

public void actionPerformed(ActionEvent e) {

String text = tf.getText();

tf.setText(text+"1");

}

}

class PressB2 implements ActionListener {

public void actionPerformed(ActionEvent e) {

String text = tf.getText();

tf.setText(text+"2");

}

}

class PressB3 implements ActionListener {

public void actionPerformed(ActionEvent e) {

String text = tf.getText();

tf.setText(text+"3");

}

}

class PressB4 implements ActionListener {

public void actionPerformed(ActionEvent e) {

String text = tf.getText();

tf.setText(text+"4");

}

}

class PressB5 implements ActionListener {

public void actionPerformed(ActionEvent e) {

String text = tf.getText();

tf.setText(text+"5");

}

}

class PressB6 implements ActionListener {

public void actionPerformed(ActionEvent e) {

String text = tf.getText();

tf.setText(text+"6");

}

}

class PressB7 implements ActionListener {

public void actionPerformed(ActionEvent e) {

String text = tf.getText();

tf.setText(text+"7");

}

}

class PressB8 implements ActionListener {

public void actionPerformed(ActionEvent e) {

String text = tf.getText();

tf.setText(text+"8");

}

}

class PressB9 implements ActionListener {

public void actionPerformed(ActionEvent e) {

String text = tf.getText();

tf.setText(text+"9");

}

}

// class: Wclose

class Wclose extends WindowAdapter {

public void windowClosing(WindowEvent e) {

System.exit(0);

}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值