鏈棧實現表示式求值

2021-10-09 00:08:41 字數 1755 閱讀 5585

標頭檔案:

/***鏈棧實現表示式求值***/

#include

using

namespace std;

const

char oper[7]

=;#define ok 1

#define error 0

#define overflow -2

typedef

char selemtype;

typedef

int status;

typedef

struct snode snode,

*linkstack;

status initstack

(linkstack &s)

bool

stackempty

(linkstack s)

status push

(linkstack &s, selemtype e)

p->data = e;

p->next = s;

s = p;

return ok;

}status pop

(linkstack &s, selemtype &e)

status gettop

(linkstack &s)

boolin(

char ch)

}return

false;}

char

precede

(char theta1,

char theta2)

else

if(theta1 ==

'('|| theta1 ==

'#'|| theta2 ==

'('||

(theta1

=='+'|| theta1 ==

'-')

&&(theta2 ==

'*'|| theta2 ==

'/')

)else

return

'>';}

char

operate

(char first,

char theta,

char second)

return0;

}//演算法3.22 表示式求值

char

evaluateexpression()

//ch不是運算子則進opnd棧

else

switch

(precede

(gettop

(optr)

, ch)

)//比較optr的棧頂元素和ch的優先順序

//switch

}//while

return

gettop

(opnd)

;//opnd棧頂元素即為表示式求值結果

}

//主檔案

#include

"boke.h"

intmenu()

intmain()

break

;case0:

cout <<

"退出成功\n"

<< endl;

exit(0

);default

:break;}

}}

執行結果:

鏈棧(表示式求值)

include include includetypedef struct snode snode,linkstack int initstack linkstack s bool push linkstack s,char e 在棧頂插入元素e bool pop linkstack s,char ...

表示式求值 棧實現

宣告 僅個人小記 演算法思想 一 設定乙個運算子棧,設定乙個字尾表示式字串 二 從左到右依次對中綴表示式中的每個字元ch分別進行一下處理,直至表示式結束 1.若ch是左括號 將其入棧 2.若 ch 是數字,將其後連續若干數字新增到字尾表示式字串之後,在新增空格作為分割符 3.若ch是運算子,先將棧頂...

表示式求值(棧)

時間限制 3000 ms 記憶體限制 65535 kb 難度 4 描述 acm隊的mdd想做乙個計算器,但是,他要做的不僅僅是一計算乙個a b的計算器,他想實現隨便輸入乙個表示式都能求出它的值的計算器,現在請你幫助他來實現這個計算器吧。比如輸入 1 2 4 程式就輸出1.50 結果保留兩位小數 輸入...