中缀表达式转后缀表达式及后缀表达式的计算

#include <iostream>
#include <sstream>
#include <cstring>

using namespace std;

void inToSuffix(const char* mid, char* suf);
double calSuffix(const char* str);

template <typename T>
class Stack
{
private:
    T *ar;
    int Max;
    int num;

public:
    Stack(int n = 50):Max(n), num(0)
    {
        ar = new T[Max];
    }
    Stack(const Stack<T>& stc):ar(NULL)
    {
        *this = stc;
    }
    ~Stack(){delete[] ar;}
    const Stack& operator = (const Stack<T>& stc)
    {
        if(this != &stc)
        {
            delete [] ar;
            Max = stc.Max;
            ar = new T[Max];
            for(num = 0; num < stc.num; num++)
                ar[num] = stc.ar[num];
        }
        return *this;
    }
    int size()const
    {
        return num;
    }
    T top()const
    {
        return ar[num-1];
    }
    bool push(const T& value)
    {
        if(num < Max)
        {
            ar[num] = value;
            num++;
            return true;
        }
        else
            return false;
    }
    T pop()
    {
        return ar[--num];
    }
};


void inToSuffix(const char* in, char* suf)
{
    Stack<char> stc(strlen(in));
    int i = 0;
    for(; *in; in++)
    {
        if((*in >= '0' && *in <= '9')||*in == '.')
            suf[i++] = *in;
        else if(*in == '(')
            stc.push(*in);
        else if(*in == '+'|| *in == '-' || *in == ')')
        {
            suf[i++] = ' ';
            while(stc.size() && stc.top()!='(')
            {
                suf[i++] = stc.pop();
                suf[i++] = ' ';
            }
            if(*in != ')')
                stc.push(*in);
            else
                stc.pop();
        }
        else if(*in == '*'||*in == '/')
        {
            suf[i++] = ' ';
            while(stc.size() &&(stc.top() == '*'||stc.top() == '/'))
            {
                suf[i++] = stc.pop();
                suf[i++] = ' ';
            }
            stc.push(*in);
        }
    }
    while(stc.size())
    {
        suf[i++] = stc.pop();
        suf[i++] = ' ';
    }
    suf[i] = '\0';
}

double calSuffix(const char* str)
{
    istringstream instr(str);
    Stack<double> stc(20);
    double num;
    double num1;
    double num2;
    char ch;
    instr.exceptions(ios_base::eofbit);
    try
    {
        while(true)
        {
            instr >> ch;
            if(ch == '+')
            {
                num1 = stc.pop();
                num2 = stc.pop();
                stc.push(num2+num1);
                continue;
            }
            else if(ch == '-')
            {
                num1 = stc.pop();
                num2 = stc.pop();
                stc.push(num2-num1);
                continue;
            }
            else if(ch == '*')
            {
                num1 = stc.pop();
                num2 = stc.pop();
                stc.push(num2*num1);
                continue;
            }
            else if(ch == '/')
            {
                num1 = stc.pop();
                num2 = stc.pop();
                stc.push(num2/num1);
                continue;
            }
            else
                instr.putback(ch);
            if(instr >> num)
                stc.push(num);
            else
                instr.clear();
        }
    }
    catch(...)
    {}
    return stc.pop();
}


int main(void)
{
    char str1[100];
    char str2[100];
    cout << "Infix Expression:";
    cin >> str1;
    inToSuffix(str1, str2);
    cout << "Suffix Expression:" <<str2 <<endl;
    cout << "Result:" << calSuffix(str2) << endl;
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值