Abdul Mohsin
BAN USERhttp://in.linkedin.com/pub/abdul-mohsin/10/70a/31a
package com.abdul.az;
import java.util.Stack;
public class Problems {
enum Operation {
PLUS('+'), MINUS('-'), MULTIPLY('*'), DIVIDE('/');
char operation;
Operation(char operation) {
this.operation = operation;
}
int getValue(int a, int b) {
if (operation == '+') {
return a + b;
} else if (operation == '-') {
return a - b;
} else if (operation == '/') {
return a / b;
} else if (operation == '*') {
return a * b;
}
throw new RuntimeException("Invalid operation");
}
public static Operation getOperation(char c) {
for (Operation op : Operation.values()) {
if (op.operation == c) {
return op;
}
}
throw new RuntimeException("Invalid Operation for :" + c);
}
};
public static void main(String[] args) {
// pairsForTwoIntegerArrays();
mathmeticalExpression("2+2+4+4");
}
private static void mathmeticalExpression(String string) {
int countTotal = 0;
boolean initialFetch = true;
for (int i = 0; i < string.length(); i++) {
char c = string.charAt(i);
if (initialFetch) {
int arg1 = Character.getNumericValue(string.charAt(i));
int arg2 = Character.getNumericValue(string.charAt(i + 2));
countTotal = Operation.getOperation(string.charAt(i + 1))
.getValue(arg1, arg2);
i += 2;
initialFetch = false;
} else {
int arg2 = Character.getNumericValue(string.charAt(i + 1));
countTotal = Operation.getOperation(string.charAt(i)).getValue(
countTotal, arg2);
i += 1;
}
}
System.out.println("*********** Total:" + countTotal);
}
}
Rephenryhokinsh, Android test engineer at ABC TECH SUPPORT
I’m Henry, I am Children's librarian in Rolling Thunder .My Work involves the responsibility for supervising the children ...
- Abdul Mohsin December 30, 2014