计算器的JAVA代码
本帖最后由 天使怪盗Dick 于 2011-1-7 12:54 编辑import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
//界面
public class jiemian extends JFrame
{
JFrame c;
JTextField tf=new JTextField(23);
JButton b1=new JButton("1");
JButton b2=new JButton("2");
JButton b3=new JButton("3");
JButton b4=new JButton("4");
JButton b5=new JButton("5");
JButton b6=new JButton("6");
JButton b7=new JButton("7");
JButton b8=new JButton("8");
JButton b9=new JButton("9");
JButton b0=new JButton("0");
JButton bjia=new JButton("+");
JButton bjian=new JButton("-");
JButton bcheng=new JButton("*");
JButton bchu=new JButton("/");
JButton blk=new JButton("(");
JButton brk=new JButton(")");
JButton bdeng=new JButton("=");
JButton bdian=new JButton(".");
JButton bfu=new JButton("+/-");
JButton bmc=new JButton("MC");
JPanel p1=new JPanel();
JPanel p2=new JPanel();
JPanel p3=new JPanel();
String tf1=new String();
void disp(){
c=new JFrame("计算器");
c.setResizable(false);
c.setBounds(200,200,300,300);
p3.add(tf);
c.add(p3,"North");
c.add(p1,"West");
p1.setLayout(new GridLayout(4,3,4,3));
p1.add(b1);
p1.add(b2);
p1.add(b3);
p1.add(b4);
p1.add(b5);
p1.add(b6);
p1.add(b7);
p1.add(b8);
p1.add(b9);
p1.add(b0);
p1.add(bdian);
p1.add(bfu);
b1.addActionListener(new Press1());
b2.addActionListener(new Press2());
b3.addActionListener(new Press3());
b4.addActionListener(new Press4());
b5.addActionListener(new Press5());
b6.addActionListener(new Press6());
b7.addActionListener(new Press7());
b8.addActionListener(new Press8());
b9.addActionListener(new Press9());
b0.addActionListener(new Press0());
bjia.addActionListener(new Pressjia());
bjian.addActionListener(new Pressjian());
bcheng.addActionListener(new Presscheng());
bchu.addActionListener(new Presschu());
blk.addActionListener(new Presslk());
brk.addActionListener(new Pressrk());
bmc.addActionListener(new Pressmc());
bdeng.addActionListener(new Pressdeng());
bfu.addActionListener(new Pressfu());
c.add(p2,"East");
p2.setLayout(new GridLayout(4,2,2,2));
p2.add(bjia);
p2.add(bmc);
p2.add(bjian);
p2.add(blk);
p2.add(bcheng);
p2.add(brk);
p2.add(bchu);
p2.add(bdeng);
c.setVisible(true);
}
class Press0 implements ActionListener{
public void actionPerformed(ActionEvent e){
String a=tf.getText();
tf.setText(a+"0");
tf1=tf1+"0";
}
}
class Press1 implements ActionListener{
public void actionPerformed(ActionEvent e){
String a=tf.getText();
tf.setText(a+"1");
tf1=tf1+"1";
}
}
class Press2 implements ActionListener{
public void actionPerformed(ActionEvent e){
String a=tf.getText();
tf.setText(a+"2");
tf1=tf1+"2";
}
}
class Press3 implements ActionListener{
public void actionPerformed(ActionEvent e){
String a=tf.getText();
tf.setText(a+"3");
tf1=tf1+"3";
}
}
class Press4 implements ActionListener{
public void actionPerformed(ActionEvent e){
String a=tf.getText();
tf.setText(a+"4");
tf1=tf1+"4";
}
}
class Press5 implements ActionListener{
public void actionPerformed(ActionEvent e){
String a=tf.getText();
tf.setText(a+"5");
tf1=tf1+"5";
}
}
class Press6 implements ActionListener{
public void actionPerformed(ActionEvent e){
String a=tf.getText();
tf.setText(a+"6");
tf1=tf1+"6";
}
}
class Press7 implements ActionListener{
public void actionPerformed(ActionEvent e){
String a=tf.getText();
tf.setText(a+"7");
tf1=tf1+"7";
}
}
class Press8 implements ActionListener{
public void actionPerformed(ActionEvent e){
String a=tf.getText();
tf.setText(a+"8");
tf1=tf1+"8";
}
}
class Press9 implements ActionListener{
public void actionPerformed(ActionEvent e){
String a=tf.getText();
tf.setText(a+"9");
tf1=tf1+"9";
}
}
class Pressjia implements ActionListener{
public void actionPerformed(ActionEvent e){
String a=tf.getText();
tf.setText(a+"+");
tf1=tf1+"+";
}
}
class Pressjian implements ActionListener{
public void actionPerformed(ActionEvent e){
String a=tf.getText();
tf.setText(a+"-");
tf1=tf1+"-";
}
}
class Presscheng implements ActionListener{
public void actionPerformed(ActionEvent e){
String a=tf.getText();
tf.setText(a+"*");
tf1=tf1+"*";
}
}
class Presschu implements ActionListener{
public void actionPerformed(ActionEvent e){
String a=tf.getText();
tf.setText(a+"/");
tf1=tf1+"/";
}
}
class Presslk implements ActionListener{
public void actionPerformed(ActionEvent e){
String a=tf.getText();
tf.setText(a+"(");
tf1=tf1+"(";
}
}
class Pressrk implements ActionListener{
public void actionPerformed(ActionEvent e){
String a=tf.getText();
tf.setText(a+")");
tf1=tf1+")";
}
}
class Pressfu implements ActionListener{
public void actionPerformed(ActionEvent e){
String a=tf.getText();
tf.setText(a+"-");
tf1=tf1+"0-";
}
}
class Pressmc implements ActionListener{
public void actionPerformed(ActionEvent e){
tf.setText("");
tf1=new String();
}
}
class Pressdeng implements ActionListener{
public void actionPerformed(ActionEvent e){
String f=tf1;
int i=0,j=0,n=0,flag=1;char []d=new char;char []a=new char;char x;
f.getChars(0,f.length(),a,0);a='#';a='\0';
ysf b=new ysf();
sz c=new sz();
//b.push_ysf('#');
while(a!='#'){
if(a=='('){
b.push_ysf('(');i++;}
else if(a==')'){
if(b.i!=-1){
x=b.top_ysf();
if(x=='(')b.cz_ysf();
else flag=0;i++;}
else {tf.setText("括号不匹配");i++;flag=0;}
//if(flag==0)
//System.out.print("括号不匹配!");
}
else i++;
}
if(b.empty_ysf()==0&&flag==1)
tf.setText("括号不匹配");
if(b.empty_ysf()==1&&flag==1){
i=0;b.push_ysf('#');
while(b.i!=-1&&a!='\0'){
if(a>='0'&& a<='9'||a=='.'){
d=a;}
else{
if(a!='#')d=' ';
switch(a){
case'(':b.push_ysf('(');break;
case')':x=b.cz_ysf();
while(x!='('){
d=x;
x=b.cz_ysf();
}break;
default:x=b.top_ysf();while(b.i!=-1&&b.bj(x)>=b.bj(a)){
if(x!='#')d=x;
b.cz_ysf();if(b.i!=-1)x=b.top_ysf();
}
if(a!='#')b.push_ysf(a);
}
}
i++;
}
d='#';System.out.print(d);
char []num=new char;double z,t1,t2,temp;i=0;
while(d!='#'){
if(d>='0'&&d<='9'||d=='.'){
for(j=0;d>='0'&&d<='9'||d=='.'; i++)num=d;
num='\0';
z=c.ctof(num);
c.push_sz(z);}
if(d==' ')i++;
if(d=='+'||d=='-'||d=='*'||d=='/')
{
t2=c.cz_sz();
t1=c.cz_sz();
char m=d;
switch(m)
{
case '+':temp=t1+t2;c.push_sz(temp);break;
case '-':temp=t1-t2;c.push_sz(temp);break;
case '*':temp=t1*t2;c.push_sz(temp);break;
case '/':temp=t1/t2;c.push_sz(temp);break;
}
i++;}
}
double t3=c.cz_sz();
//sc.setText();
tf.setText(f+"="+t3+"");}
}
}
public static void main(String [] args){
jiemian a=new jiemian();
a.disp();
}
}
class ysf{
char ysf[];
int i;
ysf(){
ysf=new char;
i=-1;
}
char cz_ysf(){
i--;
return ysf;
}
char top_ysf(){
return ysf;
}
int empty_ysf(){
if(i==-1)
return 1;
return 0;
}
void push_ysf(char a){i++;
ysf=a;
}
int bj(char a){
if(a=='+'||a=='-') return 1;
else if(a=='#') return -1;
else if(a=='*'||a=='/') return 2;
elsereturn 0;
}
}
class sz{
double sz[];
int i;
sz(){
sz=new double;
i=-1;
}
double cz_sz(){
i--;
return sz;
}
double top_sz(){
return sz;
}
int empty_sz(){
if(i==-1)
return 1;
return 0;
}
void push_sz(double a){
i++;
sz=a;
}
double ctof(char c[]){
int i,j,n=0;while(c!='\0')n++;
int x=0;
double y=0.0;
for(i=0;i<n;i++){
if(c=='.')break;
else x=(x+(c-48))*10;
}
x=x/10;
if(c=='.')
for(j=n-1;j>i;j--)y=(y+(c-48))*0.1;
return x+y;
}
} JAVA什么的……最讨厌了
上次JAVA考试老师竟然叫我们全体重考……也不想想我们成绩为什么全都这么差 又想起来我那python大作业还没做完...
准备做个类似计算器的东西 忽然想起自己十年前用C (cweb)写过一个计算器的,刚刚挖出来居然还能用...
http://paste.pocoo.org/show/316430/
页:
[1]