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

📄 evaluateexpression.java

📁 Java编写的仿windows计算器
💻 JAVA
字号:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class EvaluateExpression 
{
	private ExpressStack operator;  //运算符栈
	private ExpressStack number;    //运算数栈
	private final String optr[] = { "+","-","*","/","Mod","And","Or","Xor","Lsh","Exp","x^y","(",")","#" };
	
	/*****************************构造函数*************************************/
	public EvaluateExpression()
	{
		operator = new ExpressStack( 0 );
		number = new ExpressStack( 0 );
		operator.push( 13 );
	}//end EvaluateExpression
	
	/*****************************获取入栈的字符串*****************************/
	public void getString( String in )
	{
		double get = isOptr( in );
		if( get > 13 )
		  number.push( Double.parseDouble( in ) );
		else
          putInOpterator( (int)get );
	}// end getString
	
	/*****************************运算符号入栈*********************************/
	public void putInOpterator(int optrx )
	{
		switch( compare( (int)operator.getTop(), optrx ) )
		{
			case -1:
			   operator.push( optrx );
			   break;			  
			case 0:
			   if(optrx==13)break;
			   operator.pop(); 
			   break;
			case  1:
				double b = number.pop();
				double a = number.pop();
				double opt = operator.pop();
				number.push( evaluate( a, b, opt ) );
				putInOpterator( optrx );
				break;
		}//end switch
	}//end method putInOpterator
	
	/*****************************获取最近入栈的数字***************************/
	public double getTopNumber()
	{
		if( number.isEmpty() ) return 0;
		else return number.getTop();
	}//end method getTopNumber
	
	/*****************************获取栈顶运算符*******************************/
	public String getTopOptr()
	{
		return optr[ (int)operator.getTop() ];
	}//end method getTopOptr
	
	/*****************************清空栈中元素*********************************/
	public void clearStack()
	{
		while(!number.isEmpty())number.pop();
		while(!operator.isEmpty())operator.pop();
	}//end method clearStack
	
	/*****************************将字符串数码化*******************************/
	/*"+","-","*","/","Mod","And","Or","Xor","Lsh","Exp","x^y","(",")","#" */
	/* 0   1   2   3   4     5     6    7     8     9     10    11  12  13 */
	private int isOptr( String in )
	{
		int i;
		for( i=0; i<optr.length; i++ )
		  if( optr[i].equals( in ) )break;
		return i;	
	}//end method isOptr
	
	/*****************************进行二元运算*********************************/
	private double evaluate( double a, double b, double opt )
	{
		double result=0;
		
		if( (int)opt ==0 )
		    	result = a+b;
		else if( (int)opt ==1 )
			    result = a-b;
		else if( (int)opt ==2 )
		    	result = a*b;
		else if( (int)opt ==3 )
		    	result = a/b;
		else if( (int)opt ==4 )
		    	result = a%b;
		else if( (int)opt ==5 )
		    	result = (int)a&(int)b;
		else if( (int)opt ==6 )
		    	result = (int)a|(int)b;
		else if( (int)opt ==7 )
		    	result = (~(int)a)&(int)b;
		else if( (int)opt ==8 )
		    	result = Integer.rotateLeft( (int)a,(int)b);
		else if( (int)opt ==9 )
		    	result = a*Math.pow(10,b);
		else if( (int)opt ==10 )
		    	result = Math.pow( a,b );
		    	
		return result;
	}//end method evaluate
	
	/*****************************比较运算优先级*******************************/
	private int compare( int m, int n )
	{
		int a[][] = new int[14][14];
		
		int i,j;
		for( i=0; i<14; i++ )
		  for( j=0; j<14; j++ )
		    a[i][j] = 1;
		    
		a[11][12] = a[13][13] = 0;
		
		for( i=0; i<2; i++ )
		 for( j=2; j<12; j++)
		   a[i][j] = -1;
		   
		for( i=11; i<14; i+=2 )
		 for( j=0; j<12; j++)
		   a[i][j] = -1;
		   
		for( i=2; i<9; i++ )
		 for( j=9; j<12; j++)
		   a[i][j] = -1;
		   
		a[9][11] = a[10][11] = -1;
		return a[m][n];
	}//end method compare
}//end class EvaluateExpression

/******************************************************************************
 *                               运算符优先表
===============================================================================
         0    1    2   3    4      5    6     7    8     9     10    11  12  13
         +    -    *   /   Mod    And   Or   Xor   Lsh   Exp   x^y   (    )   # 
  
0   +    1    1    -   -    -      -    -     -     -     -     -    -    1   1 
   
1   -    1    1    -   -    -      -    -     -     -     -     -    -    1   1 
   
2   *    1    1    1   1    1      1    1     1     1     -     -    -    1   1
   
3   /    1    1    1   1    1      1    1     1     1     -     -    -    1   1
   
4  Mod   1    1    1   1    1      1    1     1     1     -     -    -    1   1
  
5  And   1    1    1   1    1      1    1     1     1     -     -    -    1   1 

6   Or   1    1    1   1    1      1    1     1     1     -     -    -    1   1

7  Xor   1    1    1   1    1      1    1     1     1     -     -    -    1   1
  
8  Lsh   1    1    1   1    1      1    1     1     1     -     -    -    1   1
  
9  Exp   1    1    1   1    1      1    1     1     1     1     1    -    1   1
  
10 x^y   1    1    1   1    1      1    1     1     1     1     1    -    1   1
   
11  (    -    -    -   -    -      -    -     -     -     -     -    -    =   n
   
12  )    1    1    1   1    1      1    1     1     1     1     1    n    1   1
 
13  #    -    -    -   -    -      -    -     -     -     -     -    -    n   =
===============================================================================*/

⌨️ 快捷键说明

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