I am making a calculator in java, and have made it so that if the user enters any string, it will extract numbers and operations, and then solve them out from left to right. The thing is that it doesn't solve for order of operations, so if you put 5+5*5, the answer it gives you is 50. Here is my class that has the main function.
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);
}
}
Here is the calculator class:
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;
}
}
And finally my equations class: `package calculate;
import java.util.*;
public class Equations {
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;
}
} `(Sorry if its not formatted well, I'm new to this stuff