2012-12-28 67 views
-2

有人可以看看我的代碼,並幫助我。我一直試圖解決這個問題,但我無法弄清楚什麼是錯的。這是一個用C語言編寫的應用程序,用於處理堆棧計算器的操作並存儲數學表達式的操作數。當執行操作時,堆棧中的最後兩個值將被刪除並用作操作數,然後將操作結果放在堆棧上。但是,我沒有得到正確的數字。請看看我的代碼。我知道這很長,但我很感激。謝謝。C程序,堆棧計算器

#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; 
} 
+3

給我們更多的上下文。你在幹什麼?你要出去什麼?你在期待什麼?這將有助於縮小範圍。 – Grambot

+0

+1 @ TheCapn。此外,您可能想嘗試調試器 - 您可能會在短時間內發現您的問題。 –

+0

我只是想讓用戶輸入兩個整數。當用戶輸入第一個整​​數時,它會被壓入堆棧,然後當用戶輸入下一個整數時,它也會被壓入堆棧。然後,當用戶選擇一個操作時,彈出最上面兩個值,用這兩個值執行操作,然後將結果壓入堆棧。操作數的順序很重要。從堆棧中取出的第一個值是右側的操作數,而從堆棧中取出的第二個值是左側的操作數。 –

回答

2

你在這段代碼中有很多問題。編譯-Wall(或同等設置)以發現isEmptyisFulltoppop不(總是)正確地返回一個值。

每個需要返回內容的函數都必須以return語句結束。有一些種類的C.

所以沒有 '默認返回值' 爲例:

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

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

PS。您需要在幫助文本中使用%%作爲文字%

編輯解決一切:

  1. printStack被嚴重打破,你需要循環從0tos,而不是從tosSIZE

  2. hasTwo需要測試tos>=1

  3. mathOp需要先測試if(isEmpty(*tos)),刪除!它說不是「如果不是空的」。

然後它應該工作。

+0

但頂部和彈出函數確實以返回語句 –

+1

結束,但它們不返回任何內容,而您指定它們返回'int'。編譯器警告這一點。如果你簡單地說'return;' – mvds

+0

mvds謝謝你的幫助,它不會返回'0'。回覆晚了非常抱歉。但我仍然試圖獲得C編程的竅門。我有幾個問題。我還有一些問題。我試着做你對printStack函數所說的話,並從0循環到tos而不是tos到SIZE。但是,當我這樣做時,我會遇到分段錯誤。當我以另一種方式離開它時,從tos到SIZE,這是不正確的,但它「有點」起作用。 –