四则运算java版

来源:互联网 发布:ubuntu查看硬盘大小 编辑:程序博客网 时间:2024/05/16 06:26

本题目要求编写的是一个简单的计算器,该计算器目前只需要支持单位正整数的加、减、乘、除运算,并支持用括号表示优先级别。和我们小学时学过的算术规则一致,乘法和除法的优先级一样,加法和减法的优先级一样。乘除法的优先级高于加减法。括号的优先级最高。      同一优先级的运算顺序为自左向右。

public class Calculator {    //输入四则运算表达式字符串,返回整型结果    public int expr(String input)    {        PostfixExpression pe=new PostfixExpression(input);        return  calculate(pe.toString());    }    //输入后缀表达式,返回计算结果    public int calculate(String postfixExpression)    {        Stack<Integer> stack=new Stack<Integer>();        int tmp=0;        char[] array=postfixExpression.toCharArray();        for(int i=0;i<array.length;i++)        {            char c=array[i];            if(c>='0'&&c<='9')            {                stack.push((c-'0'));            }else            {                int op1=stack.pop();                int op2=stack.pop();                switch (c){                    case '+':                        tmp=op2+op1;break;                    case '-':                        tmp=op2-op1;break;                    case '*':                        tmp=op2*op1;break;                    case '/':                        tmp=op2/op1;break;                }                stack.push(tmp);            }//            System.out.println("char:"+c+"  stack:"+stack);        }        return tmp;    }    public static void main(String[] args)    {        PostfixExpression pe;        Calculator calculator=new Calculator();        Scanner scanner=new Scanner(System.in);        String line;        System.out.println("input expression:");        while (true)        {            line=scanner.nextLine();//            pe=new PostfixExpression(line);//            line=pe.toString();//            System.out.println("postfixExpression:"+line);//            System.out.println(calculator.calculate(line));            try {                System.out.println(calculator.expr(line));            }catch (ArithmeticException e)            {                System.out.println("expression error:divide zero");            }        }    }}

public class PostfixExpression {    StringBuffer postfixExpression=new StringBuffer();    Stack<Character> stack=new Stack<Character>();    //中缀表达式转化为后缀表达式    PostfixExpression(String nifixExpression)    {        char[] array=nifixExpression.toCharArray();        for(int i=0;i<array.length;i++)        {            char c=array[i];//            System.out.print("char:"+c+"  ");            if(c>='0'&&c<='9')            {                postfixExpression.append(c);            }else if(c=='(')            {                stack.push(c);            }else if(c=='+'||c=='-'||c=='*'||c=='/')            {                this.addOperator(c);            }else            {                while(!stack.isEmpty())                {                    char tmp=stack.peek();                    if(tmp=='(')                    {                        stack.pop();                        break;                    }else                    {                        postfixExpression.append(stack.pop());                    }                }            }//            System.out.println("stack:"+stack+"     string:"+postfixExpression);        }        while(!stack.isEmpty())        {            postfixExpression.append(stack.pop());        }//        System.out.println("string:"+postfixExpression);    }    public void addOperator(char c)    {        if(stack.isEmpty())        {}        else if(c=='*'||c=='/')        {            while(!stack.isEmpty())            {                char tmp=stack.peek();                if(tmp=='*'||tmp=='/')                {                    postfixExpression.append(stack.pop());                }else                    break;            }        }else        {            while(!stack.isEmpty())            {                char tmp=stack.peek();                if(tmp!='(')                {                    postfixExpression.append(stack.pop());                }                else                    break;            }        }        stack.push(c);    }    public String toString()    {        return this.postfixExpression.toString();    }    public static void main(String[] args)    {        Scanner scanner=new Scanner(System.in);        String line;        PostfixExpression pe;        while(true)        {            line=scanner.nextLine();            System.out.println(line);            pe=new PostfixExpression(line);            System.out.println(pe);        }    }}


0 0
原创粉丝点击