Search code examples
ccalculatorrpn

RPN Calculator in C


Can someone please explain why I keep getting gibberish after I use strok() on myString? A picture of my output is at the bottom. I tried initializing every single element in myString to be a NULL character but my code still doesnt work :(

Expected Output:

enter image description here

My Output :

enter image description here

int main()
{
    int i;
    char myString[60];
    char *token;
    float result;
    float x;
    float y;
    struct Stack myStack;
    StackInit(&myStack);


    BOARD_Init();

    printf("\nWelcome to my RPN calculator.\n");
    printf("Enter floats and + - / * in RPN format: \n >");

    for (i = 0; i < sizeof (myString); i++) {
        myString[i] = '\0';
    }

    fgets(myString, sizeof (myString), stdin);



    token = strtok(myString, " ");
    while (token != NULL) {
        printf("%f\n", atof(token));
        token = strtok(NULL, " ");

        if (atof(token) != 0) {
            StackPush(&myStack, atof(token));
        } else if (*token == '+') { // if token is addition
            StackPop(&myStack, &y);
            StackPop(&myStack, &x);
            result = x + y;
            StackPush(&myStack, result);

        } else if (*token == '-') { // if token is subtraction
            StackPop(&myStack, &y);
            StackPop(&myStack, &x);
            result = x - y;
            StackPush(&myStack, result);
        } else if (*token == '*') { // if token is multiplication
            StackPop(&myStack, &y);
            StackPop(&myStack, &x);
            result = x * y;
            StackPush(&myStack, result);
        } else if (*token == '/') { // if token is division
            StackPop(&myStack, &y);
            StackPop(&myStack, &x);
            result = x / y;
            StackPush(&myStack, result);
        }

    }




while (1);

}


Solution

  • As pointed out by @BLUEPIXY in a comment, your code has a problem here:

    token = strtok(myString, " ");   // Here you get the first sub string
    while (token != NULL) {
        printf("%f\n", atof(token));
        token = strtok(NULL, " ");   // Here you get the second sub string
                                     // and throw away the first substring
                                     // So all the code below is never executed
                                     // for the first substring.
                                     // In other words: You never push the first
                                     // float to the stack
    
        if (atof(token) != 0) {
            StackPush(&myStack, atof(token));
    

    What you should do instead is to move token = strtok(NULL, " "); to the end of the loop:

    while (token != NULL) {
        printf("%f\n", atof(token));
        // Not here............. token = strtok(NULL, " ");
    
        if (atof(token) != 0) {
            StackPush(&myStack, atof(token));
        } else if (*token == '+') { // if token is addition
        //...
        //...
        }
    
        token = strtok(NULL, " ");    // but here
    }