2

我正在尝试创建一个程序来使用 C 以后缀表示法运行计算,并从 unix 命令行读取值。然而,我对 C 语言还是很陌生,并且在让事情正常工作时遇到了一些麻烦。我得到了 NULL 和 Segmentation Faults 的读数,但我不确定在 unix 命令行中调试是如何工作的,所以我不能告诉你我在哪里得到了错误。我将不胜感激有关此事的任何帮助。

#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 */


}
4

3 回答 3

1

printf()格式说明符%s需要一个参数 .character array但在这里你需要使用格式说明符 topinteger%d

全部更改

 printf("%s\n", top);  ==> printf("%d\n", top);
          ^                         ^ 
于 2013-09-10T15:40:18.390 回答
1

你的 push 和 pop 操作需要完全相反。

所以如果 push 是

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

流行一定是

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

否则,pop 将始终从最近推送的值之后的一个插槽返回值。


调试使用命令行参数的程序与调试任何其他程序应该没有太大区别。但是您可能需要阅读调试器的文档以了解如何将参数传递给程序。在 gdb 中:

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

也就是说,将参数添加到该run行,就好像run程序的名称一样。

于 2013-09-10T16:09:10.923 回答
0

检查所有 printf 以确保格式说明符与参数匹配。

于 2013-09-10T15:43:51.557 回答