Re: [問題] 問題出在哪??compile不過..
※ 引述《hck13kimo (清心寡愈)》之銘言:
: #include <stdio.h>
: #include <stdlib.h>
: #define Stacksize 80
: #define TRUE 1
: #define FALSE 0
: int input_infix(char); // 中序轉後序
: char priority(int); // 決定運算子優先順序
: void evalute_subvalue(int); //計算程式
: void Push_operand(struct stack_operand int*, int);//放值進運算元堆疊
^^^^^^^^^^^^^^^^^^^^ ^^^^
在這一行我還不認識struct stack_operand
且你用了內建型別int當作變數名稱
: void Push_operator(struct stack_operator char*, char);//放值進運算子堆疊
^^^^^^^^^^^^^^^^^^^^^ ^^^^
同上
: int Pop_operand(struct stack_operand int*);//拿運算元出堆疊
^^^^^^^^^^^^^^^^^^^^ ^^^^
同上
: char Pop_operator(struct stack_operator char*);//拿運算元出堆疊
^^^^^^^^^^^^^^^^^^^^^ ^^^^^
同上
: ////////////////////////////////////
: /* 運算元堆疊宣告 */
: ////////////////////////////////////
: struct stack_operand
: {
: int top;
: int stack[Stacksize];
: };
: ////////////////////////////////////
: /* 運算子堆疊宣告 */
: ////////////////////////////////////
: struct stack_operator {
: char top;
: char stack[Stacksize];
: };
: ////////////////////////////////////
: /* 主程式 */
: ////////////////////////////////////
: int main(void)
: {
: int input[Stacksize];
^^^
: printf("輸入運算式:");
: scanf("%s", input);
這裡接收字串,可能會使int input任何一個int存有1個(包含)以上的char
: input_fix(input);
^^^^^^
input_fix期待接收char
input不是一個char型別
無法從int[80]轉換成char
由int*轉為char一定會遭受截斷
: system("pause");
: return 0;
: }
: ////////////////////////////////////
: /* 中序轉後序 */
: ////////////////////////////////////
: int input_infix(char* infix)
^^^^^^
input_infix函式原型不一致
input_infix原先的宣告 int input_infix(char)
: {
: int i=0;
: char symb;
: int op;
: struct stack_operand ope_rand;
: struct stack_operator oper_ator;
: ope_rand.top = -1 ;
: oper_ator.top = -1 ;
: while(!'\0')
'\0' == 0
!0 恆等於1
這不是語法錯誤,但一定跟你表達的有差
: {
: symb = infix[i];
: if (symb = '+'||'-'||'*'||'/'){
^^^^^^^^^^^^^^^^^^^^^^^^^^
symb = ('+' || '-' || '*' || */')
symb得到的是1,不過這裡不會有語法錯誤,只是一定跟你要表達的有差
: Push_operator(*oper_ator.top, symb);
^^
對於一個struct stack_operator.top
(char)
沒有dereference操作
: }
: else
: {
: if (symb == ')')
: {
: while((op = Pop_operator(*oper_ator.top))!='(')
: {
: evalute_subvalue(op);
: continue;
: }
: }
: else
: {
: while (priority(symb) <= priority(oper_ator.top))
: {
: op = Pop_operand(*ope_rand.top);
^^
對於一個struct stack_operand ope_rand.top
(int)
沒有dereference操作
: evalute_subvalur(op);
: }
: Push_operator(*oper_ator.top, symb);
^^
同struct stack_operator.top問題
: while()
^^
必須是一個boolean表示式
: {
: op = Pop_operand(*ope_rand.top);
^^^^^^^^^^
同struct stack_operator.top問題
: evalute_subvalue(op);
: }
: printf("ans:%c",*ope_rand->stack[0]);
ope_rand沒有operator->
(ope_rand不是一個指向結構體的指標,stack未宣告)
假使ope_rand是一個指標
stack[ 0 ]是一個int
沒有dereference操作
: }
: ////////////////////////////////////
: /* 判斷運算元優先順序 */
: ////////////////////////////////////
: int priority(char symb)
^^^^^^^^^^^^^^^^^^^^^^
函式定義不一致
函式priority原型為
char priority(int)
: {
: int p;
: switch(op) {
^^^^^
op第一次在這個函式出現,未知的符號
: case '+': case '-':
: p = 1;
: break;
: case '*': case '/':
: p = 2;
: break;
: default:
: p = 0;
: break;
: }
: return p;
: }
: ////////////////////////////////////
: /* 計算函式 */
: ////////////////////////////////////
: void evalute_subvalue(int op)
: {
: int a,b;
: int op, new_value;
: b = int Pop(*ope_rand.top);
^^^^
ope_rand在這個函式沒有定義及宣告
: a = int Pop(*ope_rand.top);
^^^^
同上
: switch(op)
: {
: case '+':
: return (new_value = a + b);
^^^^^^^^^^^^^^^^^^^^^^^^^^
你在void函式return了int值,猜測你的evalute_subvalue(int op)
是不是int evalute_subvalue(int op)
: case '-':
: return (new_value = a - b);
^^^^^^^^^^^^^^^^^^^^^^^^^^
同上
: case '*':
: return (new_value = a * b);
^^^^^^^^^^^^^^^^^^^^^^^^^^
同上
: case '/':
: return (new_value = a / b);
^^^^^^^^^^^^^^^^^^^^^^^^^^
同上
: }
: Push(*ope_rand.top , new_value);
^^^^^^^^^^^^^^
未知的符號 ope_rand
: }
: ////////////////////////////////////
: /* PUSH */
: ////////////////////////////////////
: void Push_operand(struct stack_operand *ps , int symb)
: {
: if(ps.top >= Stacksize - 1)
: printf("The stack is full \n");
: retun FALSE
^^^^ ^^^^^
retun不是一個型別(compiler可能會這麼說)
期待在else前找到一個分號
: else
^^^^^
else找不到匹配的if (你少了個大括號)
: ps->stack[++(ps->top)] = symb;
: return;
: }
: void Push_operator(struct stack_operator *ps , char symb)
: {
: if(ps.top >= Stacksize - 1)
: printf("The stack is full \n");
: retun FALSE;
^^^^^
retun不是一個型別
: else
^^^^
else找不到匹配的if
: ps->stack[++(ps->top)] = symb;
: return;
: }
: ////////////////////////////////////
: /* POP */
: ////////////////////////////////////
: int Pop_operand(struct stack_operand *ps)
: {
: if(ps.top < 0)
: printf("The stack is empty \n");
: retun FALSE;
^^^^^
請不要複製貼上 retun不是一個型別
: else
^^^^
else找不到匹配的if
: return(ps->stack[ps->top --]);
: }
: int Pop_operator(struct stack_operator *ps)
: {
: if(ps.top < 0)
: printf("The stack is empty \n");
: retun FALSE;
^^^^^
retun不是一個型別
: else
^^^^
else 找不到匹配的if
: return(ps->stack[ps->top --]);
: }
: 小弟這個程式一直沒辦法compile...
: 可是我找不到錯誤到底在哪...
: 是語法有錯嗎??
: 還是其他的問題呢??
兩者都有
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 61.227.127.39
※ 編輯: sunneo 來自: 61.227.127.39 (04/06 16:03)
※ 編輯: sunneo 來自: 61.227.127.39 (04/06 16:19)
推
04/06 16:19, , 1F
04/06 16:19, 1F
※ 編輯: sunneo 來自: 61.227.127.39 (04/06 16:22)
→
04/06 17:04, , 2F
04/06 17:04, 2F
→
04/06 17:24, , 3F
04/06 17:24, 3F
→
04/06 17:24, , 4F
04/06 17:24, 4F
→
04/06 17:26, , 5F
04/06 17:26, 5F
→
04/06 17:26, , 6F
04/06 17:26, 6F
→
04/06 17:26, , 7F
04/06 17:26, 7F
→
04/06 17:27, , 8F
04/06 17:27, 8F
→
04/06 17:29, , 9F
04/06 17:29, 9F
討論串 (同標題文章)
完整討論串 (本文為第 2 之 2 篇):