zoukankan      html  css  js  c++  java
  • 2017年第八届蓝桥杯C/C++ A组国赛 —— 第三题:表达式计算

    标题:表达式计算

    虽然我们学了许久的程序设计,但对于简单的四则混合运算式,如果让我们完全白手起家地编程来解析,还是有点棘手。

    这里,我们简化一下问题,假设只有加法和乘法,并且没有括号来改变优先级。
    再假设参加运算的都是正整数。

    在这么多的限制条件下,表达式的解析似乎简单了许多。
    下面的代码解决了这个问题。请仔细阅读源码,并填写划线部分缺少的代码。

    Code

    /*
                                    ^....0
                                   ^ .1 ^1^
                                   ..     01
                                  1.^     1.0
                                 ^ 1  ^    ^0.1
                                 1 ^        ^..^
                                 0.           ^ 0^
                                 .0            1 .^
                                 .1             ^0 .........001^
                                 .1               1. .111100....01^
                                 00                 11^        ^1. .1^
                                 1.^                              ^0  0^
                                   .^                                 ^0..1
                                   .1                                   1..^
                                 1 .0                                     ^  ^
                                  00.                                     ^^0.^
                                  ^ 0                                     ^^110.^
                              0   0 ^                                     ^^^10.01
                       ^^     10  1 1                                      ^^^1110.1
                       01     10  1.1                                      ^^^1111110
                       010    01  ^^                                        ^^^1111^1.^           ^^^
                       10  10^ 0^ 1                                            ^^111^^^0.1^       1....^
                        11     0                                               ^^11^^^ 0..  ....1^   ^ ^
                        1.     0^                                               ^11^^^ ^ 1 111^     ^ 0.
                       10   00 11                                               ^^^^^   1 0           1.
                       0^  ^0  ^0                                                ^^^^    0            0.
                       0^  1.0  .^                                               ^^^^    1 1          .0
                       ^.^  ^^  0^                             ^1                ^^^^     0.         ^.1
                       1 ^      11                             1.                ^^^     ^ ^        ..^
                      ^..^      ^1                             ^.^               ^^^       .0       ^.0
                      0..^      ^0                              01               ^^^       ..      0..^
                     1 ..        .1                             ^.^              ^^^       1 ^  ^0001
                    ^  1.        00                              0.             ^^^        ^.0 ^.1
                    . 0^.        ^.^                             ^.^            ^^^         ..0.0
                   1 .^^.         .^                  1001        ^^            ^^^         . 1^
                   . ^ ^.         11                0.    1         ^           ^^          0.
                    0  ^.          0              ^0       1                   ^^^          0.
                  0.^  1.          0^             0       .1                   ^^^          ..
                  .1   1.          00            .        .1                  ^^^           ..
                 1      1.         ^.           0         .^                  ^^            ..
                 0.     1.          .^          .         0                                  .
                 .1     1.          01          .        .                                 ^ 0
                ^.^     00          ^0          1.       ^                                 1 1
                .0      00           .            ^^^^^^                                   .
                .^      00           01                                                    ..
               1.       00           10                                                   1 ^
              ^.1       00           ^.                                            ^^^    .1
              ..        00            .1                                        1..01    ..
             1.1         00           1.                                       ..^      10
            ^ 1^         00           ^.1                                      0 1      1
            .1           00            00                                       ^  1   ^
             .           00            ^.^                                        10^  ^^
           1.1           00             00                                              10^
           ..^           1.             ^.                                               1.
          0 1            ^.              00                 00                            .^
            ^            ^.              ^ 1                00   ^0000^     ^               01
         1 0             ^.               00.0^              ^00000   1.00.1              11
         . 1              0               1^^0.01                      ^^^                01
          .^              ^                1   1^^                                       ^.^
        1 1                                                                              0.
        ..                                                                              1 ^
         1                                                                               1
       ^ ^                                                                             .0
       1                                                                             ^ 1
       ..                                                          1.1            ^0.0
      ^ 0                                                           1..01^^100000..0^
      1 1                                                            ^ 1 ^^1111^ ^^
      0 ^                                                             ^ 1      1000^
      .1                                                               ^.^     .   00
      ..                                                                1.1    0.   0
      1.                                                                  .    1.   .^
      1.                                                                 1    1.   ^0
     ^ .                                                                 ^.1 00    01
     ^.0                                                                  001.     .^
     */
    // VB_king —— 2017_Finals_A_3.cpp created by VB_KoKing on 2019-05-06:18.
    /* Procedural objectives:
    
     Variables required by the program:
    
     Procedural thinking:
    代码填空题有很大的对称性
     Functions required by the program:
     
     Determination algorithm:
     
     Determining data structure:
     
    
    */
    /* My dear Max said:
    "I like you,
    So the first bunch of sunshine I saw in the morning is you,
    The first gentle breeze that passed through my ear is you,
    The first star I see is also you.
    The world I see is all your shadow."
    
    FIGHTING FOR OUR FUTURE!!!
    */
    #include <stdio.h>
    
    int f3(const char* s, int begin, int end)
    {
        int sum = 0;
        int i;
        for(i=begin; i<end; i++){
            if(s[i]==' ') continue;
            sum = sum * 10 + (s[i]-'0');
        }
        return sum;
    }
    
    int f2(const char* s, int begin, int end)
    {
        int p = begin;
        int pro = 1;
        while(1){
            int p0 = p;
            while(p!=end && s[p]!='*') p++;
    //        pro *= _______________________________;  //填空
    //p0变量和f3函数还未使用
            pro *= f3(s,p0,p);
            if(p==end) break;
            p++;
        }
        printf("f2: pro=%d
    ", pro);
        return pro;
    }
    
    int f(const char* s)
    {
        int p = 0;
        int sum = 0;
        while(1){
            int p0 = p;
            while(s[p]!=0 && s[p]!='+') p++;
            //当字符串不为空字符并且不为+号时,等价于找到第一个+为止
            sum += f2(s,p0,p);
            //把字符串s从p0到p传入函数f2
            if(s[p]==0) break;
            //如果当前字符为空字符,直接跳出
            p++;
        }
    
        return sum;
    }
    
    int main()
    {
        int x = f("12+18+5*4*3+10");
        printf("%d
    ", x);
        return 0;
    }
    
    
  • 相关阅读:
    【区间DP&&记忆化搜索】乘法游戏
    洛谷P1608路径统计
    2021省选游记
    涂色计划P4170
    01迷宫及路径记录(DFS&&BFS)
    [YBTOJ递推算法强化训练4]序列个数
    C++关于string 的优先队列以及重载运算符
    浅谈C++STL容器
    集合的划分
    图的表示
  • 原文地址:https://www.cnblogs.com/AlexKing007/p/12338343.html
Copyright © 2011-2022 走看看