简体   繁体   中英

Simple Java Calculator to Solve Expressions

I'm working on an calculator that solves expressions. I'm trying to figure out how to how make it calculate in PEMDAS order. I have a for loop to traverse the array list and a switch that calls a class that does the math. I've tried if statements, but haven't been able to figure it out.

How can I change this to make sure the expression is solved in the proper order? Here's what I have so far:

/*
Peter Harmazinski
Simple Calculator

This program solves expressions
*/

import java.util.*;

public class SimpleCalculator2 {
    static SimpleMath math = new SimpleMath();

    public static void main(String[] args) {
        Scanner console = new Scanner(System.in);
        boolean again = true;
        double number1 = 0.0;
        double number2 = 0.0;
        double answer = 0.0;
        double results = 0.0;
        String delims = "[ ]+";

        getIntroduction();

        while (again) {
            System.out.println("Please enter your expression.");
            String input = console.nextLine();
            System.out.println("This is the user's input: " + input);

            //Parses string into array list
            List<String> list = new ArrayList<String>(Arrays.asList(input.split(delims)));
            System.out.println("list: " + list);

            results = doMath(list, number1, number2);
            getResults(results);
        }
        console.close();
    }

    public static void getIntroduction() {
        System.out.println("This is a simple calculator that solves expressions.");
    }

    //Traverses array list to identify operators and does math for surrounding numbers
    //then answer is inserted in i-1 element and the elements i and i+1 are deleted

    public static double doMath(List<String> list, double number1, double number2) {
        double answer = 0.0;
        double results = 0.0;
        while (list.size() > 1) {
            for (int i = 0; i < list.size(); i++) {
                    switch (list.get(i)) {
                        case "*" :
                            number1 = Double.parseDouble(list.get(i - 1));
                            number2 = Double.parseDouble(list.get(i + 1));
                            answer = math.multiply(number1, number2);
                            System.out.println(answer);
                            list.add(i - 1, Double.toString(answer));
                            list.subList(i, i + 3).clear();
                            System.out.println(list);
                            break;
                        case "/" :
                            number1 = Double.parseDouble(list.get(i - 1));
                            number2 = Double.parseDouble(list.get(i + 1));
                            answer = math.divide(number1, number2);
                            System.out.println(answer);
                            list.add(i - 1, Double.toString(answer));
                            list.subList(i, i + 3).clear();
                            System.out.println(list);
                            break;
                        case "+" :
                            number1 = Double.parseDouble(list.get(i - 1));
                            number2 = Double.parseDouble(list.get(i + 1));
                            answer = math.add(number1, number2);
                            System.out.println(answer);
                            list.add(i - 1, Double.toString(answer));
                            list.subList(i, i + 3).clear();
                            System.out.println(list);
                            break;
                        case "-" :
                            number1 = Double.parseDouble(list.get(i - 1));
                            number2 = Double.parseDouble(list.get(i + 1));
                            answer = math.subtract(number1, number2);
                            System.out.println(answer);
                            list.add(i - 1, Double.toString(answer));
                            list.subList(i, i + 3).clear();
                            System.out.println(list);
                            break;
                        }
                }   
            }   
        return answer;
    }

    public static void getResults(double results) {
        System.out.println("Results are: " + results);
    }
}

I think a pretty standard algorithm to achieve this is Dijkstra's Shunting Yard Algorithm followed by postfix evaluation. You can read about it here, and the pseudocode is here, but you may need to have some understanding of basic data structures: http://en.wikipedia.org/wiki/Shunting-yard_algorithm .

If you don't know about stack's, queue's and postfix notation, you could write a slower, straightforward, but messier implementation. I tried this one time with sin, cos, trig, log, as well, and the code turned out functional, but not something I would try again.

Basically, the idea is to find the highest priority expression with just 1 operator, evaluate it, and replace it with it. Here's some pseudocode:

input = [user input]
while expression still contains (, ), +, -, *, or /:
    toEvaluate = highest priority expression with 1 operator (e.g. 1+2, or 2*3)
    calculate the decimal value of toEvaluate
    modify input so that you replace toEvaluate with its decimal value

Note that in your doMath() implementation, in your for loop, you just evaluate all operators as soon as you see them. For example, consider

1+2*3

You will see + first, calculate 1+2 and then multiply that result by 3. Instead, you need to go through the whole list first, find the highest priority operator, evaluate that, then start over from the beginning again.

Since you are entering it into a string why not make a for loop that iterates through every char?

// This for loop will get you the string with only one operator in it.
// EX: 30+32
// You will have to find some way to compute this string.
for(int i = 0; i < userInput.length(); i++)
{
   //int a will be the INDEX of the first operator
   //int b will be the INDEX of the place where a new operator comes up 
   char c = userInput.getCharAt(i);
   a = 0;
   //I'm just gonna do it with + and - for now. Add any other operators.
   if(c == '+' || c == '-')
   {
       b = i;
       String stringToCompute = userInput.substring(a,b);
       //Find some way to take the string there then compute it. 
       //Maybe another for loop with stringToCompute to find where the
       // operator is and then add/subtract the two doubles.  

       // Now reset it 
       a = i;
       b = null;
   }
}

Here are a couple of possible designs to solve this:

  1. You could split the processing into two parts: parsing and evaluating. In the parsing phase you convert the strings to a data structure representing how it will be evaluated. In the evaluation phase you traverse the tree and evaluate the expression.

  2. You could split the list by the operators to evaluate last (plus and minus), evaluate each of the segments (times and divide). This could be recursive if well designed.

The first option is better as it is more extensible but it is also more effort to implement. Here is an example data structure to generate via parsing and evaluate:

interface Term {
    double getValue();
}

enum Operator {
    MULTIPLY, DIVIDE, ADD, SUBTRACT;
    double getValue(List<Double> operands) {
        ...
    }
}

class Operation implements Term {
    List<Term> operands;
    Operator operator;
    double getValue() {
        return operator.getValue(operands);
    }
}

class Constant implements Term {
    private final double value;
    double getValue() {
        return value;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM