C: Evaluating Expression in a queue not working

房东的猫 提交于 2019-12-25 06:12:07

问题


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\n",subtraction);
        head = head->next;
    }
}

回答1:


Your code is ignoring the numbers, adding up character codes of '+' and '-' characters instead. The number -47 is the result of computing -45-45+43: 45 is the character code for the dash; 43 is the character code for the plus.

In order to work properly, make these changes to your code:

  • Add a sign variable of int type, with the initial value of 1
  • When you see a plus sign, set the sign to 1
  • When you see a minus sign, set the sign to -1
  • When you see a token that is neither a plus nor minus, convert it to an int val (e.g. by calling atoi), and add val * sign to the running total.

This change will make your code work for valid expressions, but it would not fail for some invalid ones. For example, it would take "expressions" such as 1 2 3 as if it were 1+2+3, and so on.




回答2:


There are a few oddities in your code, here's a few:

  1. Your inner loop that checks if token != NULL always has 1 iteration, it probably should be an if statement
  2. You are searching only for the tokens '+' and '-' and adding their character value to your total, rather than the next number, you are ignoring the tokens that hold numbers
  3. When subtracting you are still printing '+' to the screen



回答3:


#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>

typedef struct node{
    int value; 
    struct node *next; 
} Node;

typedef struct queque {
    Node *top;
    Node *tail;
} Queque;

Queque *Q_new(void){
    return calloc(1, sizeof(Queque));
}

void Q_enq(Queque *q, int value){
    Node *node = calloc(1, sizeof(Node));

    if(!node) exit((printf("failed Securing memory\n"),1));
    node->value = value;

    q->tail = q->top ? (q->tail->next = node) : (q->top = node);
}

Node *Q_deq(Queque *q){
    if(q->top){
        Node *node = q->top;
        q->top = q->top->next;
        return node;
    }
    return NULL;
}

void Q_drop(Queque *q){
    Node *node;
    while(node = Q_deq(q))
        free(node);
    free(q);
}

void strToQ(const char *str, Queque *q){
    char *p = (char*)str;
    int num;

    while(*p){
        if(isspace(*p)){
            ++p;
            continue;
        } else if(*p == '+' || *p == '-' || *p == '='){//not used to a number sign
            Q_enq(q, *p++);
            continue;
        }
        num = strtol(p, &p, 10);
        Q_enq(q, num);
    }
}

int Q_calc(Queque *q){
    int total = 0;
    Node *node = Q_deq(q);
    if(!node)
        return 0;

    total = node->value;
    free(node);
    while(node = Q_deq(q)){
        if(node->value == '='){
            free(node);
            break;
        } else {
            int op = node->value;
            free(node);
            node = Q_deq(q);//NOT NULL(if NULL then invalid syntax)
            if(op == '+')
                total += node->value;
            else if(op == '-')
                total -= node->value;
            free(node);
        }
    }
    return total;
}

int main(){
    Queque *q = Q_new();
    strToQ("1 + 3 + 5 =", q);
    printf("%d\n", Q_calc(q));//9, q to be empty
    strToQ("1-3+2-4 =", q);
    printf("%d\n", Q_calc(q));//-4
    strToQ("66 - 31 - 21 + 43 =", q);
    printf("%d\n", Q_calc(q));//57
    Q_drop(q);
}


来源:https://stackoverflow.com/questions/25003855/c-evaluating-expression-in-a-queue-not-working

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!