Search code examples
cpostfix-notation

Postfix Notation calculation program in C, having some problems


I'm attempting to create a program to run calculations in postfix notation using C, and reading in values from the unix command line. I, however, am quite new to the C language, and am having a bit of trouble getting things to work correctly. I'm getting readouts of NULL and Segmentation Faults, but I'm not really sure how debugging works in the unix command line, so I can't tell you where I am getting the error. I would appreciate any help on the matter.

#include <stdio.h>
double stack(int argc, char* argv[]);

int main(int argc, char* argv[]) {


    double result;

    result = stack(argc, argv);


    printf("%s\n", result);


}

--

#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
static double stck[100];
static int top = 0;

/*Push Function*/
static void push( double v){

    stck[top] = v; /* Places the value at the current top position of the stack */
    top++; /* Increases the top position of the stack */
    printf("%s\n", top);

}
/*Pop Function*/
static double pop() {

    double ret;
    ret = stck[top]; /* Stores the value of the top position in the stack to ret */
    top--; /* Decreases the top position of the stack */
    printf("%s\n", top);
    return ret; /* Returns the value of ret */

}

double stack(int argc, char* argv[]){

    double h; /* Placeholder Variable for the result of the mathematic equations during the loop */
    int i;

    for (i = 0; i <= argc - 1; i++) { /* Loops to read in all values, barring the first, for argv */

        if (strcmp(argv[i], "*") == 0) {

            double a = pop();  /* Pulls in the top value of the stack */
            double b = pop();  /* Pulls in the next top value of the stack*/
            h = b*a; /* Performs the multiplication of the two stack values */
            push(h); /* Returns the result to the top of the stack */

        } else if (strcmp(argv[i], "+") == 0) {
            printf("%s\n", "Made it here plus \0");
            double a = pop();  /* Pulls in the top value of the stack */
            double b = pop();  /* Pulls in the next top value of the stack*/
            h = b+a;  /* Performs the multiplication of the two stack values */
            push(h);  /* Returns the result to the top of the stack */

        } else if (strcmp(argv[i], "/") == 0) {

            double a = pop();  /* Pulls in the top value of the stack */
            double b = pop();  /* Pulls in the next top value of the stack*/
            h = b/a; /* Performs the division of the two stack values */
            push(h);  /* Returns the result to the top of the stack */


        } else if (strcmp(argv[i], "^") == 0) {

            double a = pop();  /* Pulls in the top value of the stack */
            double b = pop();  /* Pulls in the next top value of the stack*/
            h = pow(b,a);  /* Raises the number b by the number a power */
            push(h);  /* Returns the result to the top of the stack */


        } else if (strcmp(argv[i], "-") == 0) {

            double a = pop();  /* Pulls in the top value of the stack */
            double b = pop();  /* Pulls in the next top value of the stack*/
            h = b-a;  /* Performs the subtraction of the two stack values */
            push(h);  /* Returns the result to the top of the stack */

        } else {
            printf("%s\n", "Made it here \0");

            double ph;

            ph = (double)atof(argv[i]);
            printf("%s\n", ph);

            push(ph); /* Places the current value of argv[i] on the top of the stack */

        }

    }
    return stck[0]; /* Returns the final result of the calculation to main.c */


}

Solution

  • Your push and pop operations need to be the exact reverse of each other.

    So if push is

    stackarray[index] = v
    index++     //increment last
    

    pop must be

    index--     //decrement first
    return stackarray[index]
    

    otherwise, pop will always return the value from one slot after the most recently pushed value.


    Debugging a program that uses command-line arguments shouldn't be much different from debugging any other program. But you may need to read the debugger's documentation to learn how to pass the arguments to the program. In gdb:

    gdb> break main
    gdb> run arg1 arg2 arg3 ...
    

    That is, add the arguments to the run line, as if run were the name of the program.