棧的應用之表示式求值

2021-08-07 05:40:56 字數 1617 閱讀 3344

#include 

#include

#include

#include

#include

#include

using

namespace

std;

struct stacknode

;//用來訪問運算子的棧

struct stacknode1

;//用於訪問數字的棧

typedef stacknode *stack;

typedef stacknode1 *nstack;

int priority(char); //用來確定運算子的優先順序

void pop(stack); //運算子棧的出棧

int pop1(nstack s); //數字棧的出棧

char top(stack); //獲取運算子棧的棧頂元素

int top1(nstack s); //獲取數字棧的棧頂元素

void push(stack,char); //將運算子壓入運算子棧

void push1(nstack,char); //將字元轉換為數字後壓入數字棧

void push2(nstack s,int c); //直接將數字壓入數字棧

bool isnumber(char); //判斷字元是否為數字

bool isempty(stack); //判斷棧是否為空

int main()

if(instr[i]==')')

pop(s);

continue;

}while(!isempty(s)&&top(s)!='('&&priority(instr[i])<=priority(top(s)))

push(s,instr[i]);}}

while(!isempty(s))

outstr[count]='\0';

for(int i=0;iprintf("%c ",outstr[i]);

}cout

for(int i=0;iif(isnumber(outstr[i]))

else

if(outstr[i]=='+')

else

if(outstr[i]=='-')

else

if(outstr[i]=='*')

else

if(outstr[i]=='/')

}printf("the test result is:%d",top1(result));

}bool isnumber(char c)

bool isempty(stack s)

int priority(char c)

void push(stack s,char c)

void push1(nstack s,char c)

void push2(nstack s,int c)

char top(stack s)

int top1(nstack s)

int pop1(nstack s)

void pop(stack s)

棧的應用之算術表示式求值

1 2 3 4 此算術表示式由一些操作符和運算元組成。其中,操作符有 等,運算元有 1 2 3 等。對於操作符來說,其運算是有優先順序的。比如,上述表示式中,3 4應該先進行操作,將得到的結果再與2相乘。算符間的優先關係如下 運算子 根據算符間的優先關係表,使用兩個棧。乙個棧為optr,儲存運算子,...

棧的應用之後綴表示式求值

字尾表示式,指的是不包含括號,運算子放在兩個運算物件的後面,所有的計算按運算子出現的順序,嚴格從左向右進行 不再考慮運算子的優先規則 我們數學上採用的表示式叫中綴表示式,即將運算放在兩個運算物件中間。此外還有字尾表示式,即將運算子放在兩個運算物件的前面。下面是同乙個算術表示式的三種等價表示形式 中綴...

棧應用 表示式求值

include stdafx.h include using namespace std const int explenght 20 const int stack init size 20 const int stack incrment 10 templateclass stack stack...