2012-12-28 68 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,这是不正确的,但它“有点”起作用。 –