Question

i have postfix calculator, but he is not working, can you please help me? I want calculate postfix expression 2 sin 2 cos / 5 * Output is 5.000000, but correct output is 0.17... Calculations work! I think the problem is somewhere in adding expressions to the stack. Thank you for any advice!

    const char *arrayPostfix[1000];

        //.
        //.
        //.
        //Filling the field
        //field contents:
        //2 <SIN> 2 <COS> </> 5 <*>

        const char *stack[1000];
        int counter = 0;
        int i;
        int numberofElement = 7; // the number of elements - 2 <SIN> 2 <COS> </> 5 <*>
        char myString[100];
        double help;

        for (i = 0; i < numberofElement; i++) {         


            if (arrayPostfix[i][0] != '<') {
                // IS A NUMBER
                stack[counter] = arrayPostfix[i];
                counter++;

            } else {
                // IT ISNT NUMBER
                //// GET LAST OPERATOR - typedef struct 
                // this works
                operator lastOperator;
                int x = 0;

                for (x = 0; x < sizeof(operatory) / sizeof(operator); x++) {
                    if (strcmp(arrayPostfix[i], operatory[x].nazev) == 0) {
                        lastOperator = operatory[x];
                        break;
                    }
                }
                // Operators are ok..

                double n1, n2;

                if (lastOperator.numberofOperators == 2) {// a+b, a-b, a*b, a/b

                    counter--;
                    n1 = atof(stack[counter]);//string to double


                    counter--;
                    n2 = atof(stack[counter]);//string to double


                    help = lastOperator.funkce_dvaOperator(n2, n1); // this works, calculate
                    sprintf(myString, "%f", help); //double to string

                    stack[counter] = myString;
                    counter++;

                } else { // sin(a), cos(a)....

                    counter--;
                    n1 = atof(stack[counter]); //string to double

                    help = lastOperator.funkce_jedenOperator(n1);   // this works, calculate        

                    sprintf(myString, "%f", help); //double to string

                    stack[counter] = myString;
                    counter++;

                }

            }
                    return atof(stack[0]);
        }

steps:

1) add number 2
--------------------------------------------
Index [ 0 ] je : 2
--------------------------------------------


2) calculate sin(2)
--------------------------------------------
Index [ 0 ] je : 0.034899
--------------------------------------------


3) add number 2 - but in stac [0] is fail value..why?
--------------------------------------------
Index [ 0 ] je : 0.034899
Index [ 1 ] je : 0.034899
Was it helpful?

Solution

This here is a very big problem:

sprintf(myString, "%f", help); //double to string

stack[counter] = myString;

Here you set the pointer in stack[counter] to point to myString. But you forget that myString will always be the same pointer, so whenever you change myString all entries in stack that points to myString will be changed as it is all the same string.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top