1 条题解

  • 0
    @ 2026-9-30 12:34:27

    C++ :

    
    #include <cstdio>
    #include <cstdlib>
    /* 多组数据,每组一行;读到「单独一行 0」结束(该行不输出)。
       普通优先级:维护「当前项 term」与「累加和 total」。
       '+'/'-' 表示当前项结算并开启新项;'*'/'/' 直接作用在当前项上。
       除法为实数除法。每行 <= 200 字符。 */
    static char line[1005];
    int main(){
        while(fgets(line, sizeof(line), stdin)){
            char *p = line;
            while(*p == ' ' || *p == '\t') p++;
            if(*p == '0'){
                char *q = p + 1;
                int only = 1;
                while(*q && *q != '\n' && *q != '\r'){
                    if(*q != ' ' && *q != '\t'){ only = 0; break; }
                    q++;
                }
                if(only) break;
            }
            double total = 0.0, term;
            char *q = p;
            term = strtod(q, &q);
            while(1){
                while(*q == ' ' || *q == '\t') q++;
                if(*q == '\0' || *q == '\n' || *q == '\r') break;
                char c = *q++;
                while(*q == ' ' || *q == '\t') q++;
                double num = strtod(q, &q);
                if(c == '+'){ total += term; term = num; }
                else if(c == '-'){ total += term; term = -num; }
                else if(c == '*'){ term *= num; }
                else if(c == '/'){ term /= num; }
            }
            total += term;
            printf("%.2f\n", total);
        }
        return 0;
    }
    
    
    • 1

    信息

    ID
    2602
    时间
    1000ms
    内存
    256MiB
    难度
    (无)
    标签
    递交数
    0
    已通过
    0
    上传者