当前位置: 代码迷 >> Java相关 >> 高手请指点,一个计算器程序
  详细解决方案

高手请指点,一个计算器程序

热度:285   发布时间:2008-06-04 19:54:56.0
高手请指点,一个计算器程序
我刚刚学java两个星期,试着写了一个计算器,通不过编译,问题好象出在Stack类上,请高手指点,在线等候.
import java.util.*;

class Calculator
{
    private String expression="0";
    public Calculator(){}
    public Calculator(String expression)
    {
        this.expression = expression;
    }
    public void setExpression(String expression)
    {
        this.expression = expression;
    }
    private char compare(char operation1,char operation2)throws ExpressionException
    {
        char ch;
        switch(operation1)
        {
            case '+':
            case '-':
                if(operation2=='+'||operation2=='-'||operation2==')'||operation2=='=')
                    ch='<';
                else
                    ch='>';
                break;
            case '*':
            case '/':
                if(operation2=='(')
                    ch='<';
                else
                    ch='>';
                break;
            case '(':
                if(operation2==')')
                    ch='=';
                else
                    ch='<';
                break;
            case '=':
                if(operation2=='=')
                    ch='=';
                else
                    ch='<';
                break;
            default:
                throw new ExpressionException("表达式错误,请更正.");
        }
        return ch;
    }
    private void checkExpression()throws ExpressionException
    {
        int ilength = expression.length();
        for(int i=0;i<ilength;i++)
        {
            char ch = expression.charAt(i);
            if(ch!='+'&&ch!='-'&&ch!='*'&&ch!='/'&&ch!='('&&ch!=')'&&ch!='='&&(ch<48||ch>57))
                throw new ExpressionException("表达式错误,请更正.");
        }
    }
    private boolean isOperation(char cOperation)
    {
        if(cOperation=='+'||cOperation=='-'||cOperation=='*'||cOperation=='/'||cOperation=='('
            ||cOperation==')'||cOperation=='=')
            return true;
        return false;
    }
    private int calculate(int operator1,char cOperation,int operator2)
    {
        switch(cOperation)
        {
            case '+':
                return operator1+operator2;
            case '-':
                return operator1-operator2;
            case '*':
                return operator1*operator2;
            case '/':
                return operator1/operator2;
        }
    }
    private void showResult(int result)
    {
        System.out.println("表达式的值为:"+result);
    }
    private void showExpression()
    {
        System.out.println("表达式:"+expression);
    }
    public void run()
    {
        showExpression();
        try
        {
            checkExpression();
        }
        catch(ExpressionException e)
        {
            e.printStackTrace();return;
        }
        Stack operationStack = new Stack();
        Stack operatorStack = new Stack();
        operationStack.push('=');
        while(!operationStack.empty())
        {
            int i=0;
            if(Character.isDigit(expression.charAt(i)))
            {
                int j=i;
                do
                {
                    j++;
                }while(Character.isDigit(expression.charAt(j)));
                int operator = Integer.parseInt(expression.substring(i,j));
                operatorStack.push(operator);
                i=j;
            }
            else
            {
                char operation2 = expression.charAt(i);
                char relation;
                try
                {
                    relation = compare(operationStack.peek(),operation2);
                }
                catch(ExpressionException e)
                {
                    e.printStackTrace();return;
                }
                switch(relation)
                {
                    case '<':
                        operationStack.push(operation2);
                        i++;break;
                    case '=':
                        operationStack.pop();i++;break;
                    case '>':
                        char operation1 = operationStack.pop();
                        int operator2 = operatorStack.pop();
                        int operator1 = operatorStack.pop();
                        operatorStack.push(calculate(operator1,operation1,operator2));
                        break;    
                }
            }
        }
        showResult(operatorStack.pop());
    }
}

class ExpressionException extends Exception
{
    public ExpressionException(String msg)
    {
        super(msg);
    }
}

class CalculatorTest
{
    public static void main(String [] args)
    {
        Calculator calculator = new Calculator(args[0]);
        calculator.run();
    }
}
搜索更多相关的解决方案: 计算器  expression  Calculator  char  

----------------解决方案--------------------------------------------------------
这个和我用c写的很像...你去看看呢
----------------解决方案--------------------------------------------------------
我也用c写过了!java编译就是过不了!不知道为什么!
----------------解决方案--------------------------------------------------------
大哥们帮帮忙吧!!!!
----------------解决方案--------------------------------------------------------
  相关解决方案