-3

誰かが以下の私のコードを見て、私を助けてくれますか? 何時間もこれを修正しようとしてきましたが、何が問題なのかわかりません。これは、スタック計算機の操作を取り、数式のオペランドを格納することになっている C で書かれたプログラムです。演算が実行されると、スタックの最後の 2 つの値が削除されてオペランドとして使用され、演算の結果がスタックに置かれます。ただし、正しい数値が得られません。私のコードを見てください。長いと思いますがよろしくお願いします。ありがとう。

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#define SIZE 10
#define MAXINPUT 255


void printStack(int stack[], int tos)
{
if (isEmpty(tos))
{
    printf("Stack is empty\n");
    printf("---------------------------------------\n");
    return;
}   

printf("Stack: ");
while (tos < SIZE)
{
    printf("[%d] " , stack[tos]);
    tos++;

}   
printf("\n---------------------------------------\n");

}   


int top (int stack[], int tos)
{
if(isEmpty(tos))
    return;
return stack [tos];
}

int isEmpty(int tos)
{

if (tos < 0)
    return 1;
}

int isFull (int tos)
{

if(tos >= SIZE - 1)
    return 1;

}   

void push(int val, int stack [], int *tos)
{
if(isFull(*tos))
    return;
(*tos)++;
stack[*tos] = val;


}

int pop (int stack [], int *tos)
{

if(isEmpty(*tos))
    return;
int val = stack[*tos];
(*tos)--;
return val;
}

void clear(int *tos)
{
*tos = -1;

}   

int getInput (char *input)
{

printf("+------------------------------{Choose an option}------------------------------+\n");
printf("| (q) : quit the program.                                                      |\n"
       "| (integer value) : an integer value (either positive or negative) to push     |\n"
       "| (c) : clear the stack                                                        |\n"
       "| (=) : display top value on the stack                                         |\n"
       "| (+) : addition                                                               |\n"
       "| (-) : subtraction                                                            |\n"
       "| (*) : multiplication                                                         |\n"
       "| (/) : division - integer division only                                       |\n"
       "| (%) : modulus - remainder from an integer division                           |\n"
       "| (^) : exponentiation (x raised to the power of y)                            |\n"
       "+------------------------------------------------------------------------------+\n");
printf("Input: ");
gets(input);
if(strcmp(input, "q") == 0)
{
    printf("Exiting...\n");
    return 0;
}
return 1;
}   

int isNum(char *input)
{
int i;
for(i = 0; i < strlen(input); i++)
{
    if(!isdigit(input[i]))
        return 0;
}   
return 1;

}   

int hasTwo(tos)
{
if((SIZE - tos) >= 2)
    return 1;

printf("\nStack size is 1, must have 2 or more\n");
return 0;
}
void mathOp (char op, int stack[], int *tos)
{
if(!isEmpty(*tos))
    return;
if(!hasTwo(*tos))
    return;

int right = pop(stack, tos);
int left = pop(stack, tos); 
switch(op)
{
    case '+': 
        push((left + right), stack, tos);
        break;
    case '-': 
        push((left - right), stack, tos);
        break;
    case '*': 
        push((left * right), stack, tos);
        break;
    case '/': 
        push((left/right), stack, tos);
        break;
    case '%': 
        push((left % right), stack, tos);
        break;
    case '^': 
        push(pow(left, right), stack, tos);
        break;
}       

}   

int main(int argc, char **argv)
{
int verbose = 0;
int debugMode = 0;
if (argc == 2 && argv[1][0] == '-' && argv[1][1] == 'd')
{   
    debugMode = 1;
    if (strcmp("-dv", argv[1]) == 0)
    {
        verbose = 1;
    }
}

int stack[SIZE];
int tos = -1;
char input[MAXINPUT];
while (getInput(input))
{
    int result = 0;
    if (strcmp(input, "c") == 0)
        clear(&tos);
    else if (strcmp(input, "=") == 0)
    {

        result = top(stack, tos);
        printf("Top of Stack is [%d]\n", result);
    }
    else if (isNum(input))
        push(atoi(input), stack, &tos);
    else if(strcmp(input, "+") == 0 ||
            strcmp(input, "-") == 0 ||
            strcmp(input, "*") == 0 ||
            strcmp(input, "/") == 0 ||          
            strcmp(input, "%") == 0 ||          
            strcmp(input, "^") == 0 ) mathOp(input[0], stack, &tos);
    else
        printf("Invalid input\n");

    if (debugMode)
        printStack(stack, tos);     
}

return 0;
}
4

1 に答える 1

2

このコードには多くの問題があります。-Wall(または同等の設定)でコンパイルしてisEmpty、andisFulltopandpopが (常に) 適切に値を返さないことを確認します。

何かを返す必要があるすべての関数は、return ステートメントで終了する必要があります。C にはある種の「デフォルトの戻り値」がありません。

例として:

int isFull (int tos)
{
    if(tos >= SIZE - 1)
        return 1;

    return 0; // <-- not full, you probably want to return 0
}

ps。ヘルプ テキストで%%リテラルを使用する必要があります。%

編集してすべてを修正します。

  1. printStack0from totosではなく from tostoをループする必要がありますSIZE

  2. hasTwoかどうかをテストする必要がありますtos>=1

  3. mathOp最初にテストする必要があります。「空でない場合」と書かれているをif(isEmpty(*tos))削除します。!

その後、動作するはずです。

于 2012-12-28T22:15:53.313 に答える