栈的应用 逆波兰式
#define_CRT_SECURE_NO_WARNINGS1#pragmaonce#include<iostream>#include<assert.h>#include<stack>usingnamespacestd;enumType{OP_NUM,OP_SYMBOL,};enumSYMBOL{ADD,SUB,MUL,DIV,};structCell{Type_type;int_value;};#include<stack>intCountRNP(Cella[],size_tsize){assert(a);stack<int>s;for(size_ti=0;i<size;++i){if(a[i]._type==OP_NUM){s.push(a[i]._value);}else{intright=s.top();s.pop();intleft=s.top();s.pop();switch(a[i]._value){caseADD:s.push(left+right);break;caseSUB:s.push(left-right);break;caseMUL:s.push(left*right);break;caseDIV:s.push(left/right);break;}}}returns.top();}voidTestRNP(){//vector<Cell>Cella[]={{OP_NUM,12},{OP_NUM,3},{OP_NUM,4},{OP_SYMBOL,ADD},{OP_SYMBOL,MUL},{OP_NUM,6},{OP_SYMBOL,SUB},{OP_NUM,8},{OP_NUM,2},{OP_SYMBOL,DIV},{OP_SYMBOL,ADD},};cout<<"运算结果:"<<CountRNP(a,sizeof(a)/sizeof(Cell))<<endl;}intmain(){TestRNP();getchar();return0;}
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。