Well, it took me less than a day to modify the existing function, since (unusually) I knew pretty much what how what I wanted the function to do was going to be acheived (and sort of understand the function below. It is still abit buggy but works for more things than ever before! I was thinking of breaking it up into more functions, but the pointers (namely operands) doesn't get incremented properly when I try this.
Works for: Single digit operands w/ one operator, 1 2 + 4 5 + *, 1 2 + 4 5 - *, 1 2 - 4 5 + *, 1 2 - 4 5 - *, Multiple digits w/ one operator.
Doesn't work for: Multiple digits w/ > one operator, greater than one operator.
Still looking for more cases that it doesn't work for (this function will need to work for ALL cases):
Code:
//INP: equation OUT: evaluation
void EvaluateEquation(char *equation, char *evaluation) {
int op1, op2, signbit, nextToken = 0, SetBack = 0;
char *original_evaluation = evaluation;
char *writeoperation, *operands;
operands = (char *) malloc(51);
memset(operands, '\0', 51);
char *original_operands = operands;
while (*equation != '\0') {
if (isdigit(*equation)) {
while (CheckIfDigit(*equation)) {
*operands++ = *equation++; //Store number to operands
if (!nextToken) nextToken = 1; //Space Seperate each operand
}
}
else if (*equation == ' ') equation++;
else if (CheckIfOperator(*equation)) {
op1 = 0;
op2 = 0;
//Go back to start to read operands in correct order
operands = original_operands;
/*Neither operand is an operator, this needs
to be set this back after oparands have been stored */
if (!(CheckIfDigit(*operands) || *operands == '-')) {
operands = original_evaluation;
SetBack = 1;
}
//keep extracting until a ' ' is found & store the number
signbit = 0;
do {
if (*operands == '-') {
signbit = 1;
operands++;
}
filloperands(&op1, *operands);
operands++;
} while (*operands != ' ' && *operands != '\0');
if (signbit) op1 = -op1;
operands++; //Skip the space
/*Neither operand is an operator, this needs
to be set this back after oparands have been stored */
if (!(CheckIfDigit(*operands) || *operands == '-')) {
operands = evaluation;
SetBack = 1;
}
//keep extracting until another ' ' is found & store number
signbit = 0;
do {
if (*operands == '-') {
signbit = 1;
operands++;
}
filloperands(&op2, *operands);
operands++; //The first element is not always a ' '
} while (*operands != ' ' && *operands != '\0');
if (signbit) op2 = -op2;
writeoperation = performoperation(op1, op2, *equation);
//All remains of operands MUST be removed before continuing
while (*operands != '\0') *operands-- = '\0';
if (SetBack) evaluation = original_evaluation;
if (writeoperation == NULL){ //Check for DB0
strcpy(evaluation, "ERROR: Division by Zero");
return;
}
strncpy(evaluation, writeoperation, strlen(writeoperation));
evaluation += strlen(writeoperation);
//MessageBox(NULL, original_evaluation, "original_evaluation", MB_OK);
operands = original_operands;
equation++;
}
//if nextToken add space to evaluation
if (nextToken) { //Space-seperate each token, this helps w/ multiple
*operands++ = ' '; //digits
nextToken = 0;
}
}
equation++;
}
void filloperands(int *op, char evaluation) {
*op = *op * 10 + (evaluation - '0');
return;
}
Also, thanks tabstop for helping so much (don't know if I would have gotten her on my own). Hope the function is not too long.