Thanks, that fixed it! After that loop, I needed to call filloperands again as op2 was not being read. Thanks for your help tabstop and zigga15. I liked that youtube clip btw, not quite what I meant but anyway. I was thinking more of the push & pop operations on char *s. It's using the stack & these structures that I intended to get more familiar w/. I understand the basics of *s, I just haven't used them for long enough.
Still, back to the same problem I had yesterday, The output is incorrect for expressions like 1 2 - 4 5 + * and for 1 2 - 4 +. I will post the wole code below:
Code:
int CheckIfOperator(char c) {
char *listofoperators = "+-*/%";
int found = 0;
while ((*listofoperators != '\0') && (!found)) {
if (*listofoperators == c) found = 1;
listofoperators++;
}
return found;
}
void filloperands(int *op, char evaluation) {
int signbit = 0;
if (evaluation == '-') signbit = 1;
*op = *op * 10 + (evaluation - '0');
if (signbit) {
*op = -*op;
signbit = 0;
}
return;
}
//INP: equation OUT: evaluation
void EvaluateEquation(char *equation, char *evaluation) {
int op1, op2, nextToken = 0;
char *evalcopy;
char *original_evaluation = evaluation;
evalcopy = evaluation; //Just for debug
char *writeoperation;
while (*equation != '\0') {
if (isdigit(*equation)) {
while (isdigit(*equation) || *equation == '.') {
*evaluation++ = *equation++; //Store number to evaluation
if (!nextToken) nextToken = 1; //Space Seperate each operand
}
}
else if (*equation == ' ') equation++;
else if (CheckIfOperator(*equation)) {
op1 = 0;
op2 = 0;
evaluation -= 2;
/*Read operands in reverse order starting from the end */
//keep extracting until a ' ' is found & store the number
do {
filloperands(&op2, *evaluation);
evaluation--;
} while (*evaluation != ' ' && evaluation > original_evaluation);
evaluation--; //Skip the space
//keep extracting until another ' ' is found & store number
while (*evaluation != ' ' && evaluation > original_evaluation) {
filloperands(&op1, *evaluation);
evaluation--;
}
filloperands(&op1, *evaluation);
writeoperation = performoperation(op1, op2, *equation);
if (writeoperation == NULL){ //Check for DB0
strcpy(evaluation, "ERROR: Division by Zero");
return;
}
strncpy(evaluation, writeoperation, strlen(writeoperation));
/* the operation has been performed so set the reset address to
the next element, as the operation cannot be overwritten by
an operand. All remains of operands MUST be removed from
evaluation before continuing*/
/*Note: if second operator not found, evaluation = evalcopy
then evaluation-- till *evaluation == ' ' */
equation++;
}
//if nextToken add space to evaluation
if (nextToken) { //Space-seperate each token, this helps w/ multiple
*evaluation++ = ' '; //digits
nextToken = 0;
}
}
equation++;
}
char *performoperation(int op1, int op2, char chroperator) {
char *Returnoperation;
int operation;
switch (chroperator) {
case '+': {
operation = op1 + op2;
break;
}
case '-': {
operation = op1 - op2;
break;
}
case '*': {
operation = op1 * op2;
break;
}
case '/': {
if (!op2) return NULL;
operation = op1 / op2;
break;
}
case '%': {
operation = op1 % op2;
break;
}
}
sprintf(Returnoperation, "%d ", operation);
return Returnoperation;
}