2022.1.26

T1 T2 T3 完全一样
T3 没有一次 AC 的原因在于从T2贴代码时没有加上&&top3==1&&top==4,拖了自己近两个小时
至于T3T4的表达式求值
2022.1.26

点击查看代码
#include<iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<string.h>
using namespace std;
char a[100010],s1[100010],s2[100010];
int n,top1,top2,p,js[100010],topjs;
int judge(char x)
{
    if(x=='+'||x=='-')return 1;
    else if(x=='*'||x=='/')return 2;
    else if(x=='^')return 3;
    return 0;
}
int main()
{
    scanf("%s",a);
    n=strlen(a);
    for(int i=0;i<n;i++)
    {
        if(a[i]>='0'&&a[i]<='9')s2[++top2]=a[i];
        else{
            if(a[i]=='('||s1[top1]=='('||top1==0)
            {
                s1[++top1]=a[i];
                continue;
            }
            if(judge(s1[top1])>=judge(a[i])&&a[i]!=')')//需要按优先级弹栈重新进栈
            {
                s2[++top2]=s1[top1--];
                while(judge(s1[top1])>=judge(a[i])) s2[++top2]=s1[top1--];//级别低者压入数字栈
                s1[++top1]=a[i];
                continue;
            }
            if(judge(s1[top1])<judge(a[i]&&a[i]!=')'))//正常
            {
                s1[++top1]=a[i];
                continue;
            }
            if(a[i]=='(')
            {
                while(s1[top1]!='(') s2[++top2]=s1[top1--];
                top1--;
                continue;
            }
        }
    }
    while(top1>0) s2[++top2]=s1[top1--];//中缀 → 后缀
    for(int i=1;i<=top2;i++)
    {
    	p=i;
    	if(s2[i]>='0'&&s2[i]<='9')js[++topjs]=s2[i]-'0';
    	else
    	{
    		if(s2[i]=='+') js[topjs]=js[topjs]+js[--topjs];
    		if(s2[i]=='-') js[topjs]=js[topjs]-js[--topjs];
    		if(s2[i]=='*') js[topjs]=js[topjs]*js[--topjs];
    		if(s2[i]=='/') js[topjs]=js[topjs]/js[--topjs];
    		if(s2[i]=='^') js[topjs]=pow(js[topjs],js[--topjs]);
    	}
    }
    printf("%d",js[topjs]);
    return 0;
}
上一篇:DeepLearning:CNN网络学习之LetNet-5解读(论文+分析+代码)


下一篇:018Java面试002String常量池