我怎样才能打印此stackT的价值? [英] How can I print the value in this stackT?
问题描述
我发现了一些code键使C实现栈,并决定使用它。但是,有几个类型定义,和我有困难的stackT(真是一个字符数组)打印值。下面是code。我在做什么错了?
#包括LT&;&stdio.h中GT;
#包括LT&;&stdlib.h中GT;字符的typedef stackElementT;typedef结构{
stackElementT *内容;
INT MAXSIZE;
INT榜首;
} stackT;无效StackInit(stackT * stackP,诠释MAXSIZE){
stackElementT * newContents;
newContents =(stackElementT *)malloc的(的sizeof(stackElementT)* MAXSIZE);
如果(newContents == NULL){
fprintf中(标准错误,"没有足够的内存\\ n");
出口(1);
} stackP->内容= newContents;
stackP-> MAXSIZE = MAXSIZE;
stackP->顶= -1; //空...
}无效StackDestroy(stackT * stackP){
免费(stackP->的内容);
stackP->内容= NULL;
stackP-> MAXSIZE = 0;
stackP->顶= -1; //空
}INT StackIsEmpty(stackT * stackP){
返回stackP->顶部和LT; 0;
}INT StackIsFull(stackT * stackP){
返回stackP->顶> = stackP-> MAXSIZE-1;
}无效StackPush(stackT * stackP,stackElementT元素){
如果(StackIsFull(stackP)){
fprintf中(标准错误,"不能推动因素:堆栈满\\ n"。);
出口(1);
}
stackP->的内容[++ stackP->顶] =元素;
}stackElementT StackPop(stackT * stackP){
如果(StackIsEmpty(stackP)){
fprintf中(标准错误,"不能流行元素:堆栈是空\\ n"。);
出口(1);
}
返回stackP->的内容[stackP-> top--]。
}无效StackDisplay(stackT * stackP){
如果(StackIsEmpty(stackP)){
fprintf中(标准错误,"不能显示:堆栈是空\\ n");
出口(1);
}
INT I;
的printf(QUOT; [&QUOT);
对于(i = 0; I< stackP->顶;我++){
的printf(QUOT; C,&QUOT ;, stackP [I]); //发生问题的位置
}
的printf(QUOT;%C] QUOT ;, stackP [stackP->顶]);
}INT后缀(字符* EXPR,INT长度){
INT I;
stackT堆栈;
StackInit(安培;叠加,1000);
INT温度;
对于(i = 0; I<长度;我++){
如果((表达式[Ⅰ]≥= 48)及及(expr的[1] - = 57)){
的printf(QUOT;是一个数字推%d个\\ n&QUOT ;, EXPR [I]!);
StackPush(安培;栈,EXPR [I]);
}
其他{
开关(表达式[I]){
案例43:{
TEMP = StackPop(安培;堆栈);
StackPush(安培;叠加,StackPop(安培;堆栈)+温度);
}
打破;
案例45:{
TEMP = StackPop(安培;堆栈);
StackPush(安培;叠加,StackPop(安培;堆栈)-temp);
}
打破;
案例47:{
TEMP = StackPop(安培;堆栈);
StackPush(安培;叠加,StackPop(安培;堆栈)/温度);
}
打破;
案例42:{
TEMP = StackPop(安培;堆栈);
StackPush(安培;叠加,StackPop(安培;堆栈)*温度);
}
打破;
默认:
打破;
}
}
}
返回StackPop(安培;堆栈);
}诠释主(){
INT I;
字符* EXPR =" 1 2 3 + * 3 2 1 - + *英寸;
对于(i = 0;!EXPR [I] ='\\ 0';我++);
的printf(QUOT;%d个\\ n&QUOT ;,后缀(表达式,I));
}
编译器(在MacOS X的10.6.7 GCC 4.2.1)告诉我:
$ CC -O -std = C99 -Wall -Wextra st.c -o ST
st.c:在函数'StackDisplay:
st.c:72:警告:格式'%C'型预期'诠释',但参数2的类型stackT
st.c:74:警告:格式'%C'型预期'诠释',但参数2的类型stackT
$
在我的版本的code的,这两条线是的printf()
在 StackDisplay语句()
,
就在你说明你有问题。
无效StackDisplay(stackT * stackP)
{
如果(StackIsEmpty(stackP)){
fprintf中(标准错误,无法显示:堆栈是空\\ n);
出口(1);
}
INT I;
的printf([);
对于(i = 0; I< stackP->顶;我++){
的printf(%C,stackP [I]); //发生问题的位置
}
的printf(%C],stackP [stackP->顶]);
}
您可能希望 stackP->的内容[I]
。随着该修补程序,该程序的运行,但会产生:
无法流行元素:堆栈是空的。
这是你的问题要解决,现在。
(呵呵,我也定了为
循环之后的流浪分号在的main()
作为诊断意见。)
循环应该写成的strlen(表达式)
(然后你需要的#include<文件string.h>
)。的确,主程序的主体简化为:
的char * EXPR =1 2 3 + * 3 2 1 - + *;
的printf(%d个\\ N,后缀(表达式,strlen的(表达式)));
您通常应保持顶
索引到使用的下一个位置,因此初始值通常会 0
而不是 1
。
不学ASCII codeS的数字 - 忘记你做过
。 IF((表达式[I]> = 48)及和放大器;(表达式[1] - = 57)){
您应该写:
IF((表达式[1] - GT ='0')及&安培;(表达式[1] - ='9')){
,或者更好的(但你必须的#include<&文件ctype.h GT;
太):
如果(ISDIGIT(表达式[I])){
同样的评论意见适用于开关:
开关(表达式[I]){
案例43:{
TEMP = StackPop(安培;堆栈);
StackPush(安培;叠加,StackPop(安培;堆栈)+温度);
}
打破;
我不知道缩进背后的逻辑,但43应该写成+
45为' - '
47为/
和42 '*'
。
这会产生:
是一个数字!推49
是一个数字!推50
是一个数字!推51
是一个数字!推51
是一个数字!推50
是一个数字!推49
68
如果您修复的数量推code,如下所示:
的printf(是一个数字推%d个\\ N!,expr的[I] - '0');
StackPush(安培;栈,EXPR [I] - '0');
然后你得到:
是一个数字!推1
是一个数字! 2推
是一个数字! 3推
是一个数字! 3推
是一个数字! 2推
是一个数字!推1
20
和多一点的仪器,沿着线:
TEMP = StackPop(安培;堆栈);
的printf(子:结果%d个\\ N,温度);
StackPush(安培;堆,温度);
每次操作后,其结果是:
是一个数字!推1
是一个数字! 2推
是一个数字! 3推
地址:导致5
MUL:5的结果
是一个数字! 3推
是一个数字! 2推
是一个数字!推1
子:1的结果
地址:导致4
MUL:导致20
20
您已经接近。
I found some code to make a C implementation of stacks, and decided to use it. However, there were several typedefs, and I am having difficulty printing the values in a stackT (really a char array). Below is the code. What am I doing wrong?
#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 %d\n", 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] != '\0'; i++) ; printf("%d\n", postfix(expr, i)); }
The compiler (GCC 4.2.1 on MacOS X 10.6.7) tells me:
$ 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’
$
In my version of the code, these two lines are the printf()
statements in StackDisplay()
,
right where you state you have problems.
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]);
}
You probably want stackP->contents[i]
. With that fix, the program 'runs' but produces:
Can't pop element: stack is empty.
That is your problem to resolve, now.
(Oh, I also fixed the stray semi-colon after the for
loop in main()
as diagnosed in the comments.)
The loop should be written as strlen(expr)
(and then you need to #include <string.h>
). Indeed, the body of the main program simplifies to:
char* expr = "1 2 3 + * 3 2 1 - + *";
printf("%d\n", postfix(expr, strlen(expr)));
You should normally keep top
indexed to the next location to use, so the initial value would normally be 0
rather than -1
.
Don't learn the ASCII codes for the digits - forget you ever did.
if ((expr[i] >= 48) && (expr[i] <= 57)) {
You should write:
if ((expr[i] >= '0') && (expr[i] <= '9')) {
or, better (but you have to #include <ctype.h>
too):
if (isdigit(expr[i])) {
Similar comments apply to the switch:
switch (expr[i]) {
case 43: {
temp = StackPop(&stack);
StackPush(&stack, StackPop(&stack)+temp);
}
break;
I'm not sure of the logic behind the indentation, but that 43 should be written as '+'
, 45 as '-'
, 47 as '/'
, and 42 as'*'
.
This generates:
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
If you fix the number pushing code as shown:
printf("Is a number! Pushed %d\n", expr[i] - '0');
StackPush(&stack, expr[i] - '0');
Then you get:
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
And with a little more instrumentation, along the lines of:
temp = StackPop(&stack);
printf("Sub: result %d\n", temp);
StackPush(&stack, temp);
after each operation, the result is:
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
You were close.
这篇关于我怎样才能打印此stackT的价值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!