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

📄 expression.java

📁 如下功能: 1、二进制、八进制、十进制及十六进制数的加、减、乘、除、乘方、取模等简单计算 2、科学计算函数
💻 JAVA
字号:
/*
 * Expression.java
 *
 * Created on 2007年11月19日, 下午10:21
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package com.jakie.caculator.expression;
import java.util.*;

/**
 *
 * @author jiebo
 */
public class Expression {
    
    /** Creates a new instance of Expression */
    public Expression() {
    }
    
    //进制转换代码段
    public float tenN(String c,int m,int n){     //m底数,n幂
        float result;
        float ic=(float)Integer.parseInt(c);
        if(n<=0)
            result=ic;
        if(n==1)
            result=ic*m;
        else{
            result=1;
            for(int i=0;i<n;i++)
                result*=m;
            result*=ic;
        }
      //  System.out.println(result);
        return result;
    }
    public String chToD(String str,int kind){
        float result=0.0f;
        if(kind==16){
            String str2=null;
            for(int i=0;i<str.length();i++){
                if(str.charAt(i)=='A'||str.charAt(i)=='a')
                    str2="10";
                else if(str.charAt(i)=='B'||str.charAt(i)=='b')
                    str2="11";
                else if(str.charAt(i)=='C'||str.charAt(i)=='c')
                    str2="12";
                else if(str.charAt(i)=='D'||str.charAt(i)=='d')
                    str2="13";
                else if(str.charAt(i)=='E'||str.charAt(i)=='e')
                    str2="14";
                else if(str.charAt(i)=='F'||str.charAt(i)=='f')
                    str2="15";
                else
                    str2=str.charAt(i)+"";
                result+=tenN(str2,kind,str.length()-i-1);
            }
        }else if(kind==10){
            return str;
        }else{
            for(int i=0;i<str.length();i++){
                result+=tenN(str.charAt(i)+"",kind,str.length()-i-1);
            }
        }
        return result+"";
    }
    //进制转换结束
    public int whatRank(String str){       //判断符号的运算级
        if(str.equals("+")||str.equals("-"))
            return 1;
        if(str.equals("*")||str.equals("/"))
            return 2;
        return 3;
    }
    
    public boolean isNumber(String str){   //判断是否是数字
        boolean flag=true;
        if(str.equals("+")||str.equals("-")||str.equals("*")
        ||str.equals("/")||str.equals("(")||str.equals(")"))
            flag=false;
        return flag;
    }
    
    public LinkedList<String> changeToPostExpr(String expr,int n){   //中缀表达式转换成后缀表达式 存在集合LinkedList中
        final String pattern ="(?=[-+*/()]|(?<=[-+*/()]))";
        expr=expr.trim();
        LinkedList<String> list=new LinkedList<String>(Arrays.asList(expr.split(pattern)));
        if(list.getFirst().equals(""))
            list.removeFirst();
        LinkedList<String> result=new LinkedList<String>();     //存放结果LinkedList
        LinkedList<String> tempList=new LinkedList<String>();   //存放临时字符LinkedList
        String temp,flag;
        while(!list.isEmpty()){
            temp=list.removeFirst();
            if(isNumber(temp)){
                temp=chToD(temp,n);
                result.add(temp);   //
            }else{
                if(tempList.isEmpty())
                    tempList.add(temp);     //
                else{
                    flag=tempList.getLast();      
                    if(whatRank(temp)>whatRank(flag)&&(!temp.equals(")")))
                        tempList.addLast(temp);
                    else{
                        if(temp.equals(")"))
                        {
                            flag=tempList.removeLast();
                            while(!flag.equals("(")&&!tempList.isEmpty())
                            {    
                                result.addLast(flag);
                                flag=tempList.removeLast();
                            }
                        }else{
                            if((flag=tempList.getLast()).equals("("))
                                tempList.add(temp);
                            else{
                                flag=tempList.removeLast();
                                result.addLast(flag);
                                tempList.addLast(temp);
                            }
                        }
                    }
                }
                
            }
        }
        while(!tempList.isEmpty()){
            result.add(tempList.removeLast());
        }
        return result;
    }
    
    public float caculat(String str1,String str2,String oper){
        float result;
        float f1=0.0f,f2=0.0f;
        try{
                f1=Float.parseFloat(str1);
                f2=Float.parseFloat(str2);
            }catch(Exception e){
                e.printStackTrace();
            }
        if(oper.equals("+"))
        {
            return f1+f2;
        }
        if(oper.equals("-"))
        {
            return f1-f2;
        }
        if(oper.equals("*"))
        {
            return f1*f2;
        }
        if(oper.equals("/"))
        {
            return f1/f2;
        }
        return 0.0f;
    }
    
    public String getResult(String expr,int n){
        LinkedList<String> list=changeToPostExpr(expr,n);
        LinkedList<String> tempList=new LinkedList<String>();
        float result=0.0f;
        String str1,str2,oper,temp;
        while(!list.isEmpty()){
            temp=list.removeFirst();
            if(isNumber(temp))
                tempList.add(temp);
            else{
                str1=tempList.removeLast();
                if(tempList.isEmpty())
                    return "表达式错误";
                str2=tempList.removeLast();
                result=caculat(str2,str1,temp);
                tempList.addLast(String.valueOf(result));
            }
        }
        if(tempList.isEmpty())
            return "表达式错误";
        return tempList.getFirst();
    }
}

⌨️ 快捷键说明

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