Java表达式求值

来源:互联网 发布:视频音乐合成软件 编辑:程序博客网 时间:2024/05/10 12:58

1、先读取表达式,用字符串存,比如字符串“1+2+3”
2、将字符串中的数字和运算符识别出来,按依次存在linkedlist里面 ,就变成 了 1+2+3 (数字为integ类型,字符串为#character类型),这就是中序表达式
3、将中序表达式 转换成 后序表达式 存在另一个linkedlisi里面
4、计算后序表达式的值
这里写图片描述

本程序主要是理清表达式求值的方法,本程序 还存在一些小问题:不能识别 小数,负数,感兴趣的可以自行修改程序

public class biaodashiqiuzhi_01 {       List zhongxu=new LinkedList();//存储中序表达式       List houxu=new LinkedList();//存储后续表达式     /*      *  函数名:priority      *  判断操作符优先级       *  大于返回1 等于返回0 小于返回 -1      */    public int priority(char a,char b)//判断优先级    {        if (a=='+' || a=='-')            switch (b)             {                case '+':                case '-':                    return 0;                default:                    return -1;            }        else             switch (b)             {                case '+':                case '-':                    return 1;                case '*':                case '/':                    return 0;                default:                    return -1;            }    }     public void divide(String shizi)//识别字符串中的数字和字符    {        int flag=0; double a=0;double j=0;        for(int i=0;i<shizi.length();i++)        {            char c=shizi.charAt(i);            //如果是操作符            if (c=='+' || c=='-' || c=='*' || c=='/' || c=='(' || c==')'  )            {                if (flag==1) //为了处理连续两个操作符的情形,数字重复进入链表                    zhongxu.add(a);                zhongxu.add(c);                flag=0;                a=0;            }            else //如果是数字            {                if(flag==0)//第一次读到数字进行标记                    flag=1;                a=a*10+c-48;                    //数字进入链表依赖于身后的符号,但是数字后面没有符号的时候,是一种特殊情况                if (i==shizi.length()-1)                            zhongxu.add(a);                     }        }    }    public void MidTOBack()//中序表达式转换成后续表达式    {        Mystack temp=new Mystack();        for (Object object : zhongxu)         {            if (object instanceof Character)            {                if (object.equals('(')) //左括号 直接进入后续表达式                    temp.push(object);                else                    //右括号,则吧遇到左括号之前的操作符全部放到后续表达式中                    if (object.equals(')') )                    {                        while(!temp.getTop().equals('('))                            houxu.add(temp.pop());                        temp.pop();                    }                    else                         if (temp.isEmpty())                             temp.push(object);                        else                            if (  priority((char)object , (char)temp.getTop() ) >0)                                temp.push(object);                            else                                if (temp.getTop().equals('('))                                    temp.push(object);                                else                                {                                    while(!temp.isEmpty()                                         && !temp.getTop().equals('(') &&                                        priority((char)object , (char)temp.getTop() ) <1)                                    houxu.add(temp.pop());                                    temp.push(object);                                }            }            else//数字 直接进入后续表达式                {houxu.add(object);}        }        while(!temp.isEmpty())            houxu.add(temp.pop());    }    public void calulate()//计算后续表达式    {        for (int i = 0; houxu.size()!=1; i++)         {            if (houxu.get(i) instanceof Character)             {                if(houxu.get(i).equals('+'))                    houxu.set(i-2,  (Double)houxu.get(i-2)+(Double)houxu.get(i-1) );                if(houxu.get(i).equals('-'))                    houxu.set(i-2,  (Double)houxu.get(i-2)-(Double)houxu.get(i-1) );                if(houxu.get(i).equals('*'))                    houxu.set(i-2,  (Double)houxu.get(i-2)*(Double)houxu.get(i-1) );                if(houxu.get(i).equals('/'))                    houxu.set(i-2,  (Double)houxu.get(i-2)/(Double)houxu.get(i-1) );                houxu.remove(i-1);                houxu.remove(i-1);                i-=2;            }        }        System.out.println(houxu.get(0));    }    public static void main(String[] args)    {        biaodashiqiuzhi_01 test=new biaodashiqiuzhi_01();        Scanner scanner=new Scanner(System.in);        while(true)        {            String str=scanner.nextLine();            test.divide(str);            test.MidTOBack();            test.calulate();        }       }}
0 0
原创粉丝点击