【问题标题】:postfix calculator does not work, why? - C后缀计算器不起作用,为什么? - C
【发布时间】:2013-12-29 10:32:25
【问题描述】:

我有后缀计算器,但他不工作,你能帮帮我吗? 我想计算后缀表达式2 sin 2 cos / 5 * 输出是5.000000,但正确的输出是0.17... 计算工作! 我认为问题出在向堆栈添加表达式的某个地方。 谢谢你的建议!

    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]);
        }

步骤:

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

【问题讨论】:

  • 您是否以某种方式对其进行了调试以得出结论?
  • 你不小心在标题中出现了动词
  • 我使用了调试器,我编辑了问题中的步骤

标签: c calculator postfix-notation


【解决方案1】:

这是一个非常大的问题:

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

stack[counter] = myString;

在这里您将stack[counter] 中的指针设置为指向myString。但是您忘记了myString 将始终是同一个指针,因此每当您更改stack 中指向myString 的myString all 条目时,都会更改,因为它都是相同的字符串.

【讨论】:

  • @TýnaŠindelářová 在 C 语言中,这并不容易。最好的选择可能是将stack 作为数组数组(例如char stack[1000][20])并使用strcpy 复制到字符串中(或直接将sprintf 复制到其中)。
  • 那么只有双值的数组呢?双栈[1000]..?
  • @TýnaŠindelářová 是的,那可能会更好。
  • 如何复制myString,例如使用strdup()?编辑:使用double stack[1000] 要好得多。
  • @alk 是的,但是你必须记住free 字符串。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-08-12
  • 1970-01-01
  • 1970-01-01
  • 2021-10-10
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多