2013-10-23 91 views
0

請在下面找到我的函數C.我在那裏使用一個堆棧,它是另一個文件的一部分,但它工作正常。error:expected')'before';'或'}'token

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

我不知道你在用什麼來編輯你的代碼,但在這種情況下,語法突出顯示是一個相當不錯的指示,說明哪裏出了問題。並且不要跳過編譯器錯誤中的項目。您關注的是預期的令牌錯誤,其中第一個實際錯誤(以及之前的警告)實際上是問題的更好指標。 – Bart

+0

甚至StackOverflow上的語法突出顯示也會捕獲它...... – wildplasser

回答

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] == '/''/'是錯字或不是,但如果你需要反斜槓而不是'/',你將再次使用這個'\\'

+4

實際上'(c ==('*'||'\\'))'應該是'(c =='*'|| c =='\ \')' –

+2

正確's-> arr [s-> top] ==('+'||' - ')'也是 –

+1

即使是大括號也是一個問題。 ;) – Sadique

相關問題