/*
* 2006-01-14
* BigNum.java
*/
package liang;
public class BigNum{
private static int result;
private static String m;
BigNum(){
}
public static String add(String a, String b){
int la, lb;
char[] aArray = null;
char[] bArray = null;
if (a.length() >= b.length()){
la = a.length();
lb = b.length();
aArray = a.toCharArray();
bArray = b.toCharArray();
}else{
la = b.length();
lb = a.length();
aArray = b.toCharArray();
bArray = a.toCharArray();
}
int i, p = 0;
for (i = la-1; i >= la-lb; i--){
try{
int temp = (aArray[i]-'0')+(bArray[i-la+lb]-'0')+p;
aArray[i] = (char)(temp % 10 + '0');
p = temp / 10;
}catch(ArrayIndexOutOfBoundsException e){
e.printStackTrace();
}
}
while (p==1 && i>=0){
try{
int temp = (aArray[i]-'0')+p;
aArray[i] = (char)((aArray[i]-'0'+p) % 10+'0');
p = temp / 10;
i--;
}catch(ArrayIndexOutOfBoundsException e){
e.printStackTrace();
}
}
if(i < 0 && p==1){
return 1+new String(aArray);
}
return new String(aArray);
}
public static String minus(String a, String b){
String s = "", c = "";
if (a.equals(b)){
return String.valueOf(0);
}
if ((a.length()< b.length())||
(a.length()==b.length() && a.compareTo(b)<0 )){
s = "-";
c = a;
a = b;
b = c;
}
int la = a.length(), lb = b.length();
char[] aArray = a.toCharArray();
char[] bArray = b.toCharArray();
int i, p = 0;
for (i = la-1; i >= la-lb; i--){
try{
if (aArray[i] == '0'){
if (bArray[i-la+lb] != '0'){
aArray[i] = (char)(10-p-(bArray[i-la+lb]-'0')+'0');
p = 1;
}else{
if (p == 0){
aArray[i] = '0';
p = 0;
}else{
aArray[i] = '9';
p = 1;
}
}
}else{
int t = (aArray[i]-'0'+10-p) % 10;
if (t < bArray[i-la+lb]-'0'){
t += 10;
p = 1;
}else {
p = 0;
}
aArray[i] = (char)(t-(bArray[i-la+lb]-'0')+'0');
}
}catch(ArrayIndexOutOfBoundsException e){
}
}
while (p == 1){
try{
if (aArray[i] == '0'){
p = 1;
aArray[i] = '9';
}else{
p = 0;
aArray[i] -= 1;
}
}catch(ArrayIndexOutOfBoundsException e){
}
i --;
}
a = new String(aArray);
int j = 0;
while(a.charAt(j++)=='0'){}
a = a.substring(--j);
return s+a;
}
public static String multiplication(String a, String b){
int la = a.length(), lb = b.length();
int i, j, t, p;
String sum = "0";
String addParam = "";
for (i = 0; i < lb; i++){
p = 0;
char [] s = new char[la+lb];
int l;
for (l = 0; l < la+lb; l++){
s[l] = '0';
}
for (j = 0; j < la; j++){
t = (b.charAt(lb-1-i)-'0')*(a.charAt(la-1-j)-'0')+p;
p = t / 10;
s[la+lb-1-j-i] = (char)(t%10+'0');
}
s[lb-i-1] = (char)(p+'0');
addParam = new String(s);
sum = add(sum,addParam);
}
int jj = 0;
while(sum.charAt(jj++)=='0'){}
sum = sum.substring(--jj);
return sum;
}
public static void mod(String a, String b){
result = 0;
m = "";
int i;
for (i = 0; i < a.length(); i++){
if (a.charAt(i) != '0'){
break;
}
}
if (i == a.length()){
result = 0;
m = "0";
return;
}else{
a = a.substring(i);
}
if (a.length()<b.length() ||
a.length()==b.length()&&
a.compareTo(b)<0){
result = 0;
m = a;
return;
}
boolean f = true;
while(f){
if(minus(a,b).charAt(0)=='-'){
f = !f;
}else{
a = minus(a,b);
result++;
}
}
m = a;
return;
}
public static String divide(String a, String b){
int la = a.length();
int lb = b.length();
int i, k;
for (i = 0; i < 7; i++){
a += "0";
}
StringBuffer sb = new StringBuffer(1024);
sb.setLength(1024);
for (k = 0; k < lb; k++){
sb.setCharAt(k,'0');
}
mod(a.substring(0,lb),b);
int j;
String c = "";
for (j = lb-1; ;){
sb.insert(j,result);
if (j == la+6){
break;
}
try{
c = m + a.substring(++j, j+1);
}catch(StringIndexOutOfBoundsException e){
e.printStackTrace();
}
mod(c,b);
}
if (sb.charAt(la+6) > '4'){
sb = new StringBuffer(add(sb.toString().substring(0,la+6), "1"));
}else{
sb.setLength(la+6);
}
i = 0;
while(true){
if (sb.charAt(i)!= '0'){
break;
}
i++;
}
return sb.substring(i,la)+"."+sb.substring(la);
}
}
/*
* CaculatorFrame.java
*/
package liang;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class CalculatorFrame extends JFrame{
private static final long serialVersionUID = 1L;
private JMenuBar menuBar = new JMenuBar();
private Insets inset = new Insets(0,0,0,0);
private int flag = 0;
JTextField inputTxt;
JButton backButton;
JButton ceButton;
JButton cButton;
JButton mcButton;
JButton mrButton;
JButton msButton;
JButton mmButton;
JButton button7;
JButton button8;
JButton button9;
JButton divButton;
JButton sqrtButton;
JButton button4;
JButton button5;
JButton button6;
JButton mulButton;
JButton perButton;
JButton button1;
JButton button2;
JButton button3;
JButton minusButton;
JButton fracButton;
JButton button0;
JButton symButton;
JButton dotButton;
JButton addButton;
JButton equalButton;
StringBuffer c = new StringBuffer();
String a;
char op;
String b;
public CalculatorFrame(String title){
setTitle(title);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setJMenuBar(menuBar);
JMenu editMenu = new JMenu("编辑");
JMenu searchMenu = new JMenu("查看");
JMenu helpMenu = new JMenu("帮助");
menuBar.add(editMenu);
menuBar.add(searchMenu);
menuBar.add(helpMenu);
inputTxt = new JTextField("0.");
inputTxt.setHorizontalAlignment(JTextField.RIGHT);
inputTxt.setBounds(7,1,248,22);
mcButton = new JButton("MC");
mrButton = new JButton("MR");
msButton = new JButton("MS");
mmButton = new JButton("M+");
backButton = new JButton("Backspace");
ceButton = new JButton("CE");
cButton = new JButton("C");
button7 = new JButton("7");
button8 = new JButton("8");
button9 = new JButton("9");
divButton = new JButton("/");
sqrtButton = new JButton("sqrt");
button4 = new JButton("4");
button5 = new JButton("5");
button6 = new JButton("6");
mulButton = new JButton("*");
perButton = new JButton("%");
button1 = new JButton("1");
button2 = new JButton("2");
button3 = new JButton("3");
minusButton = new JButton("-");
fracButton = new JButton("1/x");
button0 = new JButton("0");
symButton = new JButton("+/-");
dotButton = new JButton(".");
addButton = new JButton("+");
equalButton = new JButton("=");
backButton.setBounds(56,34,63,24);
ceButton.setBounds(124,34,63,24);
cButton.setBounds(192,34,63,24);
mcButton.setBounds(8,66,36,24);
mrButton.setBounds(8,96,36,24);
msButton.setBounds(8,126,36,24);
mmButton.setBounds(8,156,36,24);
button7.setBounds(56,66,35,24);
button8.setBounds(97,66,35,24);
button9.setBounds(138,66,35,24);
divButton.setBounds(179,66,35,24);
sqrtButton.setBounds(220,66,35,24);
button4.setBounds(56,96,35,24);
button5.setBounds(97,96,35,24);
button6.setBounds(138,96,35,24);
mulButton.setBounds(179,96,35,24);
perButton.setBounds(220,96,35,24);
button1.setBounds(56,126,35,24);
button2.setBounds(97,126,35,24);
button3.setBounds(138,126,35,24);
minusButton.setBounds(179,126,35,24);
fracButton.setBounds(220,126,35,24);
button0.setBounds(56,156,35,24);
symButton.setBounds(97,156,35,24);
dotButton.setBounds(138,156,35,24);
addButton.setBounds(179,156,35,24);
equalButton.setBounds(220,156,35,24);
backButton.setMargin(inset);
ceButton.setMargin(inset);
cButton.setMargin(inset);
mcButton.setMargin(inset);
mrButton.setMargin(inset);
msButton.setMargin(inset);
mmButton.setMargin(inset);
button7.setMargin(inset);
button8.setMargin(inset);
button9.setMargin(inset);
divButton.setMargin(inset);
sqrtButton.setMargin(inset);
button4.setMargin(inset);
button5.setMargin(inset);
button6.setMargin(inset);
mulButton.setMargin(inset);
perButton.setMargin(inset);
button1.setMargin(inset);
button2.setMargin(inset);
button3.setMargin(inset);
minusButton.setMargin(inset);
fracButton.setMargin(inset);
button0.setMargin(inset);
symButton.setMargin(inset);
dotButton.setMargin(inset);
addButton.setMargin(inset);
equalButton.setMargin(inset);
numBtnMonitor monitor1 = new numBtnMonitor();
button7.addActionListener(monitor1);
button8.addActionListener(monitor1);
button9.addActionListener(monitor1);
button4.addActionListener(monitor1);
button5.addActionListener(monitor1);
button6.addActionListener(monitor1);
button1.addActionListener(monitor1);
button2.addActionListener(monitor1);
button3.addActionListener(monitor1);
button0.addActionListener(monitor1);
opBtnMonitor monitor2 = new opBtnMonitor();
addButton.addActionListener(monitor2);
minusButton.addActionListener(monitor2);
mulButton.addActionListener(monitor2);
divButton.addActionListener(monitor2);
rstMonitor monitor3= new rstMonitor();
equalButton.addActionListener(monitor3);
getContentPane().setLayout(null);
getContentPane().add(inputTxt);
getContentPane().add(backButton);
getContentPane().add(ceButton);
getContentPane().add(cButton);
getContentPane().add(mcButton);
getContentPane().add(mrButton);
getContentPane().add(msButton);
getContentPane().add(mmButton);
getContentPane().add(button7);
getContentPane().add(button8);
getContentPane().add(button9);
getContentPane().add(divButton);
getContentPane().add(sqrtButton);
getContentPane().add(button4);
getContentPane().add(button5);
getContentPane().add(button6);
getContentPane().add(mulButton);
getContentPane().add(perButton);
getContentPane().add(button1);
getContentPane().add(button2);
getContentPane().add(button3);
getContentPane().add(minusButton);
getContentPane().add(fracButton);
getContentPane().add(button0);
getContentPane().add(symButton);
getContentPane().add(dotButton);
getContentPane().add(addButton);
getContentPane().add(equalButton);
}
public static void setMyLookAndFeel(){
try{
Font font = new Font("Dialog", Font.PLAIN, 9);
UIManager.put("Button.font",font);
UIManager.put("TextField.font",font);
}catch(Exception e){
e.printStackTrace();
}
}
class numBtnMonitor implements ActionListener{
Object object = null;
public void actionPerformed(ActionEvent e){
object = e.getSource();
try{
if (object == button0){
inputTxt.setText(c.append("0").toString());
}else if (object == button1){
inputTxt.setText(c.append("1").toString());
}else if (object == button2){
inputTxt.setText(c.append('2').toString());
}else if (object == button3){
inputTxt.setText(c.append('3').toString());
}else if (object == button4){
inputTxt.setText(c.append('4').toString());
}else if (object == button5){
inputTxt.setText(c.append('5').toString());
}else if (object == button6){
inputTxt.setText(c.append('6').toString());
}else if (object == button7){
inputTxt.setText(c.append('7').toString());
}else if (object == button8){
inputTxt.setText(c.append('8').toString());
}else if (object == button9){
inputTxt.setText(c.append('9').toString());
}
}catch(NullPointerException ex){
}
flag = 1;
}
}
class opBtnMonitor implements ActionListener{
Object object = null;
public void actionPerformed(ActionEvent e){
c = c.delete(0,c.length());
try{
object = e.getSource();
if (object == addButton){
a = inputTxt.getText();
op = '+';
}else if (object == minusButton){
a = inputTxt.getText();
op = '-';
}else if (object == mulButton){
a = inputTxt.getText();
op = '*';
}else if (object == divButton){
a = inputTxt.getText();
op = '/';
}
}catch(NullPointerException ex){
}
}
}
class rstMonitor implements ActionListener{
public void actionPerformed(ActionEvent e){
Object object = e.getSource();
if (object == equalButton){
if (flag == 1){
b = inputTxt.getText();
flag = 0;
}
switch(op){
case '+': a = BigNum.add(a,b);
inputTxt.setText(a);
break;
case '-': a = BigNum.minus(a,b);
inputTxt.setText(a);
break;
case '*': a = BigNum.multiplication(a,b);
inputTxt.setText(a);
break;
case '/': a = BigNum.divide(a,b);
inputTxt.setText(a);
break;
default: inputTxt.setText("0.");
break;
}
c = c.delete(0,c.length());
}
}
}
}
/*
* Calculator.java
*/
package liang;
import java.awt.Toolkit;
import java.awt.Dimension;
public class Calculator{
static CalculatorFrame window;
public static void main(String args[]){
window = new CalculatorFrame("计算器");
Toolkit theKit = window.getToolkit();
Dimension wndSize = theKit.getScreenSize();
CalculatorFrame.setMyLookAndFeel();
window.setBounds(wndSize.width/4, wndSize.height/4,
266,243);
window.setResizable(false);
window.setVisible(true);
}
}