2

Cを使用して後置記法で計算を実行し、UNIXコマンドラインから値を読み取るプログラムを作成しようとしています。ただし、私は C 言語にまったく慣れていないため、正しく動作させるのに少し苦労しています。NULL およびセグメンテーション フォールトの読み取り値を取得していますが、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ここでtopはフォーマット指定子integerを使用する必要があり%dます。

すべて変更

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

プッシュ操作とポップ操作は、互いにまったく逆にする必要があります。

したがって、プッシュが

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

ポップでなければならない

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

それ以外の場合、pop は常に、最後にプッシュされた値のの1 つのスロットから値を返します。


コマンドライン引数を使用するプログラムのデバッグは、他のプログラムのデバッグと大差ありません。ただし、デバッガーのドキュメントを読んで、引数をプログラムに渡す方法を学ぶ必要がある場合があります。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 に答える