如何更改我的代码以接受用户的双值?

时间:2013-11-17 07:48:24

标签: java

import java.util.Scanner;
class MyCalculator {

public static void main(String[] args){

    Scanner input = new Scanner(System.in);

    System.out.println("Enter a expression : ");
    while(input.hasNext()){


    String exp = input.next();

    System.out.println(""+exp+" = "+ Calculate(exp));
    }
}



//define a method to decide the operators' priority 
private static int priority(char input){
    switch(input){
    case'(' : return 3; //parenthesis have highest priority
    case')' : return 3;
    case'*' : return 2; //multiplication and division have lower priority than parenthesis 
    case'/' : return 2;
    case'+' : return 1; //plus minus have lowest operator priority
    case'-' : return 1; 
    default : return 0;//numbers have lowest priority
    }
}



//now we define our big method Calculate
public static int Calculate(String exp){
    //firstly we try to check if the exp is already a number or a simple expression 
    //or having high level operator
    //the key recursive idea is to compute inner results if there is higher level operators
    //until the result is return

    char[] inputs = exp.toCharArray();//we process string to char arrays for easy control


    //now we check the highest level operators and the count
    int highestPriority =0;
    int operatorCount =0;//this value is to keep track of how many operators remaining, mostly to deal with negative number cases


    for(int i=0;i<inputs.length;i++)
    {
        if(priority(inputs[i])>highestPriority)
        {
            highestPriority = priority(inputs[i]);//update highest priority if necessary
        }
        if(priority(inputs[i])>0)
        {
            ++operatorCount;
        }
    }


    //after that we process in order

    if(operatorCount==0)//no operator remaining
    {
        return Integer.parseInt(exp);//Immediately return the result
    }
    else if(highestPriority==1){//-+ remaining
        //we firstly deal with the negative numbers
        if(operatorCount==1 && inputs[0]=='-')//if only operator is the leading minus operator
        {
            return Integer.parseInt(exp);
        }

        //otherwise, we need to find the operatorand compute result             
            int opePosition  = -1;//set as -1 as non set
            boolean ifPlusSign = true;//this is to know if plus or minus sign

            for(int i=0;i<inputs.length;i++)
            {
                //notice we may encounter the case of -1-1, thus the leading minus sign should be ignored
                if(inputs[i]== '+' || (inputs[i]=='-' && i!=0))// we differenciate here because we need know if plus or minus sign
                {
                    opePosition = i;
                    ifPlusSign = inputs[i] == '+'?true:false;
                    break;                      
                }

            }

            //after we identify the opePosition, we need find start/end of left and right operand
            int leftOperandStart = 0;
            int rightOperandEnd = opePosition+1;

            //we need to deal with negative value cases
            if(inputs[rightOperandEnd]=='-')
            {
                rightOperandEnd++;
            }

            while(rightOperandEnd<inputs.length && priority(inputs[rightOperandEnd])<1)
            {
                rightOperandEnd++;
            }

            int leftOperand = Integer.parseInt(exp.substring(0, opePosition));
            int rightOperand = Integer.parseInt(exp.substring(opePosition+1, rightOperandEnd));

            int innerResult = 0;
            if(ifPlusSign)
                {
                innerResult = leftOperand+rightOperand;
                }
            else 
                {
                innerResult = leftOperand - rightOperand;
                }

            return Calculate(""+ innerResult+ exp.substring(rightOperandEnd));  
    }

    else if(highestPriority==2)
    {
        // */ remaining
        /*if(operatorCount==1 && inputs[0]=='-'){
            return Integer.parseInt(exp);
        }*/

            int opePosition  = -1;
            boolean ifMultiplySign = true;//this is to know if plus or minus sign


            for(int i=0;i<inputs.length;i++)
            {
                if(inputs[i]== '*' || (inputs[i]=='/' && i!=0))
                {                       
                    opePosition = i;
                    ifMultiplySign = inputs[i] == '+'?true:false;
                    break;                      
                }
            }


            int leftOperandStart = opePosition-1;

            while(leftOperandStart>=0 && priority(inputs[leftOperandStart])<1)
            {
                leftOperandStart--;
            }

            int rightOperandEnd = opePosition+1;

            if(inputs[rightOperandEnd]=='-')
            {
                rightOperandEnd++;
            }

            while( rightOperandEnd<inputs.length && priority(inputs[rightOperandEnd])<1)
            {
                rightOperandEnd++;
            }

            int leftOperand = Integer.parseInt(exp.substring(leftOperandStart+1, opePosition));
            int rightOperand = Integer.parseInt(exp.substring(opePosition+1, rightOperandEnd));

            int innerResult = 0;
            if(ifMultiplySign)innerResult = leftOperand * rightOperand;
            else innerResult = leftOperand / rightOperand;

            return Calculate(exp.substring(0, leftOperandStart+1)+innerResult+exp.substring(rightOperandEnd));

}

    else 
    {
        int parOpen = -1;
        int parEnd = -1;
        int parOpenEndiff = 0;

        for(int i=0;i<inputs.length;i++)
        {
            if(inputs[i]=='(')
            {
                parOpen=parOpen<0?i:parOpen;
                parOpenEndiff++;
            }
            else if(inputs[i]==')')
            {
                parOpenEndiff--;
                if(parOpenEndiff==0)
                {
                    parEnd = i;
                    break;
                }
            }
        }

        int innerResult = Calculate(exp.substring(parOpen+1,parEnd));
        return Calculate(exp.substring(0, parOpen)+innerResult+exp.substring(parEnd+1));

    }
}

}

如何更改我的代码以接受用户的双倍值? (实际上这个程序适用于int值)。请问你能帮我自定义双值的程序......

1 个答案:

答案 0 :(得分:0)

String exp = input.next(); - &gt; double exp = input.nextDouble();

或者:

String exp = input.next();
double value = Double.parseDouble(exp);

在交换机默认情况下执行double value = Double.parseDouble(exp);