一个小计算器源码。使用google swt界面设计插件设计的界面。
比较简单,利用栈操作来实现表达式求值。
运算类Calc.java
package org.suju.calc;
import java.text.DecimalFormat;
import java.util.HashMap;
import java.util.Stack;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/*
name:calc
author:suju
date:2012-9-7
Method:接口类实现
public boolean Comopera(char och1,char och2)
//运算符优先级比较 **och opera char
public boolean Inopera(String och)
//检查字符och是否运算符
public String expressionToStack(String expression)
//表达式expression求值并返回值到字符形式
public Pattern getOperaPattern()
返回运算符正则表达式
public String toSqrt(String value);
求平方根
public String toSquare(String value);
求平方值
public String toExp(String value);
求e的指数值 value需要转换double
Method:自定义方法。
public String format(double result)
//格式化double为字符串形式。
public String checkStack(String expression)
//表达式expression进行数字运算符入栈操作
public void stack_opera(char opera)
//栈运算操作,运算符,运算数出栈计算操作
public Double calc(Double num1,Double num2,char opera)
//计算参数列表中到3个组合表达式值 example:num1 opera num2 opera为运算符
*/
public class Calc implements ICalc{
private static HashMap<Character,Character>operas; /*保存运算符到hashmap*/
private Stack<Double> n_stack; /*操作数栈*/
private Stack<Character> o_stack; /*操作符栈*/
private Pattern nump; /*数字和运算符匹配正则*/
private Pattern operamatcher; /*运算符匹配正则*/
private DecimalFormat maxformat; /*最大值格式化*/
private DecimalFormat minformat; /*最小值格式化*/
private final static String MAX="#.#######E0"; /*格式化格式*/
private final static String MIN="#.########"; /*格式化格式*/
private Calc()
{
init();
}
/*返回实例*/
public static Calc getInstance()
{
return new Calc();
}
/*初始化参数*/
public void init()
{
/*创建操作栈,运算符栈入栈一个初始运算符#*/
n_stack=new Stack<Double>();
o_stack=new Stack<Character>();
o_stack.push('#');
/*初始化运算符匹配hashmap,value表示优先级*/
operas=new HashMap<Character, Character>();
operas.put('#','0');
operas.put('+','1');
operas.put('-','1');
operas.put('*','2');
operas.put('/','2');
/*正则编译*/
/*@是替换匹配到-号,避免和正常到减号冲突*/
nump=Pattern.compile("(@?\\d+\\.?\\d*[e|E]*\\d*|[+,\\-,*,/,#])");
operamatcher=Pattern.compile("([+,\\-,*,/])");
/*初始化格式化符*/
maxformat=new DecimalFormat(MAX);
minformat=new DecimalFormat(MIN);
}
/*返回运算符正则表达式 */
public Pattern getOperaPattern()
{
return operamatcher;
}
public String format(double result)
{
String value=minformat.format(result); /*默认格式化操作*/
if(result>100000000)
{
value=maxformat.format(result); /*如果结果大于100000000,则最大化格式*/
}
else if(value.length()>10)
{
value=maxformat.format(result); /*如果结果长度超过10字符,以最大格式化显示*/
}
return value;
}
/*返回计算计算结果*/
public String expressionToStack(String expression)
{
String revalue=expression; /*默认返回值*/
Matcher matcher=operamatcher.matcher(expression); /*匹配运算符*/
if( matcher.find() ) /*查找是否表达式中包含运算符*/
{
int len=revalue.length();
if ( Inopera(revalue.substring(len-1)) ) /*查找最后一个字符是否是运算符*/
{
revalue=revalue.substring(0,len-1); /*去掉最后一个运算符*/
if( matcher.find() )
{
revalue=checkStack(revalue); /*继续查找运算符,存在则进行解析表达式操作*/
}
return revalue; /*返回值*/
}
revalue=checkStack(revalue); /*当最后一个字符不是运算符时,直接解析表达式*/
}
if (revalue.contains("-") )
{ revalue=revalue.replaceAll("-","@"); } //返回值去掉@为-号
return revalue;
}
/*求exp值*/
public String toExp(String value)
{
/*声明返回值*/
String result=value.replaceAll("@","-"); /*替换@*/
double d_result;
try{
d_result=Math.exp (Double.valueOf(result));
if(Double.isInfinite(d_result)) {d_result=0.0;} /*判断运算结果是否超出返回。*/
}catch(NumberFormatException e)
{
d_result=0.0; /*出现格式化异常就返回0*/
}
result=format(d_result);
return result;
}
/*求平方值*/
public String toSquare(String value)
{
/*声明返回值*/
String result=value.replaceAll("@","-"); /*替换@为-号*/
double d_result;
try{
d_result=Double.valueOf(result);
d_result*=d_result;
if(Double.isInfinite(d_result)) {d_result=0.0;} /*d_result自乘,并判断是否超出运算范围*/
}catch(NumberFormatException e)
{
d_result=0.0; /*抛出异常返回0*/
}
result=format (d_result); /*格式化输出*/
return result;
}
//求平方根
public String toSqrt(String value)
{
String result=value.replaceAll("@","-"); /*替换@为-号 初始化默认值*/
double d_result;
try
{
d_result=Double.valueOf(result);
if(d_result>0) { d_result=Math.sqrt(d_result);} /*判断值是否小于0,小于0返回0,避免抛出异常*/
else { d_result=0.0; }
}catch(NumberFormatException e)
{ d_result=0.0; }
result=format(d_result);
return result;
}
/**/
public String checkStack(String expression)
{
Matcher numm=nump.matcher(expression+"#"); /*数字和运算符匹配,默认结尾加上一个#号结束*/
while(numm.find())
{
String num=numm.group(); /*获取匹配数*/
if(!Inopera(num))
{
num=num.replaceAll("@","-"); /*替换@为负值表示符号*/
n_stack.push(Double.valueOf(num)); /*检测匹配数是否是数字,是就入运算数栈*/
}
else
{
stack_opera(numm.group().charAt(0)); /*是运算符就进行栈运算操作判断,chaAt(0)返回第一字符*/
}
}
String result=format(n_stack.peek()); /*计算结束,返回运算数栈结果*/
return result;
}
public void stack_opera(char opera)
{
if(Comopera(opera,o_stack.peek()))
{
o_stack.push(opera); /*将运算符和栈顶运算符比较优先级,大于则入栈*/
}
else
{
Double num2=n_stack.pop();
Double num1=n_stack.pop();
char sopera=o_stack.pop();
n_stack.push(calc(num1,num2,sopera)); /*运算数栈出栈2次,获取操作数,运算符栈出栈一次,在计算*/
if(opera=='#' && opera==o_stack.peek())
{
return; /*当运算符为#,并和栈顶相同时,返回,否则递归计算*/
}
stack_opera(opera);
}
}
public Double calc(Double num1,Double num2,char opera)
{
double result=0.0; /*默认返回值*/
/*判断opera运算符,来计算表达式值*/
switch(opera)
{
case '+':
result=num1+num2;
break;
case '-':
result=num1-num2;;
break;
case '*':
result= num1*num2;
break;
case '/':
result= num1/num2;
if(Double.isInfinite(result))result=0.0; /*当值无限大时,返回0*/
}
return result;
}
public boolean Comopera(char och1,char och2)
{
/*运算符比较优先级,根据在hashmap中到值大小*/
if(operas.get(och1)>operas.get(och2))
{
return true;
}
return false;
}
public boolean Inopera(String och)
{
/*判断och是否是hashmap中到运算符*/
for(Character opera:operas.keySet())
{
/*遍历map中到key*/
if(och.equals(opera.toString())) return true;
}
return false;
}
}
界面类CalcFrame.java
package org.suju.calc;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.Insets;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JButton;
import javax.swing.JTextField;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.Font;
import javax.swing.SwingConstants;
import java.awt.Color;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.regex.Matcher;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
public class CalcFrame extends JFrame implements ActionListener{
private JPanel contentPane;
private JTextField textField;
private StringBuilder buffer; /*界面显示区字符数组*/
private Editable editproxy; /*字符数组代理类*/
private StringBuilder edit; /*界面Edittext编辑对象*/
private JTextField edittext; /*数字显示框*/
private Handler handler; /*定义handler发送msg*/
private AboutUI about;
private ICalc calc; /*计算接口实现类*/
private final static int DEL=1; /*删除标记*/
private final static int NUM=0; /*数字标记*/
private final static int OPERA=4; /*运算符标记*/
private final static int DELALL=2; /*删除所有 标记*/
private final static int EQUAL=3; /*等于标记*/
private final static int EQUALUP=0; /*标记等号状态 没有按下*/
private final static int EQUALDOWN=1; /*标记等号状态 已经按下*/
private final static int PERIOD=5; /*小数点标记*/
private final static int SQRT=6; /*平方根标记*/
private final static int SQUARE=7; /*平方标记*/
private final static int EXP=8; /*exp标记*/
private final static int MADD=9; /*M+标记*/
private final static int MDASH=10; /*M-标记*/
private final static int MRC=11; /*MRC标记*/
private final static int PN=12; /*负号标记*/
private static String mvalue=""; /*初始M值*/
private static int equalstates=EQUALUP; /*初始化等号状态*/
private JButton mrc; /*mrc按钮*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
CalcFrame frame = new CalcFrame();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public CalcFrame() {
//auto code
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(500, 100, 320, 480);
JMenuBar menuBar = new JMenuBar();
setJMenuBar(menuBar);
JMenu menu = new JMenu("\u9009\u9879");
menuBar.add(menu);
JMenuItem menuItem = new JMenuItem("\u9000\u51FA");
menuItem.setActionCommand("exit");
menu.add(menuItem);
menuItem.addActionListener(this);
JMenu menu_1 = new JMenu("\u66F4\u591A");
menuBar.add(menu_1);
JMenuItem menuItem_1 = new JMenuItem("\u5173\u4E8E");
menuItem_1.setActionCommand("about");
menu_1.add(menuItem_1);
menuItem_1.addActionListener(this);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JButton btnNewButton = new JButton("7");
btnNewButton.setForeground(Color.BLUE);
btnNewButton.setActionCommand("key_7");
btnNewButton.addActionListener(this);
btnNewButton.setBackground(new Color(220, 220, 220));
btnNewButton.setBounds(10, 189, 48, 48);
contentPane.add(btnNewButton);
textField = new JTextField();
textField.setHorizontalAlignment(SwingConstants.RIGHT);
textField.setText("");
textField.setFont(new Font("宋体", Font.BOLD, 25));
textField.setEditable(false);
textField.setBounds(10, 10, 292, 53);
contentPane.add(textField);
textField.setColumns(10);
JButton button = new JButton("8");
button.setForeground(Color.BLUE);
button.setActionCommand("key_8");
button.addActionListener(this);
button.setBackground(new Color(220, 220, 220));
button.setBounds(68, 189, 48, 48);
contentPane.add(button);
JButton button_1 = new JButton("9");
button_1.setForeground(Color.BLUE);
button_1.setActionCommand("key_9");
button_1.addActionListener(this);
button_1.setBackground(new Color(220, 220, 220));
button_1.setBounds(126, 189, 48, 48);
contentPane.add(button_1);
JButton button_2 = new JButton("4");
button_2.setForeground(Color.BLUE);
button_2.setActionCommand("key_4");
button_2.addActionListener(this);
button_2.setBackground(new Color(220, 220, 220));
button_2.setBounds(10, 247, 48, 48);
contentPane.add(button_2);
JButton button_3 = new JButton("5");
button_3.setForeground(Color.BLUE);
button_3.setActionCommand("key_5");
button_3.addActionListener(this);
button_3.setBackground(new Color(220, 220, 220));
button_3.setBounds(68, 247, 48, 48);
contentPane.add(button_3);
JButton button_4 = new JButton("6");
button_4.setForeground(Color.BLUE);
button_4.setActionCommand("key_6");
button_4.addActionListener(this);
button_4.setBackground(new Color(220, 220, 220));
button_4.setBounds(126, 247, 48, 48);
contentPane.add(button_4);
JButton button_5 = new JButton("1");
button_5.setForeground(Color.BLUE);
button_5.setActionCommand("key_1");
button_5.addActionListener(this);
button_5.setBackground(new Color(220, 220, 220));
button_5.setBounds(10, 305, 48, 48);
contentPane.add(button_5);
JButton button_6 = new JButton("2");
button_6.setForeground(Color.BLUE);
button_6.setActionCommand("key_2");
button_6.addActionListener(this);
button_6.setBackground(new Color(220, 220, 220));
button_6.setBounds(68, 305, 48, 48);
contentPane.add(button_6);
JButton button_7 = new JButton("3");
button_7.setForeground(Color.BLUE);
button_7.setActionCommand("key_3");
button_7.addActionListener(this);
button_7.setBackground(new Color(220, 220, 220));
button_7.setBounds(126, 305, 48, 48);
contentPane.add(button_7);
JButton button_8 = new JButton(".");
button_8.setFont(new Font("宋体", Font.BOLD, 12));
button_8.setForeground(Color.BLUE);
button_8.setActionCommand("key_period");
button_8.addActionListener(this);
button_8.setBackground(new Color(220, 220, 220));
button_8.setBounds(10, 363, 48, 48);
contentPane.add(button_8);
JButton button_9 = new JButton("0");
button_9.setForeground(Color.BLUE);
button_9.setActionCommand("key_0");
button_9.addActionListener(this);
button_9.setBackground(new Color(220, 220, 220));
button_9.setBounds(68, 363, 106, 48);
contentPane.add(button_9);
JButton button_10 = new JButton("+");
button_10.setForeground(new Color(60, 179, 113));
button_10.setActionCommand("key_plus");
button_10.addActionListener(this);
button_10.setBackground(new Color(220, 220, 220));
button_10.setBounds(196, 363, 48, 48);
contentPane.add(button_10);
JButton button_11 = new JButton("-");
button_11.setForeground(new Color(50, 205, 50));
button_11.setActionCommand("key_dash");
button_11.addActionListener(this);
button_11.setBackground(new Color(220, 220, 220));
button_11.setBounds(196, 305, 48, 48);
contentPane.add(button_11);
JButton btnX = new JButton("X");
btnX.setForeground(new Color(50, 205, 50));
btnX.setActionCommand("key_star");
btnX.addActionListener(this);
btnX.setBackground(new Color(220, 220, 220));
btnX.setBounds(196, 247, 48, 48);
contentPane.add(btnX);
JButton button_13 = new JButton("\u00F7");
button_13.setForeground(new Color(50, 205, 50));
button_13.setActionCommand("key_slash");
button_13.addActionListener(this);
button_13.setBackground(new Color(220, 220, 220));
button_13.setBounds(196, 189, 48, 48);
contentPane.add(button_13);
JButton button_14 = new JButton("=");
button_14.setForeground(new Color(60, 179, 113));
button_14.setActionCommand("key_equal");
button_14.addActionListener(this);
button_14.setBackground(new Color(220, 220, 220));
button_14.setBounds(254, 305, 48, 106);
contentPane.add(button_14);
JButton button_15 = new JButton("\u221A\uFFE3");
button_15.setForeground(new Color(186, 85, 211));
button_15.setActionCommand("key_sqrt");
button_15.addActionListener(this);
button_15.setMargin(new Insets(0,0,0,0));
button_15.setBackground(new Color(220, 220, 220));
button_15.setFont(new Font("宋体", Font.PLAIN, 12));
button_15.setBounds(254, 247, 48, 48);
contentPane.add(button_15);
JButton btnX_1 = new JButton("x\u00B2");
btnX_1.setForeground(new Color(186, 85, 211));
btnX_1.setActionCommand("key_square");
btnX_1.addActionListener(this);
btnX_1.setBackground(new Color(220, 220, 220));
btnX_1.setBounds(254, 189, 48, 48);
contentPane.add(btnX_1);
JButton btnC = new JButton("C");
btnC.setForeground(new Color(255, 69, 0));
btnC.setActionCommand("key_c");
btnC.addActionListener(this);
btnC.setBackground(new Color(220, 220, 220));
btnC.setBounds(10, 131, 106, 48);
contentPane.add(btnC);
JButton button_18 = new JButton("\u2190");
button_18.setForeground(new Color(255, 99, 71));
button_18.setActionCommand("key_left");
button_18.addActionListener(this);
button_18.setBackground(new Color(220, 220, 220));
button_18.setBounds(126, 131, 48, 48);
contentPane.add(button_18);
JButton btnExp = new JButton("Exp");
btnExp.setForeground(new Color(186, 85, 211));
btnExp.setActionCommand("key_exp");
btnExp.addActionListener(this);
btnExp.setMargin(new Insets(0,0,0,0));
btnExp.setBackground(new Color(220, 220, 220));
btnExp.setFont(new Font("宋体", Font.PLAIN, 12));
btnExp.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
}
});
btnExp.setBounds(196, 131, 48, 48);
contentPane.add(btnExp);
JButton button_20 = new JButton("+/-");
button_20.setForeground(new Color(186, 85, 211));
button_20.setActionCommand("key_pn");
button_20.addActionListener(this);
button_20.setMargin(new Insets(0,0,0,0));
button_20.setBackground(new Color(220, 220, 220));
button_20.setFont(new Font("宋体", Font.PLAIN, 12));
button_20.setBounds(254, 131, 48, 48);
contentPane.add(button_20);
mrc = new JButton("MR");
mrc.setForeground(new Color(0, 191, 255));
mrc.setActionCommand("key_mrc");
mrc.setMargin(new Insets(0,0,0,0));
mrc.setBackground(new Color(220, 220, 220));
mrc.setFont(new Font("宋体", Font.PLAIN, 12));
mrc.addActionListener(this);
mrc.setBounds(10, 73, 48, 48);
contentPane.add(mrc);
JButton btnM = new JButton("M+");
btnM.setForeground(new Color(0, 191, 255));
btnM.setActionCommand("key_madd");
btnM.addActionListener(this);
btnM.setBackground(new Color(220, 220, 220));
btnM.setFont(new Font("宋体", Font.PLAIN, 12));
btnM.setBounds(68, 73, 106, 48);
contentPane.add(btnM);
JButton btnM_1 = new JButton("M-");
btnM_1.setForeground(new Color(0, 191, 255));
btnM_1.setActionCommand("key_mdash");
btnM_1.addActionListener(this);
btnM_1.setBackground(new Color(220, 220, 220));
btnM_1.setFont(new Font("宋体", Font.PLAIN, 12));
btnM_1.setBounds(196, 73, 106, 48);
contentPane.add(btnM_1);
init();
}
public void init()
{
calc=Calc.getInstance(); /*获取计算类实例*/
edittext=textField;
about=new AboutUI(this,false);
mrc.setEnabled(false);
/*实例化handler对象。*/
handler=new Handler(){
@Override
public void handleMessage(Message msg) {
/*内部实现方法*/
dohandle(msg);
}
};
/*声明一个字符缓冲组*/
buffer=new StringBuilder();
/*定义一个代理操作类,用来代理buffer和edit操作*/
editproxy=(Editable)Proxy.newProxyInstance(CalcFrame.class.getClassLoader(),new Class[]{Editable.class},
new InvocationHandler() {
/*匿名内部类 代理处理操作类*/
public Object invoke(Object obj, Method method, Object[] arg)
throws Throwable {
/*获取反射方法参数*/
Class[] params=method.getParameterTypes();
Method m=buffer.getClass().getMethod(method.getName(), params);
m.invoke(buffer, arg); /*执行buffer操作*/
m.invoke(edit, arg); /*执行edit操作*/
/*如果edit包含@,替换@为-*/
if (edit.toString().contains("@")){
String editcopy=edit.toString().replaceFirst("@","-");
int len=edit.length();
edit.replace(0, len, editcopy);
}
edittext.setText(edit.toString());
return null;
}
});
}
public void action(String num,int what)
{
/*获取消息,并初始化*/
Message msg=handler.obtainMessage();
msg.obj=num;
msg.what=what;
handler.sendMessage(msg);
}
@Override
public void actionPerformed(ActionEvent e) {
KeyEnum key=KeyEnum.valueOf(e.getActionCommand());
switch(key)
{
/*处理按钮点击操作*/
case exit:
System.exit(0);
break;
case about:
about.toshow();
break;
case key_0:
action("0",0);
break;
case key_1:
action("1",0);
break;
case key_2:
action("2",0);
break;
case key_3:
action("3",0);
break;
case key_4:
action("4",0);
break;
case key_5:
action("5",0);
break;
case key_6:
action("6",0);
break;
case key_7:
action("7",0);
break;
case key_8:
action("8",0);
break;
case key_9:
action("9",0);
break;
case key_plus:
action("+",OPERA);
break; //+
case key_dash:
action("-",OPERA);
break; //-
case key_star:
action("*",OPERA);
break; //*
case key_slash:
action("/",OPERA);
break;
/*当按钮为等号时,改变等号状态为EQUALDOWN*/
case key_equal:
action("",EQUAL);
equalstates=EQUALDOWN;
break;
case key_period:
action(".",PERIOD);
break;
/*当按钮为退格时 根据equalstates状态来确定清除操作*/
case key_left:
if(equalstates==EQUALDOWN){
action("",DELALL);
equalstates=EQUALUP;
}
else{action("",DEL);}
break;
case key_c:
action("",DELALL);
break;
case key_sqrt: /*求平方根*/
action("",SQRT);
equalstates=EQUALDOWN; /*添加一个等号按下状态,避免数字操作*/
break;
case key_square: /*平方按钮操作*/
action("",SQUARE);
equalstates=EQUALDOWN;
break;
case key_pn:
action("-",PN);
break;
case key_exp:
action("",EXP);
equalstates=EQUALDOWN;
break;
case key_madd:
action("",MADD);
equalstates=EQUALDOWN; /*初始化一个状态符*/
break;
case key_mdash:
action("",MDASH);
break;
case key_mrc:
action("",MRC);
equalstates=EQUALDOWN; /*初始化一个状态符*/
}
}
//
public void dohandle(Message msg)
{
/*获取edittext属性值*/
int len=edittext.getText().length();
edit=new StringBuilder(edittext.getText());
/*计算返回值字符串*/
String result;
/*根据消息msg.what值判断*/
switch(msg.what)
{
/*操作数为数字时*/
case NUM:
if(equalstates==EQUALDOWN)
{
editproxy.replace(0,len,msg.obj.toString()); /*当等号按过时到操作 主要用来清楚屏幕*/
equalstates=EQUALUP;
}else{
editproxy.append(msg.obj.toString()); /*等号没有按过时到操作*/
}
break;
/*操作数为运算符时*/
case OPERA:
opera(buffer,len,msg);
break;
/*操作数为退格时*/
case DEL:
if(len>0){
editproxy.delete(len-1,len); /*当显示框不为空时,删除最后一个数*/
}
break;
/*操作数为C时*/
case DELALL:
editproxy.replace(0,len,""); /*删除所有*/
equalstates=EQUALUP;
break;
/*操作数为=号时*/
case EQUAL:
result=calc.expressionToStack(buffer.toString()); /*进行计算并返回值*/
editproxy.replace(0,len,result);
break;
/*求exp*/
case EXP:
result=calc.expressionToStack(buffer.toString());
result=calc.toExp(result); /*进行计算并值e^a,其中 e 是自然对数的底数。*/
editproxy.replace(0,len,result);
break;
/*求平方*/
case SQUARE:
result=calc.expressionToStack(buffer.toString());
result=calc.toSquare(result); /*平方计算 进行平方计算并返回值*/
editproxy.replace(0,len,result);
break;
/*求平方根*/
case SQRT:
result=calc.expressionToStack(buffer.toString());
result=calc.toSqrt(result); /*返回平方根 进行平方根计算并返回值*/
editproxy.replace(0,len,result);
break;
/*M+操作,数据加入内存*/
case MADD:
result=calc.expressionToStack(buffer.toString());
if (!"@".equals(result))
{
mvalue=result;
editproxy.replace(0,len,mvalue);
mrc.setEnabled(true);
}else{
JOptionPane.showMessageDialog(this, "错误! M+存储数不能为单独的-号!");
}
break;
/*M-操作,数据从内存清除*/
case MDASH:
mvalue="";
mrc.setEnabled(false);
break;
/*MRC操作。记忆数据*/
case MRC:
valutToscreen(buffer,len); /*MR屏幕操作*/
break;
/*负号操作*/
case PN:
if (checkPN(buffer,len) )
{
edit.append(msg.obj.toString());
buffer.append('@'); /*字符缓冲区添加@,不同于显示区*/
edittext.setText(edit.toString()); /*更新界面*/
}
break;
/*操作数为小数点时*/
case PERIOD:
/*判断是否可以加小数点*/
if( checkPeriod(buffer,len) )
{
editproxy.append(msg.obj.toString());
}
}
/********************************/
}
/*运算符操作*/
public void opera(StringBuilder buffer,int len,Message msg)
{
if (!"".equals(buffer.toString())) /*显示框不为空时*/
{
String och=Character.toString(buffer.charAt(len-1)); /*最后一个字符*/
if ("@".equals(och)) return;
if (calc.Inopera(och))
{
editproxy.delete(len-1,len); /*判断显示框最后一位是否已经是运算符,并替换*/
}
editproxy.append(msg.obj.toString());
equalstates=EQUALUP; /*初始化等号状态*/
}
}
/*负号判断*/
public Boolean checkPN(StringBuilder buffer,int len)
{
Boolean result=false;
if ("".equals(buffer.toString()) )
{
result=true; /*检查是否为空串*/
}else
{
String och=Character.toString(buffer.charAt(len-1)); /*检查最后一个字符是否是运算符并判断是不是-号*/
if (calc.Inopera(och) && !"-".equals(och)) { result=true; }
}
return result;
}
/*MR存储数显示到屏幕*/
public void valutToscreen(StringBuilder buffer,int len)
{
if ("".equals( buffer.toString())){
editproxy.append(mvalue); /*屏幕字符数组为空就直接添加*/
}else
{
String och=Character.toString(buffer.charAt(len-1)); /*最后一个字符*/
if (calc.Inopera(och))
{
editproxy.append(mvalue); /*当屏幕上有字符,判断最后是不是运算符*/
}else
{
JOptionPane.showMessageDialog(this, "错误! MR存储数只能存放在操作符后面!");
}
}
}
/*判断小数点*/
public Boolean checkPeriod(StringBuilder buffer,int len)
{
Boolean result=false; /*定义返回值*/
if( !"".equals(buffer.toString())) /*判断末尾是否是运算符或子符是否是空*/
{
String och=Character.toString(buffer.charAt(len-1)); /*最后一个字符*/
if (calc.Inopera(och) || "@".equals(och)) return result; /*匹配@或运算符就返回*/
int last=buffer.toString().lastIndexOf('.'); /*从后面查找最后一个.号*/
if( last>0 )
{
Matcher matcher=calc.getOperaPattern().matcher(buffer.toString());
if( matcher.find(last) ) /*获取calc的运算符正则匹配*/
{
result=true; /*从last位置开始匹配.,成功返回true*/
}
}else
{
result=true; /*当字符不包含.的时候,返回true*/
}
}
return result; /*不满足以上条件返回默认值*/
}
}
源码中包含java源文件,发布博客,备忘