郁闷的C小加(三)中缀转前后缀并计算

           模板:各种缀的转换以及计算



代码:

#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<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 pre[maxn];//存前缀表达式
char suf[maxn];//存后缀表达式

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

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

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_pre(char a[])//中缀转前缀
{
    init_pre();
    stack<char>st;
    char b[maxn];
    int j=0;
    int  len=strlen(a);
    b[j++]='=';
    for(int i=len-2;i>=0;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.empty()&&st.top()!=')')
                {
                    b[j++]=st.top();
                    st.pop();
                }
                st.pop();
            }
            else
            {
                while(!st.empty()&&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';
    int k=0;
    for(int i=j-1;i>=0;i--)
        pre[k++]=b[i];
    pre[k]='\0';
}

double calculate_pre()//前缀表达式表达式的计算
{
    stack<double>q;
    int len=strlen(pre);
    for(int i=len-2;i>=0;i--)
    {
        if(pre[i]==' '||pre[i]=='#')
            continue;
        else if(isdigit(pre[i]))
        {
            while(isdigit(pre[i])||pre[i]=='.')//小数处理
                i--;
            i++;
            double t=atof(&pre[i]);//atof:将字串转换成浮点型数
            q.push(t);
        }
        else
        {
            double a=q.top();
            q.pop();
            double b=q.top();
            q.pop();
            q.push(cal(pre[i],a,b));
        }
    }
    return q.top();
}


void change_suf(char a[])//中缀转后缀
{
    init_suf();
    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]=='.')//小数情况
            suf[j++]=a[i];
        else
        {
            suf[j++]='#';//分割运算数
            if(a[i]=='(')
                st.push(a[i]);
            else if(a[i]==')')
            {
                while(st.top()!='(')
                {
                    suf[j++]=st.top();
                    st.pop();
                }
                st.pop();
            }
            else
            {
                while(ranks[a[i]]<=ranks[st.top()])//把优先级大于他的都出栈
                {
                    suf[j++]=st.top();
                    st.pop();
                }
                st.push(a[i]);
            }
        }
    }
    while(!st.empty())//最后清空栈
    {
        suf[j++]=st.top();
        st.pop();
    }
    suf[j]='\0';
}


double calculate_suf()//后缀表达式的计算
{
    char c[maxn];
    int j=0;
    stack<double>st;
    for(int i=0;suf[i]!='=';i++)
    {
        if(isdigit(suf[i])||suf[i]=='.')
            c[j++]=suf[i];
        else
        {
            if(j!=0)
            {
                st.push(atof(c));//字符串转数字函数的运用
                memset(c,'\0',sizeof(c));
                j=0;
            }
            if(suf[i]!='#')
            {
                double n1,n2;
                n1=st.top();
                st.pop();
                n2=st.top();
                st.pop();
                st.push(cal(suf[i],n2,n1));//运算并入栈
            }
        }
    }
    return st.top();
}



void print(char pr[])//输出
{
    int i=0;
    while(pr[i]=='#')
            i++;
    printf("%c",pr[i++]);
    for(i=i;pr[i];i++)
    {
        if(pr[i]=='.'||(isdigit(pr[i])&&(isdigit(pr[i-1])||pr[i-1]=='.')))
            printf("%c",pr[i]);
        else if(pr[i]!='#')
            printf(" %c",pr[i]);
    }
    printf("\n");
}

int main()
{
    scanf("%d",&T);
    char a[maxn];
    while(T--)
    {
        scanf("%s",a);
        change_pre(a);
        change_suf(a);
        print(pre);
        print(suf);
        //printf("%.2f\n",calculate_suf());
        printf("%.2f\n",calculate_pre());
    }
    return 0;
}

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值