資料結構 棧的應用 中綴表示式求值(c )

2021-07-03 04:36:38 字數 1540 閱讀 3908

標頭檔案:

#pragma once

#include #include #include using namespace std;

templateclass seqstack

; type *base;

int capacity;

int top;

};templateseqstack::seqstack(size_t sz = init_sz)

templateseqstack::~seqstack()

// 判斷棧是否滿了

templatebool seqstack::full()const

// 判斷是否為空棧

templatebool seqstack::empty()const

// 顯示

templatevoid seqstack::show()const

for (int i = top - 1; i >= 0; --i) }

// 入棧

templatebool seqstack::push(const type &x)

else }

// 出棧

templatebool seqstack::pop()

else }

// 獲得棧頂元素

templatevoid seqstack::gettop(type &x)

// 求棧長度

templateint seqstack::length()const

// 清空棧

templatevoid seqstack::clear()

// 摧毀棧

templatevoid seqstack::destory()

// 退出系統

templatevoid seqstack::quit_system(type &x)

主函式:

#include "exp.h"

#include "exp.h"

//檢查符號之間的優先順序,1表示》,0表示=,-1表示

int check(char c1, char c2)

//符號運算函式

double sum(char c, double d1, double d2)

}int main()

//運算子優先順序比較

char val;

optr.gettop(val);

int che = check(val, str[a]);

if (-1 == che)//棧外優先順序大直接入棧

if (0 == che)//棧內外優先順序相等則出棧

if (1 == che)//棧內優先順序大,出棧進行運算

資料結構 6 棧的應用之中綴表示式求值

參考書籍 資料結構 c語言版 嚴蔚敏 吳偉民編著 清華大學出版社 分析 設操作符棧op棧 運算元棧num棧 op棧裡不可能出現 只可能有 設ch為當前字元,假設輸入都是合法的 1.ch為 則op棧頂為 時ch入棧,否則進行運算直至op棧頂為 2.ch為 則op棧頂為 時ch入棧,否則op棧頂為 此時...

資料結構 中綴表示式求值解法

簡述中綴表示式的做題思想 例如 輸入乙個表示式為 3 2 5 3 1 3 4 2 定義兩個棧,乙個是操作符棧 sdoc 乙個是運算元棧 sdop 1.首先判斷輸入的字元是數還是操作符 如果是運算元 char cinch int x while cinch getchar n 此處是輸入函式並且不為回...

棧的典型應用 中綴表示式求值

include define max 1010 using namespace std 表示式求值有兩步,一步是中綴表示式轉字尾表示式,一步是字尾表示式求值。中綴轉字尾 從左到右遍歷中綴表示式的每個數字和符號,1 若是數字就輸出,即成為字尾表示式的一部分。2 待入棧元素為 時,彈棧至遇到 3 當棧空...