G - Ternary Calculation(运算符优先级问题)

时间:2023-01-29 05:51:47

G - Ternary Calculation(运算符优先级问题)G - Ternary Calculation(运算符优先级问题)


考虑优先级问题,才两个运算符,可以的

#include<stdio.h>
#include<string.h>
int main(){
    int a,b,c,T,i,j,k,sum;
    char ss1[10],ss2[10];
    scanf("%d",&T);
    while(T--){
        scanf("%d%s%d%s%d",&a,ss1,&b,ss2,&c);
        sum=0;
        if((ss1[0]=='+'||ss1[0]=='-')&&(ss2[0]=='*'||ss2[0]=='/'||ss2[0]=='%')){
            if(ss2[0]=='*') sum=b*c;
            else if(ss2[0]=='/')  sum=b/c;
            else if(ss2[0]=='%') sum=b%c;
            else if(ss2[0]=='+') sum=b+c;
            else if(ss2[0]=='-')  sum=b-c;
            if(ss1[0]=='+')  sum+=a;
            else if(ss1[0]=='-')  sum=a-sum;
        }
        if((ss1[0]=='+'||ss1[0]=='-')&&(ss2[0]=='+'||ss2[0]=='-')){
            if(ss1[0]=='+') sum=a+b;
            else if(ss1[0]=='-') sum=a-b;
            if(ss2[0]=='+') sum+=c;
            else sum-=c;
        }
        if((ss1[0]=='*'||ss1[0]=='/'||ss1[0]=='%')&&(ss2[0]=='*'||ss2[0]=='/'||ss2[0]=='%'||ss2[0]=='+'||ss2[0]=='-')){
            if(ss1[0]=='*') sum=a*b;
            else if(ss1[0]=='/') sum=a/b;
            else if(ss1[0]=='%') sum=a%b;
            if(ss2[0]=='*') sum=sum*c;
            else if(ss2[0]=='/')  sum=sum/c;
            else if(ss2[0]=='%') sum=sum%c;
            else if(ss2[0]=='+')  sum+=c;
            else if(ss2[0]=='-')  sum-=c;
        }
        printf("%d\n",sum);
    }
}