计算器程序中的操作顺序

时间:2016-03-15 14:36:48

标签: java

我在java中制作一个计算器,并且如果用户输入任何字符串,它将提取数字和操作,然后从左到右解决它们。问题是它没有解决操作的顺序,所以如果你把5 + 5 * 5,它给你的答案是50.这是我的类具有主要功能。

package calculate;


import java.util.*;


public class Calculate {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        Equations equations = new Equations();
        ArrayList<Double> numbers;
        ArrayList<Character> operations;
        double num1, num2;
        double result=0;
        String equation;
        Calculator calc = new Calculator();

        System.out.println("Welcome to Affan's calculator, this will calculate any equation (this can't do order of operations yet)\n");
        System.out.print("Enter your equation: ");

        equation = in.nextLine();
        numbers = equations.getnum(equation);
        operations = equations.getoperation(equation);
        num1 = numbers.get(0);

        for (int i=0; i<operations.size(); i++) {
            if(operations.get(i) == ' ') 
                break;
            if(operations.get(i) == '!')
                 result = calc.factorial(num1);
            else if(i+1 < numbers.size()) {
                num2 = numbers.get(i+1);
                result = calc.simpleCalc(num1, num2, operations.get(i));
            }
            num1 = result;
        }  
        System.out.println("The answer is: " + result);
    }
}

这是计算器类:

package calculate;

public class Calculator {
    public double simpleCalc(double num1, double num2, char operation) {
        double res =0;
        switch (operation) {
            case '+': res = num1+num2;
            break;
            case '-': res = num1-num2;
            break;
            case '*': res = num1*num2;
            break;
            case '/': res = num1/num2;
            break;
        }
        return res;
    }
    public double factorial(double num1) {
        double factorial = 1;
        for (int i=1; i<=num1; i++) {
            factorial *= i;
        }
        return factorial;
    }

}

最后我的方程类: `package calculate;

import java.util。*;

公共类方程式{

public int numindex=0; // So this can be accessed them from other classes

// This function extracts all numbers in an equation,
// and then returns an array holding those values
public ArrayList<Double> getnum(String a) {
    char[] num1char = new char[10]; // Have an array to hold characters for numbers
    String num1string; // The string that holds a whole string of individual numbers to parse
    int index = 0; // The control for looping, used with string.charAt()
    int charindex=0;
    ArrayList<Double> numbers = new ArrayList<>();

    // The main loop that keeps looping until you hit the end of the string
    do {
        num1string = null; // reasign to null, to get rid of previous number
        charindex = 0; // reasign char index to get override old numbers
        if (index<a.length()) {

            // Keep looping to get number(s)
            while(a.charAt(index) >= 48 && a.charAt(index) <= 57 ||
                a.charAt(index) == 46) {
                num1char[charindex] = a.charAt(index);
                index++;
                charindex++;

                // If index is bigger than the string, break out of loop to prevent error
                if (!(index < a.length())) {
                    break;
                }
            }
        } 

        // Algorithm to turn char into string, then parse it to double
        num1string = new String(num1char);
        numbers.add(Double.parseDouble(num1string));


        // Only increase the index if there still is a number left
        if((index+1 < a.length()) && 
           (a.charAt(index+1) >= 48 && a.charAt(index+1) <= 57 ||
            a.charAt(index+1) == 46)) {
            index++;
        }

    // The condition for the do while, keep looping while numbers left
    // and index is less than the string length
    }while((index < a.length()) && 
           (a.charAt(index) >= 48 && a.charAt(index) <= 57 ||
            a.charAt(index) == 46) && index<a.length());    

    return numbers;
}

// Similar to the getnum, it returns all the operations in the equation (not yet)
public ArrayList<Character> getoperation(String a) {
    int index = 0;
    ArrayList<Character> operations = new ArrayList<>();
    do {
        if (a.charAt(index) == '+' || a.charAt(index) == '-' || 
            a.charAt(index) == '*' || a.charAt(index) == '/' || a.charAt(index) == '!') {
            operations.add(a.charAt(index));
        }
        index++;
    }while(index<a.length());
    return operations;
}

} `(对不起,如果格式不好,我是这个东西的新手

0 个答案:

没有答案