nyoj267鬱悶的c小加 中綴表達式轉後綴求值

題目:

鬱悶的C小加(二)

時間限制:1000 ms | 內存限制:65535 KB

難度:4

描述

聰明的你幫助C小加解決了中綴表達式到後綴表達式的轉換(詳情請參考“鬱悶的C小加(一)”),C小加很高興。但C小加是個愛思考的人,他又想通過這種方法計算一個表達式的值。即先把表達式轉換爲後綴表達式,再求值。這時又要考慮操作數是小數和多位數的情況。

輸入

第一行輸入一個整數T,共有T組測試數據(T<10)。
每組測試數據只有一行,是一個長度不超過1000的字符串,表示這個運算式,每個運算式都是以“=”結束。這個表達式裏只包含+-*/與小括號這幾種符號。其中小括號可以嵌套使用。數據保證輸入的操作數中不會出現負數並且小於1000000。
數據保證除數不會爲0。

輸出

對於每組測試數據輸出結果包括兩行,先輸出轉換後的後綴表達式,再輸出計算結果,結果保留兩位小數。兩組測試數據之間用一個空行隔開。

樣例輸入

2
1+2=
(19+21)*3-4/5=

樣例輸出

12+=
3.00

1921+3*45/-=
119.20

來源

改編自NYOJ

上傳者

xiewuqiang

代碼:

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<string>
#include<vector>
#include<stack>
#include<bitset>
#include<cstdlib>
#include<cmath>
#include<set>
#include<list>
#include<cctype>
#include<deque>
#include<map>
#include<queue>
using namespace std;
typedef long long ll;
const double PI = acos(-1.0);
const double eps = 1e-6;
const int INF = 1000000000;
const int maxn = 1234;
int T,n,m;
int ranks[maxn];//優先級
char b[maxn];//存後綴表達式

void init()
{
    ranks['=']=-1;
    ranks['(']=0;
    ranks['+']=ranks['-']=1;
    ranks['*']=ranks['/']=2;
    ranks[')']=3;
}

double cal(char ch,double x,double y)
{
    if(ch=='+')
        return x+y;
    else if(ch=='-')
        return x-y;
    else if(ch=='*')
        return x*y;
    else
        return x/y;

}

void change(char a[])//中綴轉後綴
{
    stack<char>st;
    int j=0;
    int  len=strlen(a);
    st.push('=');//現將=壓進棧爲了之後的運算符好比較優先級
    for(int i=0;a[i]!='=';i++)
    {
        if(isdigit(a[i])||a[i]=='.')//小數情況
            b[j++]=a[i];
        else
        {
            b[j++]='#';//分割運算數
            if(a[i]=='(')
                st.push(a[i]);
            else if(a[i]==')')
            {
                while(st.top()!='(')
                {
                    b[j++]=st.top();
                    st.pop();
                }
                st.pop();
            }
            else
            {
                while(ranks[a[i]]<=ranks[st.top()])//把優先級大於他的都出棧
                {
                    b[j++]=st.top();
                    st.pop();
                }
                st.push(a[i]);
            }
        }
    }
    while(!st.empty())//最後清空棧
    {
        b[j++]=st.top();
        st.pop();
    }
    b[j]='\0';
}

double calculate()//後綴表達式的計算
{
    char c[maxn];
    int j=0;
    stack<double>st;
    for(int i=0;b[i]!='=';i++)
    {
        if(isdigit(b[i])||b[i]=='.')
            c[j++]=b[i];
        else
        {
            if(j!=0)
            {
                st.push(atof(c));//字符串轉數字函數的運用
                memset(c,'\0',sizeof(c));
                j=0;
            }
            if(b[i]!='#')
            {
                double n1,n2;
                n1=st.top();
                st.pop();
                n2=st.top();
                st.pop();
                st.push(cal(b[i],n2,n1));//運算併入棧
            }
        }
    }
    return st.top();
}

int main()
{
    init();
    char s[maxn];
    scanf("%d",&T);
    while(T--)
    {
        scanf("%s",s);
        change(s);
        for(int i=0;i<strlen(b);i++)
            if(b[i]!='#')
                printf("%c",b[i]);
        printf("\n");
        printf("%.2f\n",calculate());
    }
    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章