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

📄 createbutton.java

📁 Java编写的仿windows计算器
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
	    	{
	    		if(optr.equals("("))
	    		   layer(1);	
	    		else
	    		{
	    			forword = 1;
	    			layer(-1);
	    			if(carry==10)
	    		       expression.getString( display );
	    	        else 
	    		       expression.getString( mToN( carry, 10, display ) );
	    	    }
	    	    expression.getString(optr);
	    	    if(carry==10)
	    		   display = ""+expression.getTopNumber();
	    		else
	    		   display = mToN( 10, carry, ""+(int)(expression.getTopNumber()) );
	    	    if(optr.equals("(")) next = 2;
	    	    else next = 0;
	    	}//end else if
	    	else
	    	{
	    		if(forword==2)return;
                if( !(optr.equals("=") && forword!=0 ) )
                {
                   if(carry==10)
	    		     expression.getString( display );
	    	       else 
	    		     expression.getString( mToN( carry, 10, display ) );
                }
	    		   
	    		if(optr.equals("="))
	    		  expression.getString( "#" );
	    		else
	    		  expression.getString( optr );
                
	    		if(carry==10)
	    		   display = ""+expression.getTopNumber();
	    		else
	    		   display = mToN( 10, carry, ""+(int)(expression.getTopNumber()) );
	    		next = -1;
	    		forword = 2;
	    	}//end else	    	
	 	}//end method runSingalOptr of class allOptr
	 }//end inner class allOptr
	 
	 /*************************************************************************/
	 /*********************数字输入监听器**************************************/
	 /*************************************************************************/
	 private class Input implements ActionListener
	 {
	 	public void actionPerformed( ActionEvent E )
	 	{
	 		if(next ==0)return;
	 		String x = E.getActionCommand();
	 		if( x.equals("+/-") )
	 		   changeSign();
	 		else if( inputLegal( x ) )
	 		   runInput( x );
	 		displayField.setText(display);
	 		forword = 0;
	 	}//end method actionPerformed of class Input
	 	
	 	public void runInput( String key )
	 	{
	 		if(forword==2)
	 		{
	 			display = "0";
	 			forword = 0;
	 		}
	 		if( display.equals("0") && !key.equals(".") )
	 		   display = key;
	 		else 
	 		   display += key; 
	 		next = 1 ;
	 	}//end method runInput of class Input	
	 }//end inner class Input
	 
	 /*************************************************************************/
	 /*********************以下为按钮功能(仅单目运算健和功能键)****************/
	 /*************************************************************************/
	 
	 /*******************************+/-号变换*********************************/
     private void changeSign()    
     {
     	if(!display.equals("0"))
     	{
     		if( Double.parseDouble( display )<0 )
     		    display = display.substring( 1 );
     		else
     		    display = "-"+display;
        }
     }// end method changeSign
     
     /***********************************退格 BackSpace************************/
     private void backSpace() 
     {
     	char remove = display.charAt( display.length()-1 );
     	
     	if( display.length()>1 )
     	  display = display.substring( 0, display.length()-1 );
     	else
     	  display = "0";
     	  
     	if(remove=='.')point=0;
     }// end method backSpace
     
     /**********************************清空所有 C*****************************/
     private void clearAll() 
     {
     	initVariable();
     	displayField.setText( "0." );
     	layerField.setText( "" );
     }// end method clearAll
     
     /***********************************清空当前 CE***************************/
     private void clearCurrent()
     {
     	display = "0";
     	displayField.setText( "0." );
     	layerField.setText( "" );
     }// end method  clearCurrent
     
     /***********************************按格式显示****************************/
     private void showResult( )
     {
     	String toShow;
     	if( carry==10 && fe==1 )
     	{
     		double x = Double.parseDouble( display ),fag =0;
     		while( x/Math.pow(10,fag)>=10 )fag++;
     		while( x/Math.pow(10,fag)<=1 )fag--;
     		  x = x/Math.pow(10,fag);
     		  
     		if(fag<0) toShow = ""+x+"e"+fag;
     		else toShow = ""+x+"e+"+fag;
     	}
     	else
     	  toShow = display;
     	displayField.setText( toShow );
     }// end method showResult
     
     /*************************F-E状态*科学技术法格式状态**********************/
     private void fe()        
     {
     	if(fe==1)fe=0;
     	else fe=1;
     }// end method  fe
     
     /***************************括号层数标志**********************************/
     private void layer( int n )
     {
     	layer+=n;
     	if(layer!=0)layerField.setText( "(="+layer );
     	else layerField.setText( "" );
     }// end method layer
     
     /*****************************m进制到n进制的转换**************************/
     private String mToN( int m, int n, String str )
     {
     	int number;
     	if(m==n)return str;
     	String list = "0123456789ABCDEF";
     	if( m==10 )
     	    number = (int)Double.parseDouble( str );
        else
        {
     		number = list.indexOf( str.charAt(0) );
     		for( int i=1; i<str.length(); i++ )
     		   number = number*m + list.indexOf( str.charAt(i) );
     	}
     	
     	if(n==2)return Integer.toBinaryString( number );
     	else if(n==8)return Integer.toOctalString( number );	
     	else if(n==16)return  Integer.toHexString( number ).toUpperCase();
     	else return ""+number;
     }// end method mToN
     
     /****************************sin,sin函数*********************************/     
     private void sin()
     {
     	double data = Double.parseDouble( display );
     	
     		if( checkBox.isSelected() )
     	{
     		if(radioButton[4].isSelected())
     		  display = ""+Math.rint(Math.asin( data /180*Math.PI)*100000000)/100000000.0;
     	    else
     	      display = ""+Math.rint(Math.asin( data )*100000000)/100000000.0;
        }
        else
        {
        	if(radioButton[4].isSelected())
     		  display = ""+Math.rint(Math.sin( data/180*Math.PI)*100000000)/100000000.0;
     	    else
     	      display = ""+Math.rint(Math.sin( data )*100000000)/100000000.0;
        }
     }// end method sin
     
     /*******************************acos,cos函数*****************************/
     private void cos()
     {
     	double data = Double.parseDouble( display );
     	
     	if( checkBox.isSelected() )
     	{
     		if(radioButton[4].isSelected())
     		  display = ""+Math.rint(Math.acos( data /180*Math.PI)*100000000)/100000000.0;
     	    else
     	      display = ""+Math.rint(Math.acos( data )*100000000)/100000000.0;
        }
        else
        {
        	if(radioButton[4].isSelected())
     		  display = ""+Math.rint(Math.cos( data/180*Math.PI)*100000000)/100000000.0;
     	    else
     	      display = ""+Math.rint(Math.cos( data )*100000000)/100000000.0;
        }
     }// end method cos
     
     /******************************atan,tan函数******************************/
     private void tan()
     {
     	double data = Double.parseDouble( display );
     	
     		if( checkBox.isSelected() )
     	{
     		if(radioButton[4].isSelected())
     		  display = ""+Math.rint(Math.atan( data /180*Math.PI)*100000000)/100000000.0;
     	    else
     	      display = ""+Math.rint(Math.atan( data )*100000000)/100000000.0;
        }
        else
        {
        	if(radioButton[4].isSelected())
     		  display = ""+Math.rint(Math.tan( data/180*Math.PI)*100000000)/100000000.0;
     	    else
     	      display = ""+Math.rint(Math.tan( data )*100000000)/100000000.0;
        }
     }// end method tan
     
     /******************************ln 及ln的反函数****************************/
     private void ln()
     {
     	double data = Double.parseDouble( display );
     	if( !checkBox.isSelected() )
     	   display = ""+Math.log( data );
     	else 
     	   display = ""+Math.exp( data );
     }// end method ln
     
     /*******************************log及log的反函数**************************/
     private void log()
     {
     	double data = Double.parseDouble( display );
     	if( !checkBox.isSelected() )
     	   display = ""+Math.log10( data );
     	else 
     	   display = ""+Math.pow( 10, data );
     }// end method log
     
     /********************************求n次方**********************************/
     private void xpow( double x )
     {
     	if( checkBox.isSelected() )x = -x;
     	display = ""+Math.pow( Double.parseDouble( display ), x );
     }// end method xpow
     
     /*********************************开方************************************/
     private void sqrt(  )
     {
     	display = ""+Math.pow( Double.parseDouble( display ), 0.5 );
     }// end method sqrt
     
     /*******************************求百分数**********************************/
     private void percent(  )
     {
     	display = ""+Math.pow( expression.getTopNumber(), 2 )/100;
     	forword = 0;
     	next =1;
     }// end method percent
     
     /*******************************阶乘**************************************/
     public int nFactorial( int n )
     {
     	if( n<0 ) return 0;
     	else if( n==0 )return 1;
     	else return n*nFactorial( n-1 );
     }// end method nFactorial
     
     /****************************将数转换为度分秒*****************************/
     private void dms( )
     {
     	double n = Double.parseDouble( display );
     	double neg;
     	if( n>0 )neg = 1;
     	else neg =0;
     	n = Math.abs( n );
     	double d = Math.floor( n );
     	double m = Math.floor( 60*( n-d ) );
     	double s = ( n-d )*60-m;
     	double dms = d+m/100+s*0.006;
     	
     	if( Double.parseDouble( display ) <0 )dms = -dms;
     	displayField.setText( ""+dms );
     }// end method dms
     
     /*************************MR/MC/MS/M+按钮*********************************/
     private void mr()//访存
     {
     	display = mToN( 10, carry, memory );
     }// end method mr

     private void mc()//清存
     {
     	memory = "0";
     	memField.setText("");
     }// end method mc
     
     private void ms()//储存
     {
     	memory = display;
     	if(Double.parseDouble( memory )!=0)memField.setText("M");
     }// end method ms
     
     private void mp()//所存的数与当前的数相加
     {
     	double x = Double.parseDouble( mToN( carry, 10, display ) )+Double.parseDouble( memory );
     	memory = ""+Double.toString( x );
     }// end method mp
     
     /***************************取整 Int**************************************/
     private void intX()
     {
     	display = ""+(int)Double.parseDouble( display );
     }// end method intX
     
     /***************************非运算 Not************************************/
     private void notX()
     {
     	long not;
     	long x[] = { 65535, 255 };
     	int i=0;
     	if( radioButton[6].isSelected() ) i=0;
     	else if ( radioButton[7].isSelected() ) i=1;
     	
     	if(carry==10)
     	{
     	  not =  -(int)Double.parseDouble( display )-1;
     	  display = ""+not;
        }
     	else
     	{
     		not = x[i] - (int)Double.parseDouble( mToN( carry,10,display ) );
     		display = mToN( 10,carry,""+not ) ;
     	}
     }// end method notX
     
     /**************************返回当前显示的字符串***************************/
     public String currentDisplay()
     {
     	return displayField.getText();
     }// end method currentDisplay
     
     /*****************************检查输入合法性******************************/
     private boolean inputLegal( String optr )
     {
     	if( optr.equals(".") )
     	  if( point==1 || carry!=10 )
     	    return false;
     	  else 
     	    point = 1;
     	else if( optr.equals("sqrt") );
     	else if( optr.equals("%") );
     	else if( optr.equals("F-E") );
	    else if( optr.equals("dms") );
	 	else if( optr.equals("sin") ); 
     	else if( optr.equals("cos") );
	    else if( optr.equals("tan") );
	    else if( optr.equals("x^3") ); 
	    else if( optr.equals("x^2") );
	    else if( optr.equals("ln") ); 
	    else if( optr.equals("log") ); 
	    else if( optr.equals("n!") ); 
	    else if( optr.equals("1/x") ); 
	    else if( optr.equals("Int") );
	    else if( optr.equals("MC") );
	    else if( optr.equals("MR") ); 
	    else if( optr.equals("MS") );
	    else if( optr.equals("M+") );  
	    else if( optr.equals("pi") ); 
	    else if( optr.equals("Not") ); 
	    else if( optr.equals("C") );
	    else if( optr.equals("CE") );
	    else if( optr.equals("BackSpace") );
        else if( optr.equals(")") )
        {
        	if( layer==0 || next==-1 )return false;
        }
        else if( optr.equals("(") )
            if( next==1 )return false;
        else
        {
        	if(next ==2)return false;
        }
     	return true;
     }// end method inputLegal
}//end class CreateButton

⌨️ 快捷键说明

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