《野猪乐园》显示文章详细内容: [展开] [回复] [网址] [举报] [屏蔽]
闻正
闻正目前处于离线状态
等    级:资深长老
经 验 值:1981
魅 力 值:474
龙    币:4575
积    分:2401.9
注册日期:2004-04-29
 
  查看闻正个人资料   给闻正发悄悄话   将闻正加入好友   搜索闻正所有发表过的文章   给闻正发送电子邮件      

王储做的计算器,11岁就成成码农肿么办?




import java.awt.*;
import javax.swing.*;
import java.text.*;
import java.util.*;
import java.awt.event.*;
import java.math.*;

public class KXCalculator extends JFrame implements ActionListener,ButtonData
{
JPanel p1,p2,p3,p4,p5,p6,p7,p8,p9;
Vector<Vector<String>> rowData=new Vector<Vector<String>>();
Vector<String> columnNames=new Vector<String>();
JButton eb,dms,exp,a,b,c,d,e,f,sin,cos,tan,pi,two,three,four,five,six,seven,eight,nine;
JTextField te,time;
Font font=new Font("华文中宋",Font.PLAIN,20);
Font font1=new Font("Courier New",Font.PLAIN,14);
Font font2=new Font("微软雅黑",Font.PLAIN,15);
Font font3=new Font("微软雅黑",Font.PLAIN,20);
SimpleDateFormat sdf,s;
DecimalFormat df;
JRadioButton hex,n,octal,binary;
String result="0";
String x="0";
String oper="";
JLabel b1;
JLabel b2;
JTable helpTable;
boolean isdot=false;
boolean ifResult=false;
static boolean isError=false;
int fuhaoClick=0;
int i=0;
ButtonGroup bg;
JMenuBar mb;
JMenu edit,look,help;
JMenuItem copy,paste,kexue,biaozhun,numfm,helpSubject,aboutCalc;
int resultc=0;
int pastcarry=10;

public KXCalculator()
{
super("Calculator");

try{
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}catch(Exception e){}

bg=new ButtonGroup();

hex=new JRadioButton(BNAMES[0]);
n=new JRadioButton(BNAMES[1],true);
octal=new JRadioButton(BNAMES[2]);
binary=new JRadioButton(BNAMES[3]);
hex.setFont(font2);
n.setFont(font2);
octal.setFont(font2);
binary.setFont(font2);
hex.addActionListener(this);
n.addActionListener(this);
octal.addActionListener(this);
binary.addActionListener(this);
hex.setToolTipText(HEX);

p1=new JPanel();
p2=new JPanel(new BorderLayout(5,5));
p3=new JPanel();
p4=new JPanel(new GridLayout(5,12,5,5));
p5=new JPanel(new GridLayout());
p6=new JPanel(new GridLayout());
p7=new JPanel(new FlowLayout(FlowLayout.LEFT));
p8=new JPanel(new BorderLayout());

mb=new JMenuBar();
edit=new JMenu("编辑(E)");
edit.setMnemonic('E');
look=new JMenu("查看(V)");
look.setMnemonic('V');
help=new JMenu("帮助(H)");
help.setMnemonic('H');
copy=new JMenuItem("复制(C)",'C');
copy.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C,ActionEvent.CTRL_MASK));
copy.addActionListener(this);
paste=new JMenuItem("粘贴(P)",'P');
paste.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_V, ActionEvent.CTRL_MASK));
paste.addActionListener(this);
biaozhun=new JMenuItem("标准型(T)",'T');
biaozhun.addActionListener(this);
kexue=new JMenuItem("科学型(S)",'S');
kexue.addActionListener(this);
numfm=new JMenuItem("数字分组(I)",'I');
numfm.addActionListener(this);
helpSubject=new JMenuItem("帮助主题(H)",'H');
helpSubject.addActionListener(this);
aboutCalc=new JMenuItem("关于计算器(A)",'A');
aboutCalc.addActionListener(this);

eb=this.addButton(p4,BNAMES[7], this, BLUE, true,true,E);
this.addButton(p4, BNAMES[8], this, MAGENTA, true,true,LBRACKET);
this.addButton(p4, BNAMES[9], this, MAGENTA, true,true,RBRACKET);
this.addButton(p4, null, null, null, false,true,null);
this.addButton(p4, BNAMES[10], this, RED, true,true,MC);
this.addButton(p4, null, null, null, false,true,null);
seven=this.addButton(p4, BNAMES[11], this, BLUE, true,true,NUM);
eight=this.addButton(p4, BNAMES[12], this, BLUE, true,true,NUM);
nine=this.addButton(p4, BNAMES[13], this, BLUE, true,true,NUM);
this.addButton(p4, BNAMES[14], this, RED, true,true,DIVIDE);
this.addButton(p4, BNAMES[15], this, RED, true,true,MOD);
this.addButton(p4, BNAMES[16], this, RED, true,true,AND);
dms=this.addButton(p4, BNAMES[17], this, MAGENTA, true,true,DMS);
exp=this.addButton(p4, BNAMES[18], this, MAGENTA, true,true,EXP);
this.addButton(p4, BNAMES[19], this, MAGENTA, true,true,LN);
this.addButton(p4, null, null, null, false,true,null);
this.addButton(p4, BNAMES[20], this, RED, true,true,MR);
this.addButton(p4, null, null, null, false,true,null);
four=this.addButton(p4, BNAMES[21], this, BLUE, true,true,NUM);
five=this.addButton(p4, BNAMES[22], this, BLUE, true,true,NUM);
six=this.addButton(p4, BNAMES[23], this, BLUE, true,true,NUM);
this.addButton(p4, BNAMES[24], this, RED, true,true,MULTIPLY);
this.addButton(p4, BNAMES[25], this, RED, true,true,OR);
this.addButton(p4, BNAMES[26], this, RED, true,true,XOR);
sin=this.addButton(p4, BNAMES[27], this, MAGENTA, true,true,SIN);
this.addButton(p4, BNAMES[28], this, MAGENTA, true,true,XPOWY);
this.addButton(p4, BNAMES[29], this, MAGENTA, true,true,LOG);
this.addButton(p4, null, null, null, false,true,null);
this.addButton(p4, BNAMES[30], this, RED, true,true,MS);
this.addButton(p4, null, null, null, false,true,null);
this.addButton(p4, BNAMES[31], this, BLUE, true,true,NUM);
two=this.addButton(p4, BNAMES[32], this, BLUE, true,true,NUM);
three=this.addButton(p4, BNAMES[33], this, BLUE, true,true,NUM);
this.addButton(p4, BNAMES[34], this, RED, true,true,SUBRACT);
this.addButton(p4, BNAMES[35], this, RED, true,true,LSH);
this.addButton(p4, BNAMES[36], this, RED, true,true,NOT);
cos=this.addButton(p4, BNAMES[37], this, MAGENTA, true,true,COS);
this.addButton(p4, BNAMES[38], this, MAGENTA, true,true,XPOW3);
this.addButton(p4, BNAMES[39], this, MAGENTA, true,true,NAN);
this.addButton(p4, null, null, null, false,true,null);
this.addButton(p4, BNAMES[40], this, RED, true,true,MPLUS);
this.addButton(p4, null, null, null, false,true,null);
this.addButton(p4, BNAMES[41], this, BLUE, true,true,NUM);
this.addButton(p4, BNAMES[42], this, BLUE, true,true,SIGN);
this.addButton(p4, BNAMES[43], this, BLUE, true,true,DOT);
this.addButton(p4, BNAMES[44], this, RED, true,true,ADD);
this.addButton(p4, BNAMES[45], this, RED, true,true,RESULT);
this.addButton(p4, BNAMES[46], this, RED, true, true,INT);
tan=this.addButton(p4, BNAMES[47], this, MAGENTA, true, true,TAN);
this.addButton(p4, BNAMES[48], this, MAGENTA, true, true,XPOW2);
this.addButton(p4, BNAMES[49], this, MAGENTA, true, true,RECIP);
this.addButton(p4, null, null, null, false,true,null);
pi=this.addButton(p4, BNAMES[50], this, BLUE, true, true,PI);
pi.setFont(new Font("华文中宋",Font.BOLD,17));
this.addButton(p4, null, null, null, false,true,null);
a=this.addButton(p4, BNAMES[51], this, BLUE, true, false,ZIMU);
b=this.addButton(p4, BNAMES[52], this, BLUE, true, false,ZIMU);
c=this.addButton(p4, BNAMES[53], this, BLUE, true, false,ZIMU);
d=this.addButton(p4, BNAMES[54], this, BLUE, true, false,ZIMU);
e=this.addButton(p4, BNAMES[55], this, BLUE, true, false,ZIMU);
f=this.addButton(p4, BNAMES[56], this, BLUE, true, false,ZIMU);

p7.add(hex); p7.add(n); p7.add(octal); p7.add(binary);
bg.add(hex); bg.add(n); bg.add(octal); bg.add(binary);

te=new JTextField(38);
te.setText("0");
te.setEditable(false);
te.setBackground(Color.white);
te.setHorizontalAlignment(JTextField.RIGHT);
te.setFont(font3);
te.setCursor(new Cursor(Cursor.TEXT_CURSOR));

sdf=new SimpleDateFormat("MM HH:mm:ss");
String t=sdf.format(new Date());
s=new SimpleDateFormat("HH:mm:ss");

time=new JTextField(7);
time.setEditable(false);
time.setBackground(Color.white);
time.setFont(new Font("华文中宋",Font.PLAIN,20));

columnNames.add("按钮名称");
columnNames.add("作用");
columnNames.add("快捷键");

for(int i=0;i<HELPDATA.length;i++){
Vector<String> row=new Vector<String>();
row.add(BNAMES[i]);
row.add(HELPDATA[i]);
row.add(KJJ[i]);
rowData.add(row);
}
helpTable=new JTable(rowData,columnNames);

if(t.substring(0, 2).equals("01"))
{
time.setText("Jan "+s.format(new Date()));
}
else if(t.substring(0, 2).equals("02"))
{
time.setText("Feb "+s.format(new Date()));
}
else if(t.substring(0, 2).equals("12"))
{
time.setText("Dec "+s.format(new Date()));
}
p7.add(time);

this.addButton(p7, BNAMES[4], this, Color.red, true, true,BACKSPACE);
this.addButton(p7, BNAMES[5], this, Color.red, true, true,CE);
this.addButton(p7, BNAMES[6], this, Color.red, true, true,C);

p1.add(te);
p8.add(p3,BorderLayout.NORTH);
p8.add(p4,BorderLayout.CENTER);

p2.add(p7,BorderLayout.NORTH);
p2.add(p8,BorderLayout.CENTER);
p2.add(p5,BorderLayout.WEST);
p2.add(p6,BorderLayout.SOUTH);

edit.add(copy); edit.add(paste);
look.add(biaozhun); look.add(kexue); look.addSeparator(); look.add(numfm);
help.add(helpSubject); help.addSeparator(); help.add(aboutCalc);

mb.add(edit);
mb.add(look);
mb.add(help);

this.setJMenuBar(mb);
this.add(p1,BorderLayout.NORTH);
this.add(p2,BorderLayout.CENTER);
this.setVisible(true);
this.setBounds(140, 140, 740, 370);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setResizable(false);
this.setIconImage(new ImageIcon("Images/calc.png").getImage());
}
public static void main(String[] args)
{
new KXCalculator();
}

public void actionPerformed(ActionEvent e)
{
String s=e.getActionCommand();
if(!isError){
if(s.equals("1"))inputnum('1');
else if(s.equals("2"))inputnum('2');
else if(s.equals("3"))inputnum('3');
else if(s.equals("4"))inputnum('4');
else if(s.equals("5"))inputnum('5');
else if(s.equals("6"))inputnum('6');
else if(s.equals("7"))inputnum('7');
else if(s.equals("8"))inputnum('8');
else if(s.equals("9"))inputnum('9');
else if(s.equals("0"))inputnum('0');
else if(s.equals("+"))
{
i=0;
isdot=false;
ifResult=true;
if(fuhaoClick==0){
result=te.getText();
}
if(fuhaoClick>=1){
result(te.getText());
}
fuhaoClick++;
oper="+";
}
else if(s.equals("-"))
{
i=0;
isdot=false;
ifResult=true;
if(fuhaoClick==0){
result=te.getText();
}
if(fuhaoClick>=1){
result(te.getText());
}
fuhaoClick++;
oper="-";
}
else if(s.equals("*"))
{
i=0;
isdot=false;
ifResult=true;
if(fuhaoClick==0){
result=te.getText();
}
if(fuhaoClick>=1){
result(te.getText());
}
oper="*";
fuhaoClick++;
}
else if(s.equals("/"))
{
i=0;
isdot=false;
ifResult=true;
if(fuhaoClick==0){
result=te.getText();
}
if(fuhaoClick>=1){
result(te.getText());
}
fuhaoClick++;
oper="/";
}
else if(s.equals("="))
{
i=0;
ifResult=false;
resultc++;
if(resultc==1){
result(te.getText());
}
}
else if(s.equals("."))
{
inputnum('.');
}
else if(s.equals(" CE "))
{
i=0;
te.setText("0");
result="0";
oper="";
fuhaoClick=0;
isdot=false;
}
else if(s.equals(" C "))
{
i=0;
te.setText("0");
result="0";
oper="";
fuhaoClick=0;
isdot=false;
}
else if(s.equals("%"))
{
if(fuhaoClick!=0){
i=0;
ifResult=false;
baifenbi(te.getText());
}
}
else if(s.equals("Backspace"))
{
if(te.getText().length()==1){
te.setText("0");
result="0";
isdot=false;
i=0;
return;
}
String s3=te.getText().substring(0, te.getText().length()-1);
result=s3;
te.setText(result);
isDot(result);
}
else if(s.equals("+/-")){
result=CalcMath.z$f(te.getText());te.setText(result);}
else if(s.equals("1/x")){
result=CalcMath.daoshu(te.getText());te.setText(result);isDot(result);}
else if(s.equals("sin")){
result=CalcMath.sin(Double.parseDouble(te.getText()));te.setText(result);isDot(result);}
else if(s.equals("cos")){
result=CalcMath.cos(Double.parseDouble(te.getText()));te.setText(result);isDot(result);}
else if(s.equals("log")){
result=CalcMath.log(Double.parseDouble(te.getText()));te.setText(result);isDot(result);}
else if(s.equals("π")){
result=CalcMath.PI;te.setText(result);isdot=true;}
else if(s.equals("e")){
result=CalcMath.E;te.setText(result);isdot=true;}
else if(s.equals("tan")){
result=CalcMath.tan(Double.parseDouble(te.getText()));te.setText(result);isDot(result);}
else if(s.equals("x^2")){
result=CalcMath.pow(te.getText(),"2").toString();isDot(result);te.setText(result);}
else if(s.equals("x^3")){
result=CalcMath.pow(te.getText(),"3").toString();isDot(result);te.setText(result);}
else if(s.equals("x^y"))
{
i=0;
isdot=false;
ifResult=true;
if(fuhaoClick==0){
result=te.getText();
}
if(fuhaoClick>=1){
result(te.getText());
}
fuhaoClick++;
oper="^";
}
else if(s.equals("n!"))
{
String s1=textCarry(pastcarry,10,te.getText());
String s2=calcJC(new BigDecimal(s1));
if(!isError){
if(s2.length()>=30&&n.isSelected()){
result=new DecimalFormat("0.##############################E0").format(new BigDecimal(s2));
}else{
result=textCarry(10,pastcarry,s2);
}
}else{
result=s2;
}
isDot(result);te.setText(result);
}
else if(s.equals("ln")){
result=CalcMath.ln(Double.parseDouble(te.getText()));te.setText(result);isDot(result);}
else if(s.equals("十六进制"))inputChangCarry(16);
else if(s.equals("十进制"))inputChangCarry(10);
else if(s.equals("八进制"))inputChangCarry(8);
else if(s.equals("二进制 "))inputChangCarry(2);
else if(s.equals("A"))inputnum('A');
else if(s.equals("B"))inputnum('B');
else if(s.equals("C"))inputnum('C');
else if(s.equals("D"))inputnum('D');
else if(s.equals("E"))inputnum('E');
else if(s.equals("F"))inputnum('F');
else if(s.equals("exp"))
{
i=0;
isdot=false;
ifResult=true;
if(fuhaoClick==0){
result=te.getText();
}
if(fuhaoClick>=1){
result(te.getText());
}
fuhaoClick++;
oper="exp";
}
else if(s.equals("Mod"))
{
i=0;
isdot=false;
ifResult=true;
if(fuhaoClick==0){
result=te.getText();
}
if(fuhaoClick>=1){
result(te.getText());
}
fuhaoClick++;
oper="%";
}
else if(s.equals("And"))
{
i=0;
isdot=false;
ifResult=true;
if(fuhaoClick==0){
result=te.getText();
}
if(fuhaoClick>=1){
result(te.getText());
}
fuhaoClick++;
oper="&";
}
else if(s.equals("Or"))
{
i=0;
isdot=false;
ifResult=true;
if(fuhaoClick==0){
result=te.getText();
}
if(fuhaoClick>=1){
result(te.getText());
}
fuhaoClick++;
oper="|";
}
else if(s.equals("Xor"))
{
i=0;
isdot=false;
ifResult=true;
if(fuhaoClick==0){
result=te.getText();
}
if(fuhaoClick>=1){
result(te.getText());
}
fuhaoClick++;
oper="Xor";
}
else if(s.equals("Lsh"))
{
i=0;
isdot=false;
ifResult=true;
if(fuhaoClick==0){
result=te.getText();
}
if(fuhaoClick>=1){
result(te.getText());
}
fuhaoClick++;
oper="<<";
}
else if(s.equals("Not"))
{
if(isError){
return;
}
String str=te.getText();
str=str.replace(",", "");
if(str.indexOf(".")!=-1){
str=str.substring(0, str.indexOf("."));
}
if(hex.isSelected()){
textCarry(16,10,str);
}else if(octal.isSelected()){
textCarry(8,10,str);
}else if(binary.isSelected()){
textCarry(2,10,str);
}

try{
int num=Integer.parseInt(str);
num=~num;

if(hex.isSelected()){
str=Integer.toHexString(num);
}else if(octal.isSelected()){
str=Integer.toOctalString(num);
}else if(binary.isSelected()){
str=Integer.toBinaryString(num);
}else{
str=num+"";
}
result=str;
te.setText(result);
isdot=false;
}catch(NumberFormatException nfe){
te.setText("输入数据异常,计算终止!");
isError=true;
return;
}
}
else if(s.equals("dms")){
dialog();
}
else if(s.equals("(")){
dialog();
}
else if(s.equals(")")){
dialog();
}
}
else
{
if(s.equals(" CE "))
{
te.setText("0");
oper="";
isError=false;
fuhaoClick=0;
result="0";
isdot=false;
}
else if(s.equals(" C "))
{
te.setText("0");
oper="";
isError=false;
fuhaoClick=0;
result="0";
isdot=false;
}
}
if(!s.equals("="))
{
resultc=0;
}
}

public void inputnum(char num){
i++;
if(ifResult){
if(num!='.'){
if(i==1){
te.setText("");
}
te.setText(te.getText().trim()+num);
}else{
if(!isdot){
te.setText(te.getText().trim()+num);
isdot=true;
}
}
}else{
if(num!='.'){
if(te.getText().equals("0")){
te.setText("");
te.setText(te.getText().trim()+num);
}else{
te.setText(te.getText().trim()+num);
}
}else{
if(!isdot){
te.setText(te.getText().trim()+num);
isdot=true;
}
}
}
fuhaoClick=0;
}
public void result(String x){
result=textCarry(pastcarry,10,result);
x=textCarry(pastcarry,10,x);
if(oper.equals("+")){
result=CalcMath.add(result,x).toString();}
else if(oper.equals("-")){
result=CalcMath.sub(result,x).toString();}
else if(oper.equals("*")){
result=CalcMath.mul(result,x).toString();}
else if(oper.equals("/")){
df=new DecimalFormat("#.##############################");
if(x.equals("0"))
{
te.setText("除数不能为0");
isError=true;
return;
}
result=df.format(CalcMath.div(result,x));}
else if(oper.equals("^")){
try{
if(CalcMath.pow(result,x).toString().length()>=30){
result=new DecimalFormat("0.##############################E0").format(CalcMath.pow(result,x));
}else{
result=CalcMath.pow(result,x).toString();
}
}catch(NumberFormatException e1){
te.setText("∞");
isError=true;
return;
}}
else if(oper.equals("%")){
try{
double a=Double.parseDouble(result);
double b=Double.parseDouble(x);
result=(a%b)+"";
}catch(NumberFormatException nfe){
te.setText("输入数据异常,计算终止!");
isError=true;
return;
}}
else if(oper.equals("&")){
try{
result=result.replace(",","");
result=result.substring(0, result.indexOf("."));
x=x.replace(",","");
x=x.substring(0,x.indexOf("."));
long a=Long.parseLong(result);
long b=Long.parseLong(x);
result=(a&b)+"";
}catch(NumberFormatException nfe){
te.setText("输入数据异常,计算终止!");
isError=true;
return;
}}
else if(oper.equals("|")){
try{
result=result.replace(",","");
result=result.substring(0, result.indexOf("."));
x=x.replace(",","");
x=x.substring(0,x.indexOf("."));
long a=Long.parseLong(result);
long b=Long.parseLong(x);
result=(a|b)+"";
}catch(NumberFormatException nfe){
te.setText("输入数据异常,计算终止!");
isError=true;
return;
}}
else if(oper.equals("Xor")){
try{
result=result.replace(",","");
result=result.substring(0, result.indexOf("."));
x=x.replace(",","");
x=x.substring(0,x.indexOf("."));
long a=Long.parseLong(result);
long b=Long.parseLong(x);
result=(a^b)+"";
}catch(NumberFormatException nfe){
te.setText("输入数据异常,计算终止!");
isError=true;
return;
}}
else if(oper.equals("<<")){
try{
result=result.replace(",","");
result=result.substring(0, result.indexOf("."));
x=x.replace(",","");
x=x.substring(0,x.indexOf("."));
long a=Long.parseLong(result);
long b=Long.parseLong(x);
result=(a<<b)+"";
}catch(NumberFormatException nfe){
te.setText("输入数据异常,计算终止!");
isError=true;
return;
}}
result=textCarry(10,pastcarry,result);
if(result.endsWith(".0")){
Double d=Double.parseDouble(result);
Integer b=d.intValue();
result=b.toString();
}
isDot(result);
te.setText(result);
}
public void baifenbi(String x)
{
BigDecimal b1=new BigDecimal(result);
BigDecimal b2=new BigDecimal(x);
BigDecimal r=new BigDecimal(100);
result=(b1.multiply(b2)).divide(r).toString();
te.setText(result);
}
private JButton addButton(JPanel panel,String name,ActionListener a,Color color,boolean b,boolean disable,String tip){
JButton but=new JButton(name);
but.setEnabled(disable);

but.setForeground(color);
but.setToolTipText(tip);
but.addActionListener(a);
but.setVisible(b);
but.setFont(font1);

panel.add(but);
return but;
}




public String calcJC(BigDecimal n)
{
if(n.doubleValue()<0)
{
isError=true;
return "函数输入无效";
}
else if(n.doubleValue()==0)
{
return "0";
}
else if(n.doubleValue()>=22222)
{
int type=JOptionPane.showConfirmDialog(null,"数值过大,操作时间会比较长,是否继续?","提示",JOptionPane.OK_CANCEL_OPTION,JOptionPane.WARNING_MESSAGE);
if(type==JOptionPane.CANCEL_OPTION){
isError=true;
return "操作已取消";
}else{
return n.multiply(calcJC1(n.subtract(new BigDecimal(1)))).toString();
}
}
else
{
return n.multiply(calcJC1(n.subtract(new BigDecimal(1)))).toString();
}
}
public BigDecimal calcJC1(BigDecimal n)
{
BigDecimal b=new BigDecimal(1);

if(n.doubleValue()<0)
{
isError=true;
te.setText("函数输入无效");
}
if(n.doubleValue()==0)
{
return new BigDecimal(0);
}
for(int i=1;i<=n.intValue();i++)
{
b=b.multiply(new BigDecimal(i));
}
return b;
}

public void inputChangCarry(int format)
{
String str=te.getText();
if(format==16)
{
enabled(format);
String str4=CalcMath.decto(CalcMath.todec(str, pastcarry), 16);
if(str4.endsWith(".0")){
Double d=Double.parseDouble(str4);
Integer b=d.intValue();
str4=b.toString();
}
te.setText(str4);
pastcarry=16;
}
else if(format==10)
{
enabled(format);
String str4=CalcMath.decto(CalcMath.todec(str, pastcarry), 10);
if(str4.endsWith(".0")){
Double d=Double.parseDouble(str4);
Integer b=d.intValue();
str4=b.toString();
}
te.setText(str4);
pastcarry=10;
}
else if(format==8)
{
enabled(format);
String str4=CalcMath.decto(CalcMath.todec(str, pastcarry), 8);
if(str4.endsWith(".0")){
Double d=Double.parseDouble(str4);
Integer b=d.intValue();
str4=b.toString();
}
te.setText(str4);
pastcarry=8;
}
else if(format==2)
{
enabled(format);
String str4=CalcMath.decto(CalcMath.todec(str, pastcarry), 2);
if(str4.endsWith(".0")){
Double d=Double.parseDouble(str4);
Integer b=d.intValue();
str4=b.toString();
}
te.setText(str4);
pastcarry=2;
}
}
public String textCarry(int oldcarry,int newcarry,String num){
return CalcMath.decto(CalcMath.todec(num, oldcarry), newcarry);
}
public void enabled(int carry)
{
sin.setEnabled(carry==10);
cos.setEnabled(carry==10);
tan.setEnabled(carry==10);
exp.setEnabled(carry==10);
pi.setEnabled(carry==10);
eb.setEnabled(carry==10);
dms.setEnabled(carry==10);

a.setEnabled(carry==16);
b.setEnabled(carry==16);
c.setEnabled(carry==16);
d.setEnabled(carry==16);
e.setEnabled(carry==16);
f.setEnabled(carry==16);

nine.setEnabled(carry==16||carry==10);
eight.setEnabled(carry==16||carry==10);
seven.setEnabled(carry!=2);
six.setEnabled(carry!=2);
five.setEnabled(carry!=2);
four.setEnabled(carry!=2);
three.setEnabled(carry!=2);
two.setEnabled(carry!=2);
}
private void isDot(String str)
{
for(int i=0;i<str.length();i++){
if(str.charAt(i)!='.'){
isdot=false;
}else{
isdot=true;
break;
}
}
}
public void dialog(){

try{
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}catch(Exception e){}
JOptionPane.showMessageDialog(this, "对不起,此功能未开放,如有问题,请联系计算机管理员!n客服电话:13611274444 。或者到官网下载完整版。 <html><a href='http://www.51zxw.net'>点击此链接下载</a></html>","错误",JOptionPane.ERROR_MESSAGE);
JDialog d=new JDialog();
d.setTitle("错误");
String help="<html><p style='font-size:10px;color:red'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; 对不起,此功能未开放,如有问题,请联系计算机管理员!<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;客服电话:13611274444 。或者到官网下载计算器v1.0完整版。 <a href='http://www.51zxw.net'>点击此链接下载</a></p></html>";
JEditorPane f=new JEditorPane("text/html",help);
f.setEditable(false);
d.setBounds(400, 280, 600, 150);
d.setVisible(true);
d.add(f);
}
}
class CalcMath
{
public static final int DEF_DIV_SCALE =6000;
public static final String E="2.7182818284590452354";
public static final String PI="3.14159265358979323846";

public static BigDecimal add(String a,String b)
{
BigDecimal b1=new BigDecimal(a);
BigDecimal b2=new BigDecimal(b);
return b1.add(b2);
}
public static BigDecimal sub(String a,String b)
{
BigDecimal b1=new BigDecimal(a);
BigDecimal b2=new BigDecimal(b);
return b1.subtract(b2);
}
public static BigDecimal mul(String a,String b)
{
BigDecimal b1=new BigDecimal(a);
BigDecimal b2=new BigDecimal(b);
return b1.multiply(b2);
}
public static BigDecimal div(String a,String b) throws ArithmeticException
{
BigDecimal b1=new BigDecimal(a);
BigDecimal b2=new BigDecimal(b);
if(b2.doubleValue()==0) {
throw new ArithmeticException();
}
return b1.divide(b2,6000,BigDecimal.ROUND_HALF_UP);
}
public static BigDecimal pow(String a,String b)
{
BigDecimal b1=new BigDecimal(a);
BigDecimal b2=new BigDecimal(b);
return new BigDecimal(Math.pow(b1.doubleValue(),b2.doubleValue()));
}
public static String z$f(String a)
{
String sa;
if(a.charAt(0)=='-'){
sa=a.substring(1);
}else{
sa="-"+a;
}
return sa;
}
public static String daoshu(String a)
{
Double b=Double.parseDouble(a);
if(b==0){
KXCalculator.isError=true;
return "除数不能为0";
}else{
b=1/b;
if(b.toString().endsWith(".0")){
Integer h=b.intValue();
return h.toString();
}else{
return b.toString();
}
}
}
public static String decto(BigDecimal bdnum,int newcarry)
{
double num=bdnum.doubleValue();
String hexnum="0123456789abcdef";
boolean neg=(num<0);
if (newcarry==10 || num==0) return num+"";

num=Math.abs(num);
String newnum="";
while (num!=0)
{
newnum=hexnum.charAt((int)(num%newcarry))+newnum;
num=Math.floor(num/newcarry);
}
if (neg)
newnum="-"+newnum;
return newnum.toUpperCase();
}
public static BigDecimal todec(String num,int oldcarry)
{
String hexnum="0123456789abcdef";
num=num.toLowerCase();
num=num.replace(",","");
if (oldcarry==10 ||num.equals("0")) return new BigDecimal(num);
boolean neg=(num.charAt(0)=='-');
if (neg) num=num.substring(1);
double newnum=0;
for (int index=1;index<=num.length();index++)
newnum=newnum*oldcarry+hexnum.indexOf(num.charAt(index-1));

if (neg)
newnum=-newnum;

BigDecimal bd=new BigDecimal(newnum);
return bd;
}
public static String sin(double a)
{
a=StrictMath.sin(StrictMath.toRadians(a));
if(a%2==0||a%2==1){
return String.valueOf((int)a);
}else{
return String.valueOf(a);
}
}
public static String cos(double a)
{
a=StrictMath.cos(StrictMath.toRadians(a));
if(a%2==0||a%2==1){
return String.valueOf((int)a);
}else{
return String.valueOf(a);
}
}
public static String tan(double a)
{
a=StrictMath.tan(StrictMath.toRadians(a));
if(a%2==0||a%2==1){
return String.valueOf((int)a);
}else{
return String.valueOf(a);
}
}
public static String log(double a)
{
if(a<=0){
KXCalculator.isError=true;
return "函数输入无效";
}else{
a=Math.log10(a);
if(a%2==0||a%2==1){
return String.valueOf((int)a);
}else{
return String.valueOf(a);
}
}
}
public static String ln(double a)
{
if(a<=0){
KXCalculator.isError=true;
return "函数输入无效";
}else{
a=Math.log(a);
if(a%2==0||a%2==1){
return String.valueOf((int)a);
}else{
return String.valueOf(a);
}
}
}
}
2013-12-10 20:47:21   此文章已经被查看841次   
 相关文章: [回复]  [顶端] 



  您必须登录论坛才可以发表文章:
 
用户名:   密码:   记住密码:    (忘记密码 注册




版权所有 回龙观社区网 经营许可证编号:京B2-20201639 昌公网安备1101140035号

举报电话:010-86468600-5 举报邮箱: