1个回答
展开全部
package mycode;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Calculator extends JFrame{
static private int MAX_NUMBER_LENGTH = 21;
static private int PRIORITY_HIGHEST = 10;
static private int PRIORITY_HIGHER = 9;
static private int PRIORITY_HIGH = 6;
static private int PRIORITY_MIDDLE = 5;
static private int PRIORITY_LOW= 4;
static private int PRIORITY_LOWER = 2;
static private int PRIORITY_LOWEST = 1;
private JTextField t_result; //显示栏
private JPanel p_main; //盛放所有按钮的面板
private JPanel p_num; //数字按钮面板
private JPanel p_oper; //运算符按钮面板
private JPanel p_show; //
private JButton b_num[]; //数字按钮
private JButton b_oper[]; //运算符按钮
/**
* 构造函数
*/
public Calculator(){
super("Calculator");
t_result = new JTextField(" 0.0", MAX_NUMBER_LENGTH);
t_result.setEditable(false);
t_result.setBorder(BorderFactory.createLineBorder(Color.BLUE,3));
p_show = new JPanel();
p_main = new JPanel();
p_num = new JPanel();
p_oper = new JPanel();
p_show.add(t_result);
p_main.setLayout(new BorderLayout());
p_num.setLayout(new GridLayout(4, 3, 1, 1));
p_oper.setLayout(new GridLayout(4, 3, 1, 1));
//数字按钮
b_num = new JButton[12];
for(int i=0; i<10; i++)
b_num[i] = new JButton(new Integer(i+1).toString());
b_num[9] = new JButton("0");
b_num[10] = new JButton("cls");
b_num[11] = new JButton(".");
//操作符按钮
b_oper = new JButton[8];
b_oper[0] = new JButton("+");
b_oper[1] = new JButton("-");
b_oper[2] = new JButton("*");
b_oper[3] = new JButton("/");
b_oper[4] = new JButton("pow");
b_oper[5] = new JButton("sqrt");
b_oper[6] = new JButton("+/-");
b_oper[7] = new JButton("=");
//将按钮放入面板
for(int i=0; i<12; i++)
p_num.add(b_num[i]);
for(int i=0; i<8; i++)
p_oper.add(b_oper[i]);
Container contentPane = getContentPane();
contentPane.add(t_result, BorderLayout.NORTH);
contentPane.add(p_num, BorderLayout.WEST);
contentPane.add(p_oper, BorderLayout.EAST);
//初始化
setSize(400, 400);
setResizable(false);
pack();
setVisible(true);
//注册监听器
ButtonListener bl = new ButtonListener();
for(int i=0; i<12; i++)
b_num[i].addActionListener(bl);
for(int i=0; i<8; i++)
b_oper[i].addActionListener(bl);
}//end constructor
class ButtonListener implements ActionListener{
// 内部类ButtonListener继承ActionListener响应按钮
private String lastOp; //记录前一个运算符
private String strVal; //字符化的计算数值
private double number; //新输入或临时存放数值
private double total; //记录计算数值
private boolean opPressed; //判断operator是否按下
private double numStack[]; //数字堆栈
private String operStack[]; //符号堆栈
private int numPoint; //数字堆栈指针
private int operPoint; //符号堆栈指针
ButtonListener(){
numStack = new double[MAX_NUMBER_LENGTH/2];
operStack = new String[MAX_NUMBER_LENGTH/2];
numPoint = 0;
operPoint = 0;
opPressed = false;
strVal = "";
number = 0.0;
total = 0.0;
System.out.println("construtor!!");
}
//用于检查Error的函数,查看各个参数的值
void checkValues(){
System.out.println("| --------Values check out----------------");
System.out.println("| opPressed: " + opPressed);
System.out.println("| lastOp: " + lastOp);
System.out.println("| strVal: " + strVal);
System.out.println("| number: " + number);
System.out.println("| total: " + total);
System.out.println("| ----------------------------------------");
}
public void actionPerformed(ActionEvent ae){
String s = ((JButton)ae.getSource()).getActionCommand().trim();
if( Character.isDigit(s.charAt(0)) )
handleNumber(s);
else
calculate(s);
}
/*
* 处理数字
* */
void handleNumber(String s){
if( !opPressed ){ //没按下运算符按钮则为数字
strVal += s;
t_result.setText(strVal);
checkValues();
}
else{
//当按下运算符时,清除strVal并存入新输入数值的第一个数值
opPressed = false;
//lastOp = null;
strVal = s;
t_result.setText(strVal);
checkValues();
}
}
/**/
boolean isUnaryOp(String op){
return ( op.equals("sqrt") || op.equals("=") ||op.equals("cls") ||
op.equals("+/-") || op.equals("."));
}
/*运算符等级
* 一元运算符 +/-,= ,.,sqrt, cls 为1级
* 二元中pow为2级,*,/为3级,+,-为4级
* */
int operatorPriorityLevel( String op ){
int opPriLevel;
if(isUnaryOp(op)){
opPriLevel = PRIORITY_HIGHEST;
}
else {
if(op.equals("pow")){
opPriLevel = PRIORITY_HIGHER;
}
else {
if(op.equals("*") || op.equals("/")){
opPriLevel = PRIORITY_MIDDLE;
}
else //if(op.equals("+") || op.equals("-"))
{
opPriLevel = PRIORITY_LOW;
}
}
}
return opPriLevel;
}
/* 运算符包括一元(UnaryOp)和二元(BinaryOp)
一元有sqrt,cls,=,+/-,.(小数点)
二元有+,-,*,/,pow
*/
void calculate(String op){
opPressed = true; //运算符被按下
number = getNumberOnDisplay(); //将新输入的数由显示器上的转存入number
if(isUnaryOp(op))
handleUnaryOp(op);
else
handleBinaryOp(op);
}
void handleDecPoint(){
opPressed = false; //事实上小数点不算运算符,在这里我把它作为一种特殊的符号处理
if( strVal.indexOf(".")<0 ){
strVal += ".";
}
t_result.setText(strVal);
checkValues();
}
/* =号较为特殊,且用的频率较高
如果有=号,意味着一次预算的结束
所以opPressed要清零
*/
void handleUnaryOp( String op ){
if( op.equals("=") ){ //处理=号的情况
//如果前一预算符是二元运算符
if(lastOp.equals("+"))
total += number;
else if(lastOp.equals("-"))
total -= number;
else if(lastOp.equals("*"))
total *= number;
else if(lastOp.equals("/")){
try{
total /= number;
}catch(ArithmeticException ae){
//除0错误!!
t_result.setText("Error!");
}
}
else if(lastOp.equals("pow")){
total = Math.pow(total, number);
}
if(operPoint == 0){
strVal = new Double(total).toString();
t_result.setText(strVal);
opPressed = false;
numPoint = 0;
operPoint = 0;
lastOp = null;
}
else{ //堆栈中还有压有运算符
number = total;
lastOp = operStack[--operPoint];
total = numStack[--numPoint];
calculate(op);
}
checkValues();
}//end if op.equals("=")
else if( op.equals("sqrt") ){ //处理sqrt的情况
number = Math.sqrt(number);
strVal = new Double(number).toString();
t_result.setText(strVal);
}
else if(op.equals("+/-")){ //处理+/-的情况
number = -number;
strVal = new Double(number).toString().trim();
t_result.setText(strVal);
}
else if(op.equals(".")){ //处理小数点
checkValues();
handleDecPoint();
}
else if(op.equals("cls")){ //清盘处理!
clear();
}
checkValues();
}
void handleBinaryOp(String op){
//计算二元运算符并且opPressed=true
if(lastOp != null){
//前面有运算符
if(operatorPriorityLevel(lastOp) > operatorPriorityLevel(op)
|| operatorPriorityLevel(lastOp) == operatorPriorityLevel(op)){
//前一个运算符优先级大于或等于现在的
if( lastOp.equals("+") )
total += number;
else if(lastOp.equals("-"))
total -= number;
else if(lastOp.equals("*"))
total *= number;
else if(lastOp.equals("/")){
try{
total /= number;
}catch(ArithmeticException ae){
//除0错误!!
t_result.setText("Error!");
}
}
else if(lastOp.equals("pow")){
total = Math.pow(total, number);
}
number = total;
if(operPoint != 0){ //堆栈中还有压有运算符
lastOp = operStack[--operPoint];
total = numStack[--numPoint];
calculate(op);
}
strVal = (new Double(total)).toString().trim();
t_result.setText(strVal);
lastOp = op;
}
else{//现行运算符优先级高
numStack[numPoint++] = total;
operStack[operPoint++] = lastOp;
total = number;
lastOp = op; //前一运算符压入堆栈所以没lastOp
//calculate(op);
}
} //end
else{
if(operPoint != 0){
lastOp = operStack[--operPoint];
total = numStack[--numPoint];
calculate(op);
}
else{//lastOp = null && operPoint == 0) 即为第一个符号
total = number;
lastOp = op;
}
}
checkValues();
}
void clear(){
numPoint = 0;
operPoint = 0;
opPressed = false;
number = 0.0;
total = 0.0;
strVal = "";
lastOp = null;
t_result.setText(" 0.0");
checkValues();
}
double getNumberOnDisplay(){
return new Double(strVal.trim()).doubleValue();
}
}
public static void main(String args[]){
new Calculator();
}
}
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Calculator extends JFrame{
static private int MAX_NUMBER_LENGTH = 21;
static private int PRIORITY_HIGHEST = 10;
static private int PRIORITY_HIGHER = 9;
static private int PRIORITY_HIGH = 6;
static private int PRIORITY_MIDDLE = 5;
static private int PRIORITY_LOW= 4;
static private int PRIORITY_LOWER = 2;
static private int PRIORITY_LOWEST = 1;
private JTextField t_result; //显示栏
private JPanel p_main; //盛放所有按钮的面板
private JPanel p_num; //数字按钮面板
private JPanel p_oper; //运算符按钮面板
private JPanel p_show; //
private JButton b_num[]; //数字按钮
private JButton b_oper[]; //运算符按钮
/**
* 构造函数
*/
public Calculator(){
super("Calculator");
t_result = new JTextField(" 0.0", MAX_NUMBER_LENGTH);
t_result.setEditable(false);
t_result.setBorder(BorderFactory.createLineBorder(Color.BLUE,3));
p_show = new JPanel();
p_main = new JPanel();
p_num = new JPanel();
p_oper = new JPanel();
p_show.add(t_result);
p_main.setLayout(new BorderLayout());
p_num.setLayout(new GridLayout(4, 3, 1, 1));
p_oper.setLayout(new GridLayout(4, 3, 1, 1));
//数字按钮
b_num = new JButton[12];
for(int i=0; i<10; i++)
b_num[i] = new JButton(new Integer(i+1).toString());
b_num[9] = new JButton("0");
b_num[10] = new JButton("cls");
b_num[11] = new JButton(".");
//操作符按钮
b_oper = new JButton[8];
b_oper[0] = new JButton("+");
b_oper[1] = new JButton("-");
b_oper[2] = new JButton("*");
b_oper[3] = new JButton("/");
b_oper[4] = new JButton("pow");
b_oper[5] = new JButton("sqrt");
b_oper[6] = new JButton("+/-");
b_oper[7] = new JButton("=");
//将按钮放入面板
for(int i=0; i<12; i++)
p_num.add(b_num[i]);
for(int i=0; i<8; i++)
p_oper.add(b_oper[i]);
Container contentPane = getContentPane();
contentPane.add(t_result, BorderLayout.NORTH);
contentPane.add(p_num, BorderLayout.WEST);
contentPane.add(p_oper, BorderLayout.EAST);
//初始化
setSize(400, 400);
setResizable(false);
pack();
setVisible(true);
//注册监听器
ButtonListener bl = new ButtonListener();
for(int i=0; i<12; i++)
b_num[i].addActionListener(bl);
for(int i=0; i<8; i++)
b_oper[i].addActionListener(bl);
}//end constructor
class ButtonListener implements ActionListener{
// 内部类ButtonListener继承ActionListener响应按钮
private String lastOp; //记录前一个运算符
private String strVal; //字符化的计算数值
private double number; //新输入或临时存放数值
private double total; //记录计算数值
private boolean opPressed; //判断operator是否按下
private double numStack[]; //数字堆栈
private String operStack[]; //符号堆栈
private int numPoint; //数字堆栈指针
private int operPoint; //符号堆栈指针
ButtonListener(){
numStack = new double[MAX_NUMBER_LENGTH/2];
operStack = new String[MAX_NUMBER_LENGTH/2];
numPoint = 0;
operPoint = 0;
opPressed = false;
strVal = "";
number = 0.0;
total = 0.0;
System.out.println("construtor!!");
}
//用于检查Error的函数,查看各个参数的值
void checkValues(){
System.out.println("| --------Values check out----------------");
System.out.println("| opPressed: " + opPressed);
System.out.println("| lastOp: " + lastOp);
System.out.println("| strVal: " + strVal);
System.out.println("| number: " + number);
System.out.println("| total: " + total);
System.out.println("| ----------------------------------------");
}
public void actionPerformed(ActionEvent ae){
String s = ((JButton)ae.getSource()).getActionCommand().trim();
if( Character.isDigit(s.charAt(0)) )
handleNumber(s);
else
calculate(s);
}
/*
* 处理数字
* */
void handleNumber(String s){
if( !opPressed ){ //没按下运算符按钮则为数字
strVal += s;
t_result.setText(strVal);
checkValues();
}
else{
//当按下运算符时,清除strVal并存入新输入数值的第一个数值
opPressed = false;
//lastOp = null;
strVal = s;
t_result.setText(strVal);
checkValues();
}
}
/**/
boolean isUnaryOp(String op){
return ( op.equals("sqrt") || op.equals("=") ||op.equals("cls") ||
op.equals("+/-") || op.equals("."));
}
/*运算符等级
* 一元运算符 +/-,= ,.,sqrt, cls 为1级
* 二元中pow为2级,*,/为3级,+,-为4级
* */
int operatorPriorityLevel( String op ){
int opPriLevel;
if(isUnaryOp(op)){
opPriLevel = PRIORITY_HIGHEST;
}
else {
if(op.equals("pow")){
opPriLevel = PRIORITY_HIGHER;
}
else {
if(op.equals("*") || op.equals("/")){
opPriLevel = PRIORITY_MIDDLE;
}
else //if(op.equals("+") || op.equals("-"))
{
opPriLevel = PRIORITY_LOW;
}
}
}
return opPriLevel;
}
/* 运算符包括一元(UnaryOp)和二元(BinaryOp)
一元有sqrt,cls,=,+/-,.(小数点)
二元有+,-,*,/,pow
*/
void calculate(String op){
opPressed = true; //运算符被按下
number = getNumberOnDisplay(); //将新输入的数由显示器上的转存入number
if(isUnaryOp(op))
handleUnaryOp(op);
else
handleBinaryOp(op);
}
void handleDecPoint(){
opPressed = false; //事实上小数点不算运算符,在这里我把它作为一种特殊的符号处理
if( strVal.indexOf(".")<0 ){
strVal += ".";
}
t_result.setText(strVal);
checkValues();
}
/* =号较为特殊,且用的频率较高
如果有=号,意味着一次预算的结束
所以opPressed要清零
*/
void handleUnaryOp( String op ){
if( op.equals("=") ){ //处理=号的情况
//如果前一预算符是二元运算符
if(lastOp.equals("+"))
total += number;
else if(lastOp.equals("-"))
total -= number;
else if(lastOp.equals("*"))
total *= number;
else if(lastOp.equals("/")){
try{
total /= number;
}catch(ArithmeticException ae){
//除0错误!!
t_result.setText("Error!");
}
}
else if(lastOp.equals("pow")){
total = Math.pow(total, number);
}
if(operPoint == 0){
strVal = new Double(total).toString();
t_result.setText(strVal);
opPressed = false;
numPoint = 0;
operPoint = 0;
lastOp = null;
}
else{ //堆栈中还有压有运算符
number = total;
lastOp = operStack[--operPoint];
total = numStack[--numPoint];
calculate(op);
}
checkValues();
}//end if op.equals("=")
else if( op.equals("sqrt") ){ //处理sqrt的情况
number = Math.sqrt(number);
strVal = new Double(number).toString();
t_result.setText(strVal);
}
else if(op.equals("+/-")){ //处理+/-的情况
number = -number;
strVal = new Double(number).toString().trim();
t_result.setText(strVal);
}
else if(op.equals(".")){ //处理小数点
checkValues();
handleDecPoint();
}
else if(op.equals("cls")){ //清盘处理!
clear();
}
checkValues();
}
void handleBinaryOp(String op){
//计算二元运算符并且opPressed=true
if(lastOp != null){
//前面有运算符
if(operatorPriorityLevel(lastOp) > operatorPriorityLevel(op)
|| operatorPriorityLevel(lastOp) == operatorPriorityLevel(op)){
//前一个运算符优先级大于或等于现在的
if( lastOp.equals("+") )
total += number;
else if(lastOp.equals("-"))
total -= number;
else if(lastOp.equals("*"))
total *= number;
else if(lastOp.equals("/")){
try{
total /= number;
}catch(ArithmeticException ae){
//除0错误!!
t_result.setText("Error!");
}
}
else if(lastOp.equals("pow")){
total = Math.pow(total, number);
}
number = total;
if(operPoint != 0){ //堆栈中还有压有运算符
lastOp = operStack[--operPoint];
total = numStack[--numPoint];
calculate(op);
}
strVal = (new Double(total)).toString().trim();
t_result.setText(strVal);
lastOp = op;
}
else{//现行运算符优先级高
numStack[numPoint++] = total;
operStack[operPoint++] = lastOp;
total = number;
lastOp = op; //前一运算符压入堆栈所以没lastOp
//calculate(op);
}
} //end
else{
if(operPoint != 0){
lastOp = operStack[--operPoint];
total = numStack[--numPoint];
calculate(op);
}
else{//lastOp = null && operPoint == 0) 即为第一个符号
total = number;
lastOp = op;
}
}
checkValues();
}
void clear(){
numPoint = 0;
operPoint = 0;
opPressed = false;
number = 0.0;
total = 0.0;
strVal = "";
lastOp = null;
t_result.setText(" 0.0");
checkValues();
}
double getNumberOnDisplay(){
return new Double(strVal.trim()).doubleValue();
}
}
public static void main(String args[]){
new Calculator();
}
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询