My code fails when I input something like 3(5)
.
How do I modify my code so that it solves in expression in the middle and then multiplies?
double parseTerm() {
double x = parseFactor();
while(true) {
if (shift('*')){
x *= parseFactor(); // multiply
}
else if (shift('/')){
x /= parseFactor(); // divide
}
else{
return x;
}
}
}
double parseExpression() {
double x = parseTerm();
while(true) {
if (shift('+')){
x += parseTerm(); // addition
}
else if (shift('-')){
x -= parseTerm(); // subtraction
}
else{
return x;
}
}
}
double parseFactor() {
if (shift('+')){
return parseFactor(); //plus
}
if (shift('-')){
return -parseFactor(); //minus
}
double x;
int startPos = this.pos;
if (shift('(')) { // brackets
x = parseExpression();
shift(')');