I'm programming a basic command line calculator in java that can parse expressions and evaluate them, does not handle brackets yet.
For example an expression: \$9+9/2^2*5-1\$.
At this point I can give my program an expression that contains exponents, and It will evaluate them one by one until there are none left.
For example, I can input the following string: \$2^2+1\$ and it will output \$4.0+1\$.
public class Main {
public static void main(String[] args) {
String powerExpression = "2^2+1";
loopPower(powerExpression);
}
public static void loopPower(String exp) {
while (containsExponents(exp)) {
exp = procPower(exp);
}
System.out.println("FINAL ANSWER IS: " + exp);
}
public static boolean containsExponents(String exp) {
boolean contains = false;
for (int i = 0; i < exp.length(); i++) {
if (exp.charAt(i) == '^') {
contains = true;
break;
}
}
return contains;
}
public static String procPower(String exp) {
int operandOneStart = 0;
int operandTwoStart = 0;
int operandTwoEnd = 0;
boolean onFirst = true;
boolean foundOp = false;
for (int i = 0; i < exp.length(); i++) {
if (isCharNumerical(exp.charAt(i))) {
if (onFirst) {
operandOneStart = i;
onFirst = false;
}
if (foundOp) {
operandTwoStart = i;
//Perform op
String operandOne = "";
String operandTwo = "";
//Get operand1
for (int op1 = operandOneStart; op1 < exp.length();op1++) {
if (isCharNumerical(exp.charAt(op1))) {
operandOne += exp.charAt(op1);
}
else {
break;
}
}
//Get operand2
for (int op2 = operandTwoStart; op2 < exp.length(); op2++) {
if (isCharNumerical(exp.charAt(op2))) {
operandTwo += exp.charAt(op2);
}
else {
operandTwoEnd = (op2-1);
System.out.println("Op 2 End: " + operandTwoEnd);
break;
}
if (isCharNumerical(exp.charAt(op2)) & op2 == (exp.length()-1)) {
operandTwoEnd = op2;
System.out.println("Op 2 End: " + operandTwoEnd);
}
}
System.out.println("Operand One: " + operandOne);
System.out.println("Operand Two: " + operandTwo);
boolean noBeforenoAfter = false;
boolean yesBeforeyesAfter = false;
boolean noBeforeyesAfter = false;
boolean yesBeforenoAfter = false;
float opx1 = Float.valueOf(operandOne);
float opx2 = Float.valueOf(operandTwo);
if (operandOneStart == 0 & operandTwoEnd == (exp.length()-1)) {
noBeforenoAfter = true;
exp = String.valueOf(Math.pow(opx1, opx2));
}
if (operandOneStart > 0 & operandTwoEnd < (exp.length()-1)) {
yesBeforeyesAfter = true;
String before = "";
for (int a = 0;a < operandOneStart;a++) {
before += exp.charAt(a);
}
String after = "";
for (int a = operandTwoEnd+1; a < exp.length(); a++) {
after += exp.charAt(a);
}
double subans = Math.pow(opx1,opx2);
exp = before + String.valueOf(subans) + after;
}
if (operandOneStart == 0 & operandTwoEnd < (exp.length()-1)) {
noBeforeyesAfter = true;
String after = "";
for (int a = operandTwoEnd+1; a < exp.length(); a++) {
after += exp.charAt(a);
}
double subans = Math.pow(opx1,opx2);
exp = String.valueOf(subans) + after;
}
if (operandOneStart> 0 & operandTwoEnd == (exp.length()-1)) {
yesBeforenoAfter = true;
String before = "";
for (int a = 0;a < operandOneStart;a++) {
before += exp.charAt(a);
}
double subans = Math.pow(opx1,opx2);
exp = before+subans;
}
//
break;
}
}
else {
if (exp.charAt(i) == '^') {
foundOp = true;
}
else {
onFirst = true;
foundOp = false;
}
}
}
return exp;
}
public static boolean isCharNumerical(char c) {
boolean numerical = false;
switch (c) {
case '0':
numerical = true;
break;
case '1':
numerical = true;
break;
case '2':
numerical = true;
break;
case '3':
numerical = true;
break;
case '4':
numerical = true;
break;
case '5':
numerical = true;
break;
case '6':
numerical = true;
break;
case '7':
numerical = true;
break;
case '8':
numerical = true;
break;
case '9':
numerical = true;
break;
case '.':
numerical = true;
break;
default:
numerical = false;
break;
}
return numerical;
}
}
What is the quality of what I have written/coded? Anything I need to change? Should I go on to make my parser be able to do other arithmetic operations such as multiplication, division, adding, subtracting, or does this need lots of improvement at this stage?