我怎样才能打印这个堆栈中的值?
我发现了一些代码来实现堆栈的C实现,并决定使用它。 然而,有几个类型定义,并且我很难在一个stackT中打印这些值(实际上是一个char数组)。 以下是代码。 我究竟做错了什么?
#include <stdio.h> #include <stdlib.h> typedef char stackElementT; typedef struct { stackElementT *contents; int maxSize; int top; } stackT; void StackInit(stackT *stackP, int maxSize) { stackElementT *newContents; newContents = (stackElementT *)malloc(sizeof(stackElementT)*maxSize); if (newContents == NULL) { fprintf(stderr, "Not enough memory.n"); exit(1); } stackP->contents = newContents; stackP->maxSize = maxSize; stackP->top = -1; //empty... } void StackDestroy(stackT *stackP) { free(stackP->contents); stackP->contents = NULL; stackP->maxSize = 0; stackP->top = -1; //empty } int StackIsEmpty(stackT *stackP) { return stackP->top < 0; } int StackIsFull(stackT *stackP) { return stackP->top >= stackP->maxSize-1; } void StackPush(stackT *stackP, stackElementT element) { if(StackIsFull(stackP)) { fprintf(stderr, "Can't push element: stack is full.n"); exit(1); } stackP->contents[++stackP->top] = element; } stackElementT StackPop(stackT *stackP) { if(StackIsEmpty(stackP)) { fprintf(stderr, "Can't pop element: stack is empty.n"); exit(1); } return stackP->contents[stackP->top--]; } void StackDisplay(stackT *stackP) { if(StackIsEmpty(stackP)) { fprintf(stderr, "Can't display: stack is empty.n"); exit(1); } int i; printf("[ "); for (i = 0; i < stackP->top; i++) { printf("%c, ", stackP[i]); //the problem occurs HERE } printf("%c ]", stackP[stackP->top]); } int postfix(char* expr, int length) { int i; stackT stack; StackInit(&stack, 1000); int temp; for (i = 0; i < length; i++) { if ((expr[i] >= 48) && (expr[i] <= 57)) { printf("Is a number! Pushed %dn", expr[i]); StackPush(&stack, expr[i]); } else { switch (expr[i]) { case 43: { temp = StackPop(&stack); StackPush(&stack, StackPop(&stack)+temp); } break; case 45: { temp = StackPop(&stack); StackPush(&stack, StackPop(&stack)-temp); } break; case 47: { temp = StackPop(&stack); StackPush(&stack, StackPop(&stack)/temp); } break; case 42: { temp = StackPop(&stack); StackPush(&stack, StackPop(&stack)*temp); } break; default: break; } } } return StackPop(&stack); } int main() { int i; char* expr = "1 2 3 + * 3 2 1 - + *"; for(i = 0; expr[i] != ' '; i++) ; printf("%dn", postfix(expr, i)); }
编译器(Mac OS X 10.6.7上的GCC 4.2.1)告诉我:
$ cc -O -std=c99 -Wall -Wextra st.c -o st
st.c: In function ‘StackDisplay’:
st.c:72: warning: format ‘%c’ expects type ‘int’, but argument 2 has type ‘stackT’
st.c:74: warning: format ‘%c’ expects type ‘int’, but argument 2 has type ‘stackT’
$
在我的代码版本中,这两行是StackDisplay()
中的printf()
语句,在这里你声明你有问题。
void StackDisplay(stackT *stackP)
{
if(StackIsEmpty(stackP)) {
fprintf(stderr, "Can't display: stack is empty.n");
exit(1);
}
int i;
printf("[ ");
for (i = 0; i < stackP->top; i++) {
printf("%c, ", stackP[i]); //the problem occurs HERE
}
printf("%c ]", stackP[stackP->top]);
}
你可能想要stackP->contents[i]
。 通过该修复程序,该程序“运行”,但生成:
Can't pop element: stack is empty.
这是你现在要解决的问题。
(哦,我也在注释中诊断出的main()
的for
循环之后修正了散列分号。)
循环应该写成strlen(expr)
(然后你需要#include <string.h>
)。 事实上,主程序的主体简化为:
char* expr = "1 2 3 + * 3 2 1 - + *";
printf("%dn", postfix(expr, strlen(expr)));
您通常应将top
索引保留到下一个要使用的位置,因此初始值通常为0
而不是-1
。
不要学习数字的ASCII码 - 忘记你曾经做过。
if ((expr[i] >= 48) && (expr[i] <= 57)) {
你应该写:
if ((expr[i] >= '0') && (expr[i] <= '9')) {
或者更好(但你也必须#include <ctype.h>
):
if (isdigit(expr[i])) {
类似的评论适用于交换机:
switch (expr[i]) {
case 43: {
temp = StackPop(&stack);
StackPush(&stack, StackPop(&stack)+temp);
}
break;
我不确定缩进背后的逻辑,但43应该写成'+'
,45写成'-'
,47写为'/'
,42写为'*'
。
这会产生:
Is a number! Pushed 49
Is a number! Pushed 50
Is a number! Pushed 51
Is a number! Pushed 51
Is a number! Pushed 50
Is a number! Pushed 49
68
如果您修复了数字推送代码,如下所示:
printf("Is a number! Pushed %dn", expr[i] - '0');
StackPush(&stack, expr[i] - '0');
然后你会得到:
Is a number! Pushed 1
Is a number! Pushed 2
Is a number! Pushed 3
Is a number! Pushed 3
Is a number! Pushed 2
Is a number! Pushed 1
20
多用一些仪器,包括以下几点:
temp = StackPop(&stack);
printf("Sub: result %dn", temp);
StackPush(&stack, temp);
每次操作后,结果是:
Is a number! Pushed 1
Is a number! Pushed 2
Is a number! Pushed 3
Add: result 5
Mul: result 5
Is a number! Pushed 3
Is a number! Pushed 2
Is a number! Pushed 1
Sub: result 1
Add: result 4
Mul: result 20
20
你很近。
链接地址: http://www.djcxy.com/p/61337.html