C 順序棧實現字尾表示式計算

2021-10-22 07:37:15 字數 1277 閱讀 1170

題目:

使用棧實現字尾表示式計算

要求:

使用棧實現字尾表示式計算,其中,在字尾表示式中,輸入的數字為整數,且為正數,數字、符號之間用逗號隔開,整個字尾表示式用「#」表示結束。

輸入樣例:

11 2 3 + *#

輸出樣例:

55

//順序棧實現字尾表示式計算

//author:mitchell

#includeusing namespace std;

class stack

stack()

~stack()

bool isempty();

bool push(const double pushvalue);

bool pop(double& popvalue);

};bool stack::isempty()

else }

bool stack::push(const double pushvalue)

st[++top] = pushvalue;

return true;

}bool stack::pop(double& popvalue)

popvalue = st[top--];

return true;

}class calculator

;void calculator::run()

switch (instream)

}double finalnumber;

if (data.pop(finalnumber)) }

void calculator::calculate(char symbolvalue)

case'-':

case'*':

case'/':

else

}} }

else }

void calculator::clear()

bool calculator::get(double& a, double& b)

data.pop(a);

if (data.isempty())

data.pop(b);

return true;

}int main()

計算表示式值(字尾表示式) 棧

處理表示式主要是對優先順序以及括號的判斷 1.運算子棧頂的優先順序小於加入的時,需要將所有的不評級的取出並計算,2.當遇到 時,需要括號內的運算全部處理 2.其他情況全部加入運算子棧和資料棧 include include include using namespace std typedef lo...

棧與字尾表示式C實現

1 include2 include3 4 typedef char datatype 5 typedef struct stack 6stack 1112 初始化空棧 13 void initstack stack st,int sz 14 19 釋放站空間 20 void freestack s...

利用棧計算字尾表示式

字尾表示式又稱作逆波蘭式,操作符在運算數的後面,所以叫做字尾表示式。例如 1 2 3的字尾表示式是 1 2 3 可以 2 3 看作乙個運算數a,1 a 也符合運算數 運算數 操作符的格式。的特殊處理 因為要處理大於9的數,所以要在每個數和操作符後加上空格,將相鄰的數分隔開。字尾表示式以字串的形式存在...