0

私はJavaの初心者で、混合数と分数を受け入れることができる計算機を作ろうとしていますが、値を計算するのではなく、2つを組み合わせるだけです。(ex.1 + 1/2 答えは 11/2 )` import java.util.Scanner; パブリッククラス電卓{

public static void main(String[] args) {
Scanner Woith = new Scanner(System.in);

System.out.println("Welcome to the Calc-O-Lator 9000\nthis calculator is able to\nadd, subtract, mulitiple, divide, and handle exponents of\nFRACTIONS\n\nenter 'quit' when done");
System.out.println("To input a mixed number use an underscore in addition with a slash(ex. 2_1/2), also provide a space between the first number and operator\n and the operator and the second number.");
Boolean on=true;
Scanner console=new Scanner(System.in);String firstNumber = Woith.next();
    if (firstNumber.equals("quit")) {
        on = false;
        System.out.println("goodbye");


    } else {
        firstNumber = parseFullNumber(firstNumber);
    }
    String operator = Woith.next();
    if (operator.equals("quit")) {
        on = false;
        System.out.println("goodbye");

    } else if (operator.equals("+") || operator.equals("-") || operator.equals("/") || operator.equals("*")) {
    } else {
        throw new ArithmeticException();
    }

    String secondNumber = Woith.next();
    if (secondNumber.equals("quit")) {
        on = false;
        System.out.println("goodbye");

    } else {
        secondNumber = parseFullNumber(secondNumber);
    }
    int wholeNumber = 0;
    int numerator = 0;
    int denominator = 0;
    System.out.println(calculate(operator ,  firstNumber, secondNumber, wholeNumber, numerator, denominator));
}
public static String parseFullNumber(String input) {
    int wholeNumber = 0;
    int numerator = 0;
    int denominator = 0;
    int underscoreIdx = input.indexOf('_');
    int slashIdx = input.indexOf('/');
    if (underscoreIdx > -1) { 
        wholeNumber = Integer.parseInt(input.substring(0, underscoreIdx));
        numerator = Integer.parseInt(input.substring(underscoreIdx + 1, slashIdx));
        denominator = Integer.parseInt(input.substring(slashIdx + 1, input.length()));
    } else {
        if (slashIdx > -1) {

            numerator = Integer.parseInt(input.substring(0, slashIdx));
            denominator = Integer.parseInt(input.substring(slashIdx + 1, input.length()));
        } else {

            wholeNumber = Integer.parseInt(input);
        }
    }
    return reduce(wholeNumber, numerator, denominator);

}

public static String reduce(int wholeNumber, int numerator, int denominator) {
    int absNumerator = Math.abs(numerator);
    if (absNumerator > 1) {
        int commonFactor = 1;
        for (int i = 2; i < Math.min(absNumerator, denominator); i++) {
            if (numerator % i == 0 && denominator % i == 0) {
                commonFactor = i;
            }
        }
        numerator /= commonFactor;
        denominator /= commonFactor;
    }
    if (absNumerator > denominator) {
        int reduction = numerator / denominator;
        if (wholeNumber >= 0) {
            wholeNumber += reduction;
        } else {
            wholeNumber -= reduction;
        }
        numerator %= denominator;
    }
    if (wholeNumber != 0) {
        if (numerator != 0) {
            return wholeNumber + "_" + numerator + "/" + denominator; 
        } else {
            return String.valueOf(wholeNumber);
        }
    } else {
        if (numerator != 0) {
            return numerator + "/" + denominator;
        } else {
            return String.valueOf(0);
        }
    }
}

public static String calculate(String input, String firstNumber,String secondNumber,int wholeNumber,int numerator,int denominator){
    if (input.contains ("+"))
    {
        if(!input.contains("/")){
            return ("The answer is "+firstNumber + secondNumber);
        }
        if (input.contains("/")){
            return ("The answer is "+(numerator*denominator)+(numerator*denominator)+"/"+(numerator*denominator));
        }
        if(input.contains("_")){
            return ("The answer is "+wholeNumber+numerator+"/"+denominator);
        }
        if(input.contains("-")){
            if(!input.contains("/")){
                return ("The answer is "+firstNumber + secondNumber);
            }
            if (input.contains("/")){
                return ("The answer is "+(numerator*denominator)+"/"+(numerator*denominator));
            }
            if(input.contains("_")){
                return ("The answer is "+wholeNumber+numerator+"/"+denominator);
            }
        if(input.contains("*")){
            if(!input.contains("/")){
                    return ("The answer is "+firstNumber + secondNumber);
                }
            if (input.contains("/")){
                    return ("The answer is "+(numerator*numerator)+"/"+(denominator*denominator));
                }
            if(input.contains("_")){
                    return ("The answer is "+wholeNumber+numerator+"/"+denominator);
                }
            }
        }
    }
    return input;
}

}

4

2 に答える 2