⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 calc.java

📁 这是我上学期收集的一些我们班高手的JAVA期末作业的源代码
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.math.*;

public class Calc extends Applet implements ActionListener,KeyListener
{
  private double result=0,data1=0,radixPointDepth=1,memData=0;
     //result变量保存计算的结果,data1保存当前正在输入的数字, radixPointDepth保存当前输入的数字是小数点后的第几位,他由radixPointIndicate控制是否起作用, memData内存暂存数据

  private boolean radixPointIndicate=false,equalSignPressIndicate=false;
     //radixPointIndicate记录小数点是否被按下,被按下后为true, equalSignPressIndicate记录上一次控制键是否为等号,为等号则为true
  private boolean numberPressIndicate=false,memDataIndicate=false;  
     //numberPressIndicate记录上一次按键是否为数字(包括小数点),是为true, memDataIndicate记录内存中是否有数据,有为true
  private String s;
  private char prec='+';   
  private TextField tf;    
  private Button b1,b2,b3,b4,b5,b6,b7,b8,b9,b10,b11,b12,b13,b14,b15,b16,b17,b18;
  private Button b19,b20,b21,b22,b23,b24,b25,b26,b27,b28,b29,b30,b31,b32,b33;
  private Panel p1,p2,p3,p4,p5,p6,p7;
  private Double displayNumber=new Double(data1);   
  private double PI = 3.14159265358979323846;
  public void init()
   {
     Color blue=Color.blue;
     Color red=Color.red;
     Color white=Color.white;       
     Color black=Color.black; 
     Color wr=new Color(210,30,225);                          
     Color snow1=new Color(192,220,206);
     p1=new Panel();  
     p2=new Panel();   p2.setFont(new Font("Terminal",Font.PLAIN,18));
     p3=new Panel();   p3.setFont(new Font("Terminal",Font.PLAIN,18));
     p4=new Panel();   p4.setFont(new Font("Terminal",Font.PLAIN,18));
     p5=new Panel();   p5.setFont(new Font("Terminal",Font.PLAIN,18));
     p6=new Panel();   p6.setFont(new Font("Terminal",Font.PLAIN,18));
     p7=new Panel();   p7.setFont(new Font("Terminal",Font.PLAIN,18));
     p1.setBackground(snow1);
     p2.setBackground(snow1);
     p3.setBackground(snow1);
     p4.setBackground(snow1);
     p5.setBackground(snow1);
     p6.setBackground(snow1);
     p7.setBackground(snow1);
     tf=new TextField("0",22);
     tf.setFont(new Font("System",Font.BOLD,18));
     tf.setEditable(false);    //将文本框设为不可编辑,只能显示计算数据
     tf.setBackground(white);
     b1=new Button("      ");       
     b2=new Button("Backspace");     b2.setForeground(red);
     b3=new Button(" CE ");       b3.setForeground(red);
     b4=new Button(" C ");        b4.setForeground(red);
     b5=new Button("MC");       b5.setForeground(red);
     b6=new Button("MR");       b6.setForeground(red);
     b7=new Button("MS");       b7.setForeground(red);
     b8=new Button("M+");       b8.setForeground(red);  
     b9=new Button(" 7 ");        b9.setForeground(blue);
     b10=new Button(" 8 ");       b10.setForeground(blue);	
     b11=new Button(" 9 ");       b11.setForeground(blue); 
     b12=new Button(" * ");       b12.setForeground(red);
     b13=new Button(" 4 ");       b13.setForeground(blue);   
     b14=new Button(" 5 ");       b14.setForeground(blue); 
     b15=new Button(" 6 ");       b15.setForeground(blue); 
     b16=new Button(" / ");       b16.setForeground(red);
     b17=new Button(" 1 ");       b17.setForeground(blue);
     b18=new Button(" 2 ");       b18.setForeground(blue);
     b19=new Button(" 3 ");       b19.setForeground(blue); 
     b20=new Button(" + ");       b20.setForeground(red);
     b21=new Button(" 0 ");       b21.setForeground(blue);
     b22=new Button(" . ");       b22.setForeground(blue);
     b23=new Button("+/-");     b23.setForeground(blue);
     b24=new Button(" - ");       b24.setForeground(red);      
     b25=new Button(" %  ");       b25.setForeground(red); 
     b26=new Button("1/x ");     b26.setForeground(red);
     b27=new Button("x^2 ");     b27.setForeground(red);
     b28=new Button("sqrt");    b28.setForeground(red);
     b29=new Button("sin");     b29.setForeground(wr);
     b30=new Button("cos");     b30.setForeground(wr);
     b31=new Button("tan");     b31.setForeground(wr);
     b32=new Button("    =    ");       b32.setForeground(red);
     b33=new Button("mod");     b33.setForeground(red);

     //下面为界面部分     
     setLayout(new GridLayout(7,5));
     p1.add(tf); 
     p2.add(b1);   p2.add(b2);   p2.add(b3);   p2.add(b4);           
     p3.add(b8);   p3.add(b9);   p3.add(b10);  p3.add(b11);  p3.add(b12);  p3.add(b27);
     p4.add(b7);   p4.add(b13);  p4.add(b14);  p4.add(b15);  p4.add(b16);  p4.add(b28);
     p5.add(b6);   p5.add(b17);  p5.add(b18);  p5.add(b19);  p5.add(b20);  p5.add(b25);
     p6.add(b5);   p6.add(b21);  p6.add(b22);  p6.add(b23);  p6.add(b24);  p6.add(b26); 
     p7.add(b29);  p7.add(b30);  p7.add(b31);  p7.add(b33);  p7.add(b32);

     add(p1);  add(p2);  add(p3);  add(p4);  
     add(p5);  add(p6);  add(p7);  
                     
     tf.addKeyListener(this);           
     b2.addActionListener(this);   b2.addKeyListener(this);     
     b3.addActionListener(this);
     b4.addActionListener(this);   b4.addKeyListener(this);	  
     b5.addActionListener(this);	 
     b6.addActionListener(this);	
     b7.addActionListener(this);
     b8.addActionListener(this);
     b9.addActionListener(this);   b9.addKeyListener(this);	  
     b10.addActionListener(this);  b10.addKeyListener(this);    
     b11.addActionListener(this);  b11.addKeyListener(this);	  
     b12.addActionListener(this);  b12.addKeyListener(this);	  
     b13.addActionListener(this);  b13.addKeyListener(this);	  
     b14.addActionListener(this);  b14.addKeyListener(this);	  
     b15.addActionListener(this);  b15.addKeyListener(this);	  
     b16.addActionListener(this);  b16.addKeyListener(this);	  
     b17.addActionListener(this);  b17.addKeyListener(this);    
     b18.addActionListener(this);  b18.addKeyListener(this);    
     b19.addActionListener(this);  b19.addKeyListener(this);    
     b20.addActionListener(this);  b20.addKeyListener(this);    
     b21.addActionListener(this);  b21.addKeyListener(this);    
     b22.addActionListener(this);  b22.addKeyListener(this);    
     b23.addActionListener(this);
     b24.addActionListener(this);  b24.addKeyListener(this);    
     b25.addActionListener(this); 
     b26.addActionListener(this);
     b27.addActionListener(this);
     b28.addActionListener(this);  b28.addKeyListener(this);    
     b29.addActionListener(this);
     b30.addActionListener(this);    
     b31.addActionListener(this);
     b32.addActionListener(this); 
     b33.addActionListener(this);    
    }

   //下面为键盘事件响应    
   public void keyPressed(KeyEvent e){}
   public void keyReleased(KeyEvent e)
    {                
      Character Char1=new Character(e.getKeyChar()); 
      s=Char1.toString();    
      if(e.getKeyCode()==10){ s="="; }   
      if(e.getKeyCode()==27){ s="C"; }                                            
      if(e.getKeyCode()==8){ s="Backspace"; }                        
      if(s.equals("C"))
       {   //以下六条语句为初始化计算器即清零
         result=0;
         data1=0;
         prec='+';
         radixPointDepth=1;
         numberPressIndicate=false;
         radixPointIndicate=false;
         displayNumber=new Double(data1);
         tf.setText(displayNumber.toString());
       }
      if(s.equals("Backspace")&&(data1!=0))
        {    //清除前一个错误输入数字
          if(radixPointIndicate){
               radixPointDepth=radixPointDepth*10;
               data1=((int)(data1/radixPointDepth))*radixPointDepth;
             }
          else    data1=(int)(data1/10); 
          displayNumber=new Double(data1);
          tf.setText(displayNumber.toString()); 
        }
      switch(s.charAt(0))
       {   //以下是数字输入响应
        case '0': case '1': case '2': case '3': case '4': 
        case '5': case '6': case '7': case '8': case '9':
             //按完等号键后,直接按任何数字键,计算器认为应该清零后从新计算
           if(equalSignPressIndicate)
             {
               result=0;
               data1=0;
               prec='+';
               radixPointDepth=1;
	       numberPressIndicate=false;	
               radixPointIndicate=false;
             }
           if(radixPointIndicate)
             {
               radixPointDepth=radixPointDepth/10;	
               data1=data1+(Integer.parseInt(s))*radixPointDepth;
             }
	   else  data1=data1*10+(Integer.parseInt(s));
           numberPressIndicate=true;
           equalSignPressIndicate=false;
           displayNumber=new Double(data1);
           tf.setText(displayNumber.toString());
           break;
          //在按+ - * / =号后的运算
	 case '+': case '-':case '*':case '/':case '=':
	     if(s.charAt(0)!='='&&equalSignPressIndicate){
                  prec=s.charAt(0);
                  equalSignPressIndicate=false;			           
               }
	     else{	
                  switch(prec){
                                case '+':        
                                         result=result+data1;
                                         break;	
		      		case '-':
			      		 result=result-data1;		
      		      		         break;		
		   	        case '*':		
		   			 result=result*data1;	
				   	 break;			
				case '/':			
					 result=result/data1;			
				         break;	
                                }	
		   }
             radixPointIndicate=false;		
	     radixPointDepth=1;
             numberPressIndicate=false;
	     if(s.charAt(0)!='='){    
                                   data1=0;
	      	                   prec=s.charAt(0);		
			         }  	
             else   equalSignPressIndicate=true;
             displayNumber=new Double(result);	    
	     tf.setText(displayNumber.toString());
             break;
        case '.':
                  radixPointIndicate=true;
                  numberPressIndicate=true;
                  break;
        case '%':
                  if(equalSignPressIndicate||(numberPressIndicate==false))
                    {
               	     result=result*100;
                     displayNumber=new Double(result);
                     tf.setText(displayNumber.toString());
                    }
                  else{
                       data1=data1/100;
                       displayNumber=new Double(data1);
	               tf.setText(displayNumber.toString());
                       } 
                  break;
       }
    }
   public void keyTyped(KeyEvent e){}

   public void actionPerformed(ActionEvent e)
    {
       s=e.getActionCommand();
       s=s.trim();        
       if(s.equals("C")){    //以下六条语句为初始化计算器即清零
                             result=0;	
                             data1=0;	
 	                     prec='+';	
                             radixPointDepth=1;	
                             numberPressIndicate=false;		
                             radixPointIndicate=false;	
  		             //显示数据	
                             displayNumber=new Double(data1);	
                             tf.setText(displayNumber.toString());  
                         }  
        if(s.equals("CE")){  //清除前一个输入的数	
 	                     data1=0;
                             radixPointDepth=1;	
	                     numberPressIndicate=false;	
                             radixPointIndicate=false;
  	                     displayNumber=new Double(data1);
                             tf.setText(displayNumber.toString()); 
                          }                                                       
         if(s.equals("Backspace")&&(data1!=0))
           {    
              //清除前一个错误输入数字
            if(radixPointIndicate){
               radixPointDepth=radixPointDepth*10;
               data1=((int)(data1/radixPointDepth))*radixPointDepth;
               }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -