表达式求值简单类型总结

前缀和后缀转中缀
中缀转后缀 nyoj郁闷的C小加(一)

#include<stdio.h>
#include<stack>
#include<string.h>
#include<iostream>
#include<algorithm>
using namespace std;
/*
2
1+2
(1+2)*3+4*5

12+
12+3*45*+
*/
const int maxn=1005;
char str[maxn];
stack<char>stack_ch;
void solve1()
{
    char chtop=stack_ch.top();
    while(chtop!='(')
    {
        printf("%c",chtop);
        stack_ch.pop();
        chtop=stack_ch.top();
    }
}
void solve2()
{
    char chtop=stack_ch.top();
    while(chtop=='*'||chtop=='/')
    {
        printf("%c",chtop);
        stack_ch.pop();
        chtop=stack_ch.top();
    }
}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%s",str);
        int len=strlen(str);
        str[len]='=';
        stack_ch.push('(');
        for(int i=0; i<=len; i++)
        {
            if(str[i]>='0'&&str[i]<='9')
            {
                printf("%c",str[i]);
                continue;
            }
            switch(str[i])
            {
            case '+':
            case '-':
                solve1();
                stack_ch.push(str[i]);
                break;
            case '*':
            case '/':
                solve2();
                stack_ch.push(str[i]);
                break;
            case '(':
                stack_ch.push(str[i]);
                break;
            case ')':
            case '=':
                solve1();
                stack_ch.pop();
                break;
            }
        }
        printf("\n");
    }
    return 0;
}

中缀转后缀并求结果 郁闷的C小加(二)

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<stack>
#include<iostream>
#include<algorithm>
using namespace std;
/*
2
1+2=
(19+21)*3-4/5=

12+=
3.00

1921+3*45/-=
119.20
*/
const int maxn=1009;
char str[maxn];
char num[maxn];
stack<char>stack_ch;
stack<double>stack_num;
void solve1()
{
    char ch=stack_ch.top();
    while(ch!='(')
    {
        double num1=stack_num.top();
        stack_num.pop();
        double num2=stack_num.top();
        stack_num.pop();
        printf("%c",ch);
        switch(ch)
        {
        case '+':
            num2+=num1;
            break;
        case '-':
            num2-=num1;
            break;
        case '*':
            num2*=num1;
            break;
        case '/':
            num2/=num1;
            break;
        }
        stack_ch.pop();
        stack_num.push(num2);
        ch=stack_ch.top();
    }
}
void solve2()
{
    char ch=stack_ch.top();
    while(ch=='*'||ch=='/')
    {
        printf("%c",ch);
        double num1=stack_num.top();
        stack_num.pop();
        double num2=stack_num.top();
        stack_num.pop();
        switch(ch)
        {
        case '*':
            num2*=num1;
            break;
        case '/':
            num2/=num1;
            break;
        }
        stack_ch.pop();
        stack_num.push(num2);
        ch=stack_ch.top();
    }
}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        int k=0;
        scanf("%s",str);
        stack_ch.push('(');
        for(int i=0; i<strlen(str); i++)
        {
            if((str[i]-'0'>=0&&str[i]-'0'<=9)||str[i]=='.')
            {
                num[k++]=str[i];
                continue;
            }
            num[k]=0;
            if(num[0]!=0)
            {
                double m = atof(num);
                printf("%s",num);
                num[0] = 0;
                stack_num.push(m);
            }
            k=0;
            switch(str[i])
            {
            case '+':
            case '-':
                solve1();
                stack_ch.push(str[i]);
                break;
            case '*':
            case '/':
                solve2();
                stack_ch.push(str[i]);
                break;
            case '(':
                stack_ch.push(str[i]);
                break;
            case ')':
            case '=':
                solve1();
                stack_ch.pop();
                break;
            }
        }
        printf("=\n");
        printf("%.2lf\n",stack_num.top());
        if(t)
            printf("\n");
        stack_num.pop();
    }
    return 0;
}

nyoj 中缀转前缀,后缀并求值 郁闷的C小加(三)

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<stack>
#include<iostream>
#include<algorithm>
using namespace std;
/*
2
1+2=
(19+21)*3-4/5=
+ 1 2 =
1 2 + =
3.00
- * + 19 21 3 / 4 5 =
19 21 + 3 * 4 5 / - =
119.20
*/
const int maxn=1009;
char str[maxn];
char num[maxn];
char num2[maxn];
stack<char>stack_ch;
stack<double>stack_num;
stack<char>stack_prech;
stack<string>stack_ans;
void solve1()
{
    char ch=stack_ch.top();
    while(ch!='(')
    {
        double num1=stack_num.top();
        stack_num.pop();
        double num2=stack_num.top();
        stack_num.pop();
        printf("%c ",ch);
        switch(ch)
        {
        case '+':
            num2+=num1;
            break;
        case '-':
            num2-=num1;
            break;
        case '*':
            num2*=num1;
            break;
        case '/':
            num2/=num1;
            break;
        }
        stack_ch.pop();
        stack_num.push(num2);
        ch=stack_ch.top();
    }
}
void solve2()
{
    char ch=stack_ch.top();
    while(ch=='*'||ch=='/')
    {
        printf("%c ",ch);
        double num1=stack_num.top();
        stack_num.pop();
        double num2=stack_num.top();
        stack_num.pop();
        switch(ch)
        {
        case '*':
            num2*=num1;
            break;
        case '/':
            num2/=num1;
            break;
        }
        stack_ch.pop();
        stack_num.push(num2);
        ch=stack_ch.top();
    }
}
//中缀转前缀表达式
int judge(char c)
{
    if(c=='*'||c=='/') return 2;
    if(c=='+'||c=='-') return 1;
}
void solve(char ch)
{
    if(stack_prech.empty()||stack_prech.top()==')')
    {
        stack_prech.push(ch);
        return ;
    }
    if(judge(ch)>=judge(stack_prech.top()))
    {
        stack_prech.push(ch);
        return ;
    }


    char chh=stack_prech.top();
    string s="";
    s+=chh;
    stack_ans.push(s);
    stack_prech.pop();
    solve(ch);
}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%s",str);
        int k=0;

        for(int i=strlen(str)-2; i>=0; i--)//前缀表达式
        {
            if((str[i]>='0'&&str[i]<='9')||str[i]=='.')
            {
                while((i>=0&&str[i]>='0'&&str[i]<='9')||str[i]=='.')
                {
                    num[k++]=str[i];
                    i--;
                }
                i++;
                num[k]=0;
                string s="";
                while(k--)
                {
                    s+=num[k];
                }
                stack_ans.push(s);
                k=0;
            }
            else if(str[i]=='(')
            {
                while(stack_prech.top()!=')')
                {
                    char ch=stack_prech.top();
                    string s="";
                    s+=ch;
                    stack_ans.push(s);
                    stack_prech.pop();
                }
                stack_prech.pop();
            }
            else if(str[i]==')')
            {
                stack_prech.push(str[i]);
            }
            else
            {
                solve(str[i]);
            }
        }
        while(!stack_prech.empty())
        {
            char ch=stack_prech.top();
            string s="";
            s+=ch;
            stack_ans.push(s);
            stack_prech.pop();
        }
        while(!stack_ans.empty())
        {
            cout<<stack_ans.top()<<" ";
            stack_ans.pop();
        }
        printf("=\n");
        //中缀转后缀表达式并求值
        k=0;
        stack_ch.push('(');
        for(int i=0; i<strlen(str); i++)
        {
            if((str[i]-'0'>=0&&str[i]-'0'<=9)||str[i]=='.')
            {
                num[k++]=str[i];
                continue;
            }
            num[k]=0;
            if(num[0]!=0)
            {
                double m = atof(num);
                printf("%s ",num);
                num[0] = 0;
                stack_num.push(m);
            }
            k=0;
            switch(str[i])
            {
            case '+':
            case '-':
                solve1();
                stack_ch.push(str[i]);
                break;
            case '*':
            case '/':
                solve2();
                stack_ch.push(str[i]);
                break;
            case '(':
                stack_ch.push(str[i]);
                break;
            case ')':
            case '=':
                solve1();
                stack_ch.pop();
                break;
            }
        }
        printf("=\n");
        printf("%.2lf\n",stack_num.top());
        stack_num.pop();
    }
    return 0;
}

nyoj前缀式计算

#include<stdio.h>
#include<string>
#include<sstream>
#include<iostream>
#include<algorithm>
using namespace std;
/*
+ 2 * + 3 4 5
+ 5.1 / 3 7
37.00
5.53
*/
stringstream ss;
double a;
char ch;
double solve()
{
    ss>>ch;
    switch(ch)
    {
    case '+':
        return solve()+solve();
    case '-':
        return solve()-solve();
    case '*':
        return solve()*solve();
    case '/':
        return solve()/solve();
    default:
        ss.unget();
        ss>>a;
        return a;
    }
}
int main()
{
    string str;
    while(getline(cin,str))
    {
        ss.clear();
        ss<<str;
        printf("%.2lf\n",solve());
    }
    return 0;
}

nyoj 中缀式变后缀式

#include<stdio.h>
#include<stack>
#include<string.h>
#include<iostream>
#include<algorithm>
using namespace std;
const int maxn=1005;
/*
2
1.000+2/4=
((1+2)*5+1)/4=

1.000 2 4 / + =
1 2 + 5 * 1 + 4 / =
*/
char str[maxn];
char num[maxn];
stack<char>stack_ch;
void solve1()
{
    char chtop=stack_ch.top();
    while(chtop!='(')
    {
        printf("%c ",chtop);
        stack_ch.pop();
        chtop=stack_ch.top();
    }
}
void solve2()
{
    char chtop=stack_ch.top();
    while(chtop=='*'||chtop=='/')
    {
        printf("%c ",chtop);
        stack_ch.pop();
        chtop=stack_ch.top();
    }
}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%s",str);
        int len=strlen(str);
        stack_ch.push('(');
        int k=0;
        for(int i=0; i<len; i++)
        {
            if((str[i]-'0'>=0&&str[i]-'0'<=9)||str[i]=='.')
            {
                num[k++]=str[i];
                continue;
            }
            num[k]=0;
            if(num[0]!=0)
            {
                double m = atof(num);
                printf("%s ",num);
                num[0] = 0;
            }
            k=0;
            switch(str[i])
            {
            case '+':
            case '-':
                solve1();
                stack_ch.push(str[i]);
                break;
            case '*':
            case '/':
                solve2();
                stack_ch.push(str[i]);
                break;
            case '(':
                stack_ch.push(str[i]);
                break;
            case ')':
            case '=':
                solve1();
                stack_ch.pop();
                break;
            }
        }

        printf("=\n");
    }
    return 0;
}

洛谷 P1449 后缀表达式

#include<stdio.h>
#include<stack>
#include<string>
#include<sstream>
#include<iostream>
#include<algorithm>
using namespace std;
/*
3.5.2.-*7.+@

16
*/
stack<int>q;
int main()
{
    char ch;
    while(cin>>ch&&ch!='@')
    {
        if(ch=='.')
            continue;
        else if(ch>='0'&&ch<='9')
        {
            int ans=0;
            ans=ch-'0';
            while(cin>>ch&&ch>='0'&&ch<='9')
            {
                ans=ans*10+ch-'0';
            }
            q.push(ans);
        }
        else if(ch=='-')
        {
            int t=q.top();
            q.pop();
            int s=q.top();
            q.pop();
            q.push(s-t);
        }
        else if(ch=='+')
        {
            int t=q.top();
            q.pop();
            int s=q.top();
            q.pop();
            q.push(s+t);
        }
        else if(ch=='*')
        {
            int t=q.top();
            q.pop();
            int s=q.top();
            q.pop();
            q.push(s*t);
        }
        else if(ch=='/')
        {
            int t=q.top();
            q.pop();
            int s=q.top();
            q.pop();
            q.push(s/t);
        }
    }
    printf("%d\n",q.top());
    q.pop();
    return 0;
}

普通的表达式求值nyoj35 表达式求值

/*
2
1.000+2/4=
((1+2)*5+1)/4=

1.50
4.00
*/
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<stack>
#include<iostream>
#include<algorithm>
using namespace std;
const int maxn=1009;
char str[maxn];
char num[maxn];
stack<char>stack_ch;
stack<double>stack_num;
void solve1()
{
    char ch=stack_ch.top();
    while(ch!='(')
    {
        double num1=stack_num.top();
        stack_num.pop();
        double num2=stack_num.top();
        stack_num.pop();
        switch(ch)
        {
        case '+':
            num2+=num1;
            break;
        case '-':
            num2-=num1;
            break;
        case '*':
            num2*=num1;
            break;
        case '/':
            num2/=num1;
            break;
        }
        stack_ch.pop();
        stack_num.push(num2);
        ch=stack_ch.top();
    }
}
void solve2()
{
    char ch=stack_ch.top();
    while(ch=='*'||ch=='/')
    {
        double num1=stack_num.top();
        stack_num.pop();
        double num2=stack_num.top();
        stack_num.pop();
        switch(ch)
        {
        case '*':
            num2*=num1;
            break;
        case '/':
            num2/=num1;
            break;
        }
        stack_ch.pop();
        stack_num.push(num2);
        ch=stack_ch.top();
    }
}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        int k=0;
        scanf("%s",str);
        stack_ch.push('(');
        for(int i=0; i<strlen(str); i++)
        {
            if((str[i]-'0'>=0&&str[i]-'0'<=9)||str[i]=='.')
            {
                num[k++]=str[i];
                continue;
            }
            num[k]=0;
            if(num[0]!=0)
            {
                double m = atof(num);
                num[0] = 0;
                stack_num.push(m);
            }
            k=0;
            switch(str[i])
            {
            case '+':
            case '-':
                solve1();
                stack_ch.push(str[i]);
                break;
            case '*':
            case '/':
                solve2();
                stack_ch.push(str[i]);
                break;
            case '(':
                stack_ch.push(str[i]);
                break;
            case ')':
            case '=':
                solve1();
                stack_ch.pop();
                break;
            }
        }
        printf("%.2lf\n",stack_num.top());
        stack_num.pop();
    }
    return 0;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值