Reputation: 13
Im trying to create a recursive parsing calculator in java for addition, multiplication and factorial, but I'm struggling on the very first part of just reading through the user input to split the input into numbers and operators. While debugging I tried to see where it was going wrong, and I found that when the "+" was going through the if else statements it just skipped over it. I'm really unsure on what the issue is, I originally tried using tokens, and splitting up into substring, but it wasn't going well then either. Any help would be appreciated. Thank you
package com.company;
import java.util.Scanner;
class Main {
public static void main(String[] param) {
String input = input("Please enter an expression");
int n = input.length()-1;
String[] splitter = input.split("(?<=\\G.)");
split(input, n);
//int result = calculate(input);
//String[] splitter = input.split("(?<=\\G.)");
}
public static String split(String input, int n) {
String[] splitter = input.split("(?<=\\G.)");
System.out.println(splitter[n]);
String symbol = splitter[n];
if (symbol.equals("+")) {
evalADD(n, splitter);
}
if (symbol.equals("*")) {
evalMULT(n, splitter);
}
if (symbol.equals("!")) {
evalFACT(n, splitter);
}
else if (Integer.parseInt(splitter[n]) >= 0 && Integer.parseInt(splitter[n]) <=9)
{
if (n != 0) {
n = n - 1;
split(input, n);
}
}
if (n != 0)
n = n - 1;
split(input, n);
return input;
}
public static int evalADD(int n, String [] splitter){
int arg1;
int arg2;
int result;
arg1 = Integer.parseInt(splitter[n+1]);
arg2 = Integer.parseInt(splitter[n+2]);
result = arg1 + arg2;
return result;
}
public static int evalMULT(int n, String [] splitter){
int arg1;
int arg2;
int result;
arg1 = Integer.parseInt(splitter[n+1]);
arg2 = Integer.parseInt(splitter[n+2]);
result = arg1 * arg2;
return result;
}
public static int evalFACT(int n, String [] splitter){
int arg1;
int arg2;
int result;
arg1 = Integer.parseInt(splitter[n+1]);
arg2 = Integer.parseInt(splitter[n+2]);
result = arg1 - arg2;
return result;
}
public static String input(String message) {
Scanner scanner = new Scanner(System.in);
System.out.println(message);
return (scanner.nextLine());
}
}
Upvotes: 1
Views: 4036
Reputation:
I have noticed that you are using the java.util.Scanner
. I wrote a script that should do the task for you by following all of your criteria:
import java.util.Scanner;
class recursiveParsingCalculator {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Ask user to input the expression
System.out.println("Please input the expression");
String userInput = scanner.nextLine();
System.out.println(
"And the final result is: " + recursiveCalculation(userInput, userInput.length() - 1, 0, 0, 0));
scanner.close();
System.exit(0);
}
// Identify the type of character at a specific position
public static char charOfString(String userInput, int i) {
return userInput.charAt(i);
}
/*
* Position must be userInput.length() - 1 initially. currentResults, operand1
* and operand2 are also meant to be initilized with 0.
*/
public static int recursiveCalculation(String userInput, int position, int operand1, int operand2,
int currentResults) {
// If position is zero, just output the operand.
if (position == 0) {
if (Character.isDigit(charOfString(userInput, position))) {
return charOfString(userInput, position) - '0';
} else {
System.out.println("Invalid input.");
}
}
if (position > -1) {
// Check if it is a number or an operator
if (Character.isDigit(charOfString(userInput, position))) {
operand1 = charOfString(userInput, position) - '0'; // First operand
// Check if 2nd char is a number or an operator.
if (Character.isDigit(charOfString(userInput, position - 1))) {
operand2 = charOfString(userInput, position - 1) - '0';
position = position - 1;
}
} else {
// If it is an operator, then proceed to compute the results so far
char operator = charOfString(userInput, position);
// If it is a binary situation
if (operator == '+' || operator == '*') {
currentResults = binaryOperator(operator, operand1, operand2);
operand2 = currentResults;
}
// If it is an unary situation
else if (operator == '!') {
if (currentResults == 0) {
currentResults = operand1;
}
currentResults = unaryOperator(currentResults);
operand2 = currentResults;
} else {
System.out.println("Invalid operator");
return 0; // Return zero by default
}
}
position = position - 1;
}
if (position > -1) {
return recursiveCalculation(userInput, position, operand1, operand2, currentResults);
} else {
return currentResults;
}
}
public static int binaryOperator(char operator, int operand1, int operand2) {
switch (operator) {
case '+':
return operand1 + operand2;
case '*':
return operand1 * operand2;
default:
System.out.println("Invalid binary Operator");
return 0; // Return zero by default
}
}
// Calculate the factorial
public static int unaryOperator(int operand) {
if (operand <= 1)
return 1;
else
return operand * unaryOperator(operand - 1);
}
}
Examples of usage: For a binary operator, input +21, and it will add them for you. With unary, input !3, and it will yield the factorial. Now, you can try any chain of combinations and permutations of numbers with unary and binary operators, and it will calculate the values for you recursively.
For example, consider the input !*3+12: It will add 1 and 2, then multiply it by 3 and finally, it calculates the factorial out of the whole expression, thereby resulting in 362880 as expected.
Upvotes: 1
Reputation: 487
Why don't you assign the input calculation string to a character array and iterate through the array and match the characters '+', '-','*'?
Upvotes: 1