请在下面找到我在C中的功能。我在那里使用堆栈操作,这是另一个文件的一部分,但这是正常的。
void doOperation ( tStack* s, char c, char* postExpr, unsigned* postLen ) {
if ( ( c == ( '*' || '\' ) ) && ( s->arr[s->top] == ( '+' || '-' ) ) )
stackPush( s, c);
else if ( c == ( '+' || '-' ) && s->arr[s->top] == ( '*' || '/' ) ) {
stackTop( s, postExpr[postLen] );
*(postLen)++;
stackPop( s );
stackPush( s, c);
}
else if ( c == '(' )
stackPush( s, c);
else if ( c == ')' )
untilLeftPar( s, postExpr, postLen);
else {
stackTop( s, postExpr[postLen] );
*(postLen)++;
stackPop( s );
stackPush( s, c);
}
}
我收到这些错误,我不知道出了什么问题:
c204.c:70:23: warning: character constant too long for its type [enabled by default]
c204.c:70:58: warning: multi-character character constant [-Wmultichar]
c204.c:70:65: warning: missing terminating ' character [enabled by default]
c204.c:70:2: error: missing terminating ' character
c204.c:71:3: error: void value not ignored as it ought to be
c204.c:71:19: error: expected ‘)’ before ‘;’ token
c204.c:88:1: error: expected ‘)’ before ‘}’ token
c204.c:88:1: error: expected ‘)’ before ‘}’ token
c204.c:88:1: error: expected expression before ‘}’ token
../c202/c202.c: In function ‘stackTop’:
../c202/c202.c:100:18: warning: the comparison will always
evaluate as ‘true’ for the address of ‘stackEmpty’ will never be NULL [-Waddress]
../c202/c202.c: In function ‘stackPop’:
../c202/c202.c:120:18: warning: the comparison will always
evaluate as ‘true’ for the address of ‘stackEmpty’ will never be NULL [-Waddress]
../c202/c202.c: In function ‘stackPush’:
../c202/c202.c:133:17: warning: the comparison will always
evaluate as ‘false’ for the address of ‘stackFull’ will never be NULL [-Waddress]
make: *** [c204-test] Error 1
这些错误的原因可能是什么?
答案 0 :(得分:10)
您需要阅读有关转义序列的内容,此'\'
是行中的问题:
if ( ( c == ( '*' || '\' ) ) && ( s->arr[s->top] == ( '+' || '-' ) ) )
stackPush( s, c);
将其替换为'\\'
:
if ( ( c == '*' || c == '\\' ) && ( ( s->arr[s->top] == '+' || s->arr[s->top] == '-' ) ) )
stackPush( s, c);
\\
适用于反斜杠。更多read this answer。此条件也不应写为( c == ( '*' || '\\' ) )
- 它应为c == '*' || c == '\\'
else if
部分也有些混乱,应该是:
else if ( ( c == '+' || c == '-' ) && ( s->arr[s->top] == '*' || s->arr[s->top] == '/' ) )
另外还不确定s->arr[s->top] == '/'
'/'
是否是拼写错误,但如果你需要反斜杠而不是'/'
,你将再次使用'\\'