import java.util.regex.*;
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
Scanner scanner = new Scanner(System.in);
System.out.println("Please enter expression ( for example 12+4 or 64/16 ) :");
String s = scanner.nextLine();
Pattern p = Pattern.compile("\\s*(\\d+)\\s*([\\-+*/])\\s*(\\d+)\\s*");
Matcher m = p.matcher(s);
if (m.find())
{
int num1 = Integer.parseInt(m.group(1));
String operation = m.group(2);
int num2 = Integer.parseInt(m.group(3));
System.out.printf("Result = %d", Calc(num1, num2, operation));
} else
{
throw new UnsupportedOperationException("Error: Invalid expression.");
}
}
public static int Calc(int num1, int num2, String operation)
{
if (operation.equals("+"))
{
return num1 + num2;
} else if (operation.equals("-"))
{
return num1 - num2;
} else if (operation.equals("*"))
{
return num1 * num2;
} else if (operation.equals("/"))
{
if (num2 == 0)
{
throw new ArithmeticException("Error: Divide by zero.");
}
return num1 / num2;
} else
{
throw new UnsupportedOperationException("Error: Invalid operation.");
}
}
}