中綴表示式轉化為字尾表示式,並計算字尾表示式

2021-07-06 02:14:22 字數 1295 閱讀 3496

//標頭檔案

//該函式中用了棧的特點,所以增加了棧的標頭檔案,下面是棧的標頭檔案

#ifndef stack_h

#define stack_h

template

class stack

t pop();

};template

stack

::stack()

template

bool stack

::empty() template

t stack

::gettop() return data[top - 1]; } template

void stack

::push(const t value) data[top++] = value; } template

t stack

::pop() t value = data[top - 1]; top--; return value; } #endif //該標頭檔案,是處理中綴表示式轉化為字尾表示式,並計算字尾表示式 #ifndef calculate_h #define calculate_h #include

#include

#include"stack.h" using namespace std; class calculate }; int calculate::outpriority(char str) } int calculate::priority(char str) } void calculate::setinfix() bool calculate::isoperator(char str) } void calculate::postfix() operation.pop(); } else if (isoperator(infix[i])) postfix[j++] = ' ';//用空格來分開運算元 //棧運算子入棧 operation.push(infix[i]); } i++; } while (!operation.empty()) postfix[j] = '\0'; } int calculate::stringtonumber(char str,int* n) return value; } int calculate::value() else if (postfix[i] == '-') else if (postfix[i] == '*') else if (postfix[i] == '/') } return operator.gettop(); } #endif //主函式 #include"calculate.h" int main(int argc, char argv)

中綴表示式轉化為字尾表示式

注意 中綴表示式需要空格隔開運算元或者操作符 關鍵有 判斷是否操作符,操作符優先順序 public class profixexpression 計算排好的字尾操作計算式 param prostr return public static intprofixcalculate string pros...

中綴表示式轉化為字尾表示式

中綴表示式轉化為字尾表示式有兩種方法,一種是利用棧,一種是把表示式轉化為樹再進一步求解,今天我們來深入了解一下這兩種方法 給出下面乙個例子 我們把中綴表示式 9 3 1 3 10 2 轉化為字尾表示式 1.首先初始化乙個空棧,用來對符號進出棧使用 2.第乙個字元是數字9,輸出9,將後面的符號 進棧 ...

中綴表示式轉化為字尾表示式

中綴表示式轉化為字尾表示式 例如 1 2 3 4 7 5 123 4 75 1 遇到數字輸出,否則進棧。2 遇到有右括號匹配棧裡的左括號,輸出棧裡的內容 3 遇到比自己比棧裡的運算子優先順序高,入棧 4 遇到比自己比棧裡的運算子優先順序低,將棧裡的運算子出棧 include include incl...