I have created a queue using linked list that takes a string in the format 1 + 3 + 5 =
or 1-3+2-4 =
but the following code which is supposed to do the calculation given the sign and produce the answer is not working right for example if I passed a string like 66 - 31 - 21 + 43 =
is passed the answer turns out to be -47
instead of 57
. Can someone help me solve it or point to what I'm doing wrong.
void printQueue(struct node* head)
{
struct node* temp;
char *token, *del=" ";
int total = 0;
while (head != NULL)
{
token = strtok(head->data, del);
while (token != NULL) {
int a = strcmp(token, "+");
int b = strcmp(token, "-");
if (a == 0)
{
printf("+");
total = total + *token;
}
else if (b == 0) {
printf("+");
total = total - *token;
}
printf("%s ", token);
token = strtok(NULL, del);
}
printf(" %d
",subtraction);
head = head->next;
}
}
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…