设为首页收藏本站喵玉殿官方微博

 找回密码
 少女注册中
搜索
查看: 5286|回复: 3

[编程算法] 计算器的JAVA代码

[复制链接]
发表于 2011-1-7 12:37:23 | 显示全部楼层 |阅读模式
本帖最后由 天使怪盗Dick 于 2011-1-7 12:54 编辑
  1. import java.awt.*;
  2. import java.awt.event.*;
  3. import javax.swing.*;
  4. //界面
  5. public class jiemian extends JFrame
  6. {
  7.      JFrame c;
  8.          
  9.      JTextField tf=new JTextField(23);
  10.    
  11.      JButton b1=new JButton("1");
  12.      JButton b2=new JButton("2");
  13.      JButton b3=new JButton("3");
  14.      JButton b4=new JButton("4");
  15.      JButton b5=new JButton("5");
  16.      JButton b6=new JButton("6");
  17.      JButton b7=new JButton("7");
  18.      JButton b8=new JButton("8");
  19.      JButton b9=new JButton("9");
  20.      JButton b0=new JButton("0");
  21.      JButton bjia=new JButton("+");
  22.      JButton bjian=new JButton("-");
  23.      JButton bcheng=new JButton("*");
  24.      JButton bchu=new JButton("/");
  25.      JButton blk=new JButton("(");
  26.      JButton brk=new JButton(")");
  27.      JButton bdeng=new JButton("=");
  28.      JButton bdian=new JButton(".");
  29.      JButton bfu=new JButton("+/-");
  30.      JButton bmc=new JButton("MC");
  31.      JPanel p1=new JPanel();
  32.      JPanel p2=new JPanel();
  33.      JPanel p3=new JPanel();
  34.      String tf1=new String();
  35. void disp(){
  36.   c=new JFrame("计算器");
  37.   c.setResizable(false);
  38.      c.setBounds(200,200,300,300);
  39.      p3.add(tf);
  40.      c.add(p3,"North");
  41.      c.add(p1,"West");
  42.      p1.setLayout(new GridLayout(4,3,4,3));
  43.      p1.add(b1);
  44.      p1.add(b2);
  45.      p1.add(b3);
  46.      p1.add(b4);
  47.      p1.add(b5);
  48.      p1.add(b6);
  49.      p1.add(b7);
  50.      p1.add(b8);
  51.      p1.add(b9);
  52.      p1.add(b0);
  53.      p1.add(bdian);
  54.      p1.add(bfu);
  55.      b1.addActionListener(new Press1());
  56.      b2.addActionListener(new Press2());
  57.      b3.addActionListener(new Press3());
  58.      b4.addActionListener(new Press4());
  59.      b5.addActionListener(new Press5());
  60.      b6.addActionListener(new Press6());
  61.      b7.addActionListener(new Press7());
  62.      b8.addActionListener(new Press8());
  63.      b9.addActionListener(new Press9());
  64.      b0.addActionListener(new Press0());
  65.      bjia.addActionListener(new Pressjia());
  66.      bjian.addActionListener(new Pressjian());
  67.      bcheng.addActionListener(new Presscheng());
  68.      bchu.addActionListener(new Presschu());
  69.      blk.addActionListener(new Presslk());
  70.      brk.addActionListener(new Pressrk());
  71.      bmc.addActionListener(new Pressmc());
  72.      bdeng.addActionListener(new Pressdeng());
  73.      bfu.addActionListener(new Pressfu());
  74.      c.add(p2,"East");
  75.      p2.setLayout(new GridLayout(4,2,2,2));
  76.      p2.add(bjia);
  77.      p2.add(bmc);
  78.      p2.add(bjian);
  79.      p2.add(blk);
  80.      p2.add(bcheng);
  81.      p2.add(brk);
  82.      p2.add(bchu);
  83.      p2.add(bdeng);

  84.      c.setVisible(true);
  85. }
  86.     class Press0 implements ActionListener{
  87.   public void actionPerformed(ActionEvent e){
  88.    String a=tf.getText();
  89.    tf.setText(a+"0");
  90.    tf1=tf1+"0";
  91.    }
  92.   
  93. }
  94. class Press1 implements ActionListener{
  95.   public void actionPerformed(ActionEvent e){
  96.    String a=tf.getText();
  97.    tf.setText(a+"1");
  98.    tf1=tf1+"1";
  99.   }
  100. }
  101. class Press2 implements ActionListener{
  102.   public void actionPerformed(ActionEvent e){
  103.    String a=tf.getText();
  104.    tf.setText(a+"2");
  105.    tf1=tf1+"2";
  106.   }
  107. }
  108. class Press3 implements ActionListener{
  109.   public void actionPerformed(ActionEvent e){
  110.    String a=tf.getText();
  111.    tf.setText(a+"3");
  112.    tf1=tf1+"3";
  113.   }
  114. }
  115. class Press4 implements ActionListener{
  116.   public void actionPerformed(ActionEvent e){
  117.    String a=tf.getText();
  118.    tf.setText(a+"4");
  119.    tf1=tf1+"4";
  120.   }
  121. }
  122. class Press5 implements ActionListener{
  123.   public void actionPerformed(ActionEvent e){
  124.    String a=tf.getText();
  125.    tf.setText(a+"5");
  126.    tf1=tf1+"5";
  127.   }
  128. }
  129. class Press6 implements ActionListener{
  130.   public void actionPerformed(ActionEvent e){
  131.    String a=tf.getText();
  132.    tf.setText(a+"6");
  133.    tf1=tf1+"6";
  134.   }
  135. }
  136. class Press7 implements ActionListener{
  137.   public void actionPerformed(ActionEvent e){
  138.    String a=tf.getText();
  139.    tf.setText(a+"7");
  140.    tf1=tf1+"7";
  141.   }
  142. }
  143. class Press8 implements ActionListener{
  144.   public void actionPerformed(ActionEvent e){
  145.    String a=tf.getText();
  146.    tf.setText(a+"8");
  147.    tf1=tf1+"8";
  148.   }
  149. }
  150. class Press9 implements ActionListener{
  151.   public void actionPerformed(ActionEvent e){
  152.    String a=tf.getText();
  153.    tf.setText(a+"9");
  154.    tf1=tf1+"9";
  155.   }
  156. }
  157. class Pressjia implements ActionListener{
  158.   public void actionPerformed(ActionEvent e){
  159.    String a=tf.getText();
  160.    tf.setText(a+"+");
  161.    tf1=tf1+"+";
  162.   }
  163. }
  164. class Pressjian implements ActionListener{
  165.   public void actionPerformed(ActionEvent e){
  166.    String a=tf.getText();
  167.    tf.setText(a+"-");
  168.    tf1=tf1+"-";
  169.   }
  170. }
  171. class Presscheng implements ActionListener{
  172.   public void actionPerformed(ActionEvent e){
  173.    String a=tf.getText();
  174.    tf.setText(a+"*");
  175.    tf1=tf1+"*";
  176.   }
  177. }
  178. class Presschu implements ActionListener{
  179.   public void actionPerformed(ActionEvent e){
  180.    String a=tf.getText();
  181.    tf.setText(a+"/");
  182.    tf1=tf1+"/";
  183.   }
  184. }
  185. class Presslk implements ActionListener{
  186.   public void actionPerformed(ActionEvent e){
  187.    String a=tf.getText();
  188.    tf.setText(a+"(");
  189.    tf1=tf1+"(";
  190.   }
  191. }
  192. class Pressrk implements ActionListener{
  193.   public void actionPerformed(ActionEvent e){
  194.    String a=tf.getText();
  195.    tf.setText(a+")");
  196.    tf1=tf1+")";
  197.   }
  198. }
  199. class Pressfu implements ActionListener{
  200.   public void actionPerformed(ActionEvent e){
  201.    String a=tf.getText();
  202.    tf.setText(a+"-");
  203.    tf1=tf1+"0-";
  204.   }
  205. }
  206. class Pressmc implements ActionListener{
  207.   public void actionPerformed(ActionEvent e){   
  208.    tf.setText("");
  209.    tf1=new String();
  210.   }
  211. }
  212. class Pressdeng implements ActionListener{
  213.   public void actionPerformed(ActionEvent e){
  214.    String f=tf1;
  215.    
  216.    int i=0,j=0,n=0,flag=1;char []d=new char[100];char []a=new char[100];char x;
  217.    f.getChars(0,f.length(),a,0);a[f.length()]='#';a[f.length()+1]='\0';
  218.    
  219.      
  220.    ysf b=new ysf();
  221.    sz c=new sz();
  222.    //b.push_ysf('#');
  223.    while(a[i]!='#'){
  224.    
  225.      if(a[i]=='('){
  226.       b.push_ysf('(');i++;}
  227.      else if(a[i]==')'){
  228.       if(b.i!=-1){
  229.        x=b.top_ysf();
  230.          if(x=='(')b.cz_ysf();
  231.          else flag=0;i++;}
  232.       else {tf.setText("括号不匹配");i++;flag=0;}
  233.       //if(flag==0)
  234.       //System.out.print("括号不匹配!");
  235.       }
  236.      else i++;
  237.      
  238.     }
  239.     if(b.empty_ysf()==0&&flag==1)
  240.    
  241.    
  242.     tf.setText("括号不匹配");
  243.    
  244.     if(b.empty_ysf()==1&&flag==1){
  245.     i=0;b.push_ysf('#');
  246.     while(b.i!=-1&&a[i]!='\0'){
  247.      if(a[i]>='0'&& a[i]<='9'||a[i]=='.'){
  248.         
  249.                  d[j++]=a[i];}
  250.               else{
  251.                if(a[i]!='#')d[j++]=' ';
  252.                switch(a[i]){
  253.                 case'(':b.push_ysf('(');break;
  254.                 case')':x=b.cz_ysf();
  255.                         while(x!='('){
  256.                        d[j++]=x;
  257.                        x=b.cz_ysf();
  258.                          }break;
  259.                    default:x=b.top_ysf();while(b.i!=-1&&b.bj(x)>=b.bj(a[i])){
  260.                  if(x!='#')d[j++]=x;
  261.                  b.cz_ysf();if(b.i!=-1)x=b.top_ysf();
  262.                  }
  263.                  if(a[i]!='#')b.push_ysf(a[i]);
  264.                 }
  265.                
  266.                }
  267.               
  268.               i++;
  269.     }
  270.     d[j]='#';System.out.print(d);
  271.    
  272.     char []num=new char[30];double z,t1,t2,temp;i=0;
  273.     while(d[i]!='#'){
  274.    
  275.       if(d[i]>='0'&&d[i]<='9'||d[i]=='.'){
  276.       
  277.                          for(j=0;d[i]>='0'&&d[i]<='9'||d[i]=='.'; i++)num[j++]=d[i];
  278.                        num[j]='\0';  
  279.                       z=c.ctof(num);
  280.    
  281.                       c.push_sz(z);}
  282.      
  283.     if(d[i]==' ')i++;
  284.     if(d[i]=='+'||d[i]=='-'||d[i]=='*'||d[i]=='/')
  285.               {
  286.              t2=c.cz_sz();
  287.              t1=c.cz_sz();
  288.              char m=d[i];
  289.              switch(m)
  290.              {
  291.               case '+':temp=t1+t2;c.push_sz(temp);break;
  292.               case '-':temp=t1-t2;c.push_sz(temp);break;
  293.               case '*':temp=t1*t2;c.push_sz(temp);break;
  294.               case '/':temp=t1/t2;c.push_sz(temp);break;
  295.              }
  296.             
  297.     i++;}
  298.     }
  299.    
  300.    
  301.    
  302.    
  303.    
  304.    
  305.     double t3=c.cz_sz();
  306.    
  307.    //sc.setText();
  308.    tf.setText(f+"="+t3+"");}
  309.    }
  310.   
  311. }

  312. public static void main(String [] args){
  313.   jiemian a=new jiemian();
  314.   a.disp();
  315. }
  316. }
  317. class ysf{
  318. char ysf[];
  319. int i;
  320. ysf(){
  321.   ysf=new char[100];
  322.   i=-1;
  323. }
  324. char cz_ysf(){
  325.   i--;
  326.   return ysf[i+1];
  327.   
  328. }
  329. char top_ysf(){
  330.   
  331.    
  332.                   
  333.   return ysf[i];
  334.   
  335. }
  336. int empty_ysf(){
  337.   if(i==-1)
  338.   return 1;
  339.   return 0;
  340. }
  341. void push_ysf(char a){i++;
  342.   ysf[i]=a;
  343.   
  344.      
  345. }
  346. int bj(char a){
  347.   if(a=='+'||a=='-') return 1;
  348. else if(a=='#') return -1;
  349. else if(a=='*'||a=='/') return 2;
  350. else  return 0;

  351. }
  352. }
  353. class sz{
  354. double sz[];
  355. int i;
  356. sz(){
  357.   sz=new double[100];
  358.   i=-1;
  359. }
  360. double cz_sz(){
  361.   i--;
  362.   return sz[i+1];
  363.   
  364. }
  365. double top_sz(){
  366.   
  367.   return sz[i];
  368. }
  369. int empty_sz(){
  370.   if(i==-1)
  371.   return 1;
  372.   return 0;
  373. }
  374. void push_sz(double a){
  375.   i++;
  376.   sz[i]=a;
  377.   
  378. }
  379. double ctof(char c[]){
  380. int i,j,n=0;while(c[n]!='\0')n++;
  381. int x=0;
  382. double y=0.0;
  383. for(i=0;i<n;i++){
  384.     if(c[i]=='.')break;
  385.     else x=(x+(c[i]-48))*10;
  386. }
  387.     x=x/10;
  388.     if(c[i]=='.')
  389.   for(j=n-1;j>i;j--)y=(y+(c[j]-48))*0.1;
  390. return x+y;
  391. }
  392. }
复制代码
发表于 2011-1-7 12:53:45 | 显示全部楼层
JAVA什么的……最讨厌了
上次JAVA考试老师竟然叫我们全体重考……也不想想我们成绩为什么全都这么差
回复

使用道具 举报

发表于 2011-1-7 13:24:02 | 显示全部楼层
又想起来我那python大作业还没做完...
准备做个类似计算器的东西
回复

使用道具 举报

发表于 2011-1-7 19:12:23 | 显示全部楼层
忽然想起自己十年前用C (cweb)写过一个计算器的,刚刚挖出来居然还能用...

http://paste.pocoo.org/show/316430/
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 少女注册中

本版积分规则

合作与事务联系|无图版|手机版|小黑屋|喵玉殿

GMT+8, 2025-10-31 07:16

Powered by Discuz! X3.5

© 2001-2025 Discuz! Team.

快速回复 返回顶部 返回列表