【发布时间】:2012-12-14 03:04:32
【问题描述】:
有人可以看看我下面的代码并帮助我。我已经尝试解决这个问题好几个小时了,但我不知道出了什么问题。这是一个用 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;
}
【问题讨论】:
-
给我们更多的背景信息。你在里面放什么?你出去做什么?你在期待什么?这将有助于缩小范围。
-
+1 @TheCapn。此外,您可能想尝试调试器 - 您可能很快就会发现问题。
-
我只是想让用户输入两个整数。当用户输入第一个整数时,它被压入堆栈,然后当用户输入下一个整数时,它也被压入堆栈。然后,当用户选择一个操作时,前两个值被弹出,对这两个值执行一个操作,然后将结果压入堆栈。操作数的顺序很重要。从堆栈中取出的第一个值是右侧操作数,而从堆栈中取出的第二个值是左侧操作数。
-
不要使用
gets()——它太危险了。忘记它的存在。假装它会导致你的机器融化成一滩熔融的硅和金属(以及碳化塑料)。根本没有办法可靠地使用它。
标签: c stack calculator