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

📄 newclass.java

📁 将前缀表达式转为中缀表达式
💻 JAVA
字号:
/* * To change this template, choose Tools | Templates * and open the template in the editor. */package pro;/** * * @author Administrator */public class NewClass {    static char[] a;    static double[] c;    static int i,j;    static char[] b;    static String[] zh;    static J_Tree root;    public static J_Tree JReadExpre(String o){        int n,i1;        J_Tree p=null,q=null;        char[] a1;        n=o.length();        a1=new char[n];        a1[0]=o.charAt(0);        p=new J_Tree(a1[0]);        q=p;root=p;        for(i1=1;i1<n;i1++){            a1[i1]=o.charAt(i1);            if(a1[i1]=='+'||a1[i1]=='-'||a1[i1]=='*'||a1[i1]=='/'||a1[i1]=='^'){                q=new J_Tree(a1[i1]);                                if(p.left==null){                    p.left=q;                    q.parent=p;                }                else if(p.right==null){                    p.right=q;                    q.parent=p;                }                else{                    while(p.right!=null)p=p.parent;                    p.right=q;                    q.parent=p;                }                p=q;                            }            else{                q=new J_Tree(a1[i1]);                if(q.element-'0'>=0&&q.element-'9'<=9)                q.N=q.element-'0';                if(p.left==null){                    p.left=q;                }                else if(p.right==null){                    p.right=q;                }                else{                    while(p.right!=null)p=p.parent;                    p.right=q;                }                                }        }        return root;    }    public static void run(J_Tree o){        J_Tree p;        int k=0;        p=o;        if(p==null) return;        if(p.left!=null){            if(p.left.element=='+'||p.left.element=='-'||p.left.element=='*'||                    p.left.element=='/'||p.left.element=='^')                if(((p.left.element=='+'||p.left.element=='-')&&(p.element=='*'||p.element=='/'||p.element=='^'))                ||((p.left.element=='*'||p.left.element=='/')&&p.element=='^')) {                    a[i]='(';                    i++;                    run(p.left);                    a[i]=')';                    i++;                }                else{run(p.left);}            else{                run(p.left);            }                   }        if(p.element!='@'){            a[i]=p.element;            i++;        }        else{            a[i]='@';            c[i]=p.N;            i++;        }            if((p.element-'0'>=0&&p.element-'0'<=9)||p.element=='+'||p.element=='-'||                    p.element=='*'||p.element=='/'||p.element=='^'){}            else{                for(int i2=0;i2<j;i2++){                    if(b[i2]==p.element){                        k=1;                        break;                    }                }                if(k==0){                    b[j]=p.element;                    j++;                }                            }        if(p.right!=null){            if(p.right.element=='+'||p.right.element=='-'||p.right.element=='*'||                    p.right.element=='/'||p.right.element=='^')                if(((p.right.element=='+'||p.right.element=='-')&&(p.element=='*'||p.element=='/'||p.element=='^'||p.element=='+'||p.element=='-'))                ||((p.right.element=='*'||p.right.element=='/')&&(p.element=='^'||p.element=='*'||p.element=='/'))                ||(p.right.element=='^'&&p.element=='^')) {                    a[i]='(';                    i++;                    run(p.right);                    a[i]=')';                    i++;                }else{run(p.right);}            else{                run(p.right);            }                   }    }       public static String WriteExpr(J_Tree o){        String s;        a=new char[100];        b=new char[100];        c=new double[100];        zh=new String[100];        for(int i=0;i<100;i++) zh[i]="未赋值";        i=0;j=0;        run(o);        s=new String("");        for(int k=0;k<i;k++){            if(a[k]=='@') s=s.concat(""+c[k]);            else s=s.concat(new String(a,k,1));        }       // s=new String(a,0,i);            return s;    }     public static double Value(J_Tree o){        if(o.element=='+') return(Value(o.left)+Value(o.right));        else if(o.element=='-') return(Value(o.left)-Value(o.right));        else if(o.element=='*') return(Value(o.left)*Value(o.right));        else if(o.element=='/') return(Value(o.left)/Value(o.right));        else if(o.element=='^') return(java.lang.Math.pow(Value(o.left),Value(o.right)));        else if(o.element-'0'>=0&&o.element-'0'<=9) return(o.element-'0');        else{            for(int i=0;i<j;i++)                if(o.element==b[i]) {                    return(java.lang.Double.parseDouble(zh[i]));                }             }        return 0;    }    public static J_Tree hebing(J_Tree o1,J_Tree o2,int o){                if(o==0) root=new J_Tree('+');        else if(o==1) root=new J_Tree('-');        else if(o==2) root=new J_Tree('*');        else if(o==3) root=new J_Tree('/');        else  root=new J_Tree('^');        root.left=o1;        root.right=o2;        return root;    }    public static J_Tree bing (J_Tree q){        char em;        J_Tree p1=null,p2=null;        if(q.left!=null) p1=bing(q.left);        if(q.right!=null) p2=bing(q.right);                if(q.left!=null&&q.right!=null&&p1.N<10000&&p2.N<10000){            em=q.element;            if(em=='+'){                q.element='@';                q.N=p1.N+p2.N;                q.left=null;                q.right=null;                 }            else if(em=='-'){                q.element='@';                q.N=p1.N-p2.N;                q.left=null;                q.right=null;                 }            else if(em=='*'){                q.element='@';                q.N=p1.N*p2.N;                q.left=null;                q.right=null;                 }            else if(em=='/'){                q.element='@';                q.N=p1.N/p2.N;                q.left=null;                q.right=null;                 }            else if(em=='^'){                q.element='@';                q.N=java.lang.Math.pow(p1.N,p2.N);                q.left=null;                q.right=null;                 }                    }        return q;            }        public static String chang(J_Tree o){        J_Tree p=o;        bing(p);        return WriteExpr(p);    }    }

⌨️ 快捷键说明

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