| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 917 人关注过本帖
标题:高手请指点,一个计算器程序
取消只看楼主 加入收藏
readyforlove
Rank: 1
等 级:新手上路
帖 子:25
专家分:0
注 册:2008-3-5
收藏
 问题点数:0 回复次数:2 
高手请指点,一个计算器程序
我刚刚学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 
2008-06-04 19:54
readyforlove
Rank: 1
等 级:新手上路
帖 子:25
专家分:0
注 册:2008-3-5
收藏
得分:0 
我也用c写过了!java编译就是过不了!不知道为什么!
2008-06-04 20:09
readyforlove
Rank: 1
等 级:新手上路
帖 子:25
专家分:0
注 册:2008-3-5
收藏
得分:0 
大哥们帮帮忙吧!!!!
2008-06-04 20:16
快速回复:高手请指点,一个计算器程序
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.025964 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved