栈—中缀、后缀表达式

利用栈计算后缀表达式

#include 
#include 
#include 
using namespace std;
int main()
{
    stack s;
    char str[50];
    cin.getline( str,sizeof(str),'\n' );
    //cout<='0' ){

            while( str[i]<='9'&&str[i]>='0' ){
                num= num*10 + (str[i]-'0');
                ++i;
            }
            s.push( num );
            --i;
            //cout<<"i:  "<

利用栈,把中缀表达式变成后缀表达式

#include 
#include 
#include 
#include 
using namespace std;

int main()
{
    //定义运算符优先级
    map priority;
    priority['('] = 0;
    priority['+'] = priority['-'] = 1;
    priority['*'] = priority['/'] = 2;

    char str[50];
    cin.getline( str,sizeof(str),'\n' );

    stack s;

    for( int i=0; i='0' ){

            while( str[i]<='9'&&str[i]>='0' ){
                cout<

你可能感兴趣的:(栈,数据结构)