I have got a fully working function that converts infix to postfix, but now I am having trouble w/ the second stage in evaluating the equation in postfix. I realise that there is an example given in K&R but I don't really understand it & have decided to make my own from scratch.

I had this algorithm working yesterday, but I changed it as it was extracting from the wrong end and couldn't parse equations like: 1 2 - 4 5 + *. The problem I am having is:

given the input 1 2 -, it returns 1 instead of -1;

given the input 666 1 -, it returns 65 instead of 665;

given the input 1+2, it returns nothing instead of 3 etc.

I am sure the function performoperation is working correctly & i have also checked that evaluation is correct for the above inputs after the strncpy function is called. I have not changed anything in what calls this function (& also this function is called on a button-click even so I cannot post it here).

No idea as to the cause of this problem (I was going really well until this prob arose) Please Help!

Code:`//INP: equation OUT: evaluation`

char *EvaluateEquation(char *equation, char *evaluation) {

int op1, op2, signbit = 0;

int nextToken = 0;

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 (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 {

if (*evaluation == '-') signbit = 1;

else {

op2 = op2 * 10 + (*evaluation - '0');

if (signbit) {

op2 = -op2;

signbit = 0;

}

}

evaluation--;

} while (*evaluation != ' ');

evaluation--; //Skip the space

//keep extracting until another ' ' is found & store number

while (*evaluation != ' ' && *evaluation != '\0') {

if (*evaluation == '-') signbit = 1;

else {

op1 = op1 * 10 + (*evaluation - '0');

if (signbit) {

op1 = -op1;

signbit = 0;

}

}

evaluation--;

}

writeoperation = performoperation(op1, op2, *equation); //Works fine upto and including this line

if (writeoperation == NULL){ //Check for DB0

strcpy(evaluation, "ERROR: Division by Zero");

return evaluation;

}

strncpy(evaluation, writeoperation, sizeof(writeoperation));

while (*evaluation != ' ') { // ' ' at the end of the operand

evaluation++;

}

evaluation++; //we shouldn't have removed the space

/* 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*/

while (*evaluation != '\0') *evaluation++ = '\0';

/*Note: if second operator not found, evaluation = evalcopy

then evaluation-- till *evaluation == ' ' */

equation++;

}

else if (*equation == ' ') equation++;

//if nextToken add space to evaluation

if (nextToken) { //Space-seperate each token, this helps w/ multiple

*evaluation++ = ' '; //digits

nextToken = 0;

}

}

equation++;

}