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

    标题:表达式计算

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

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

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

    #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 *= _______________________________;  //填空
    		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);
    		if(s[p]==0) break;
    		p++;
    	}
    	
    	return sum;
    }
    
    int main()
    {
    	int x = f("12+18+5*4*3+10");
    	printf("%d
    ", x);
    	return 0;
    }
    

    注意:只填写划线处缺少的内容,不要填写已有的代码或符号,也不要填写任何解释说明文字等。

    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.^
                                 1 ^ 0                                     ^^110.^
                               0.   0 ^                                    ^^^10.01
                       ^^     010^   1 1                                   ^^^1110.1
                       0001  10 0   ^ 1.1                                   ^^^1111110
                       0^ 10 . 01   ^^  ^^                                   ^^^1111^1.^           ^^^
                       10  10^ 0^                                             ^^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.     .^
     */
    /* Procedural objectives:
    
    Functions required by the program:
    
    Variables required by the program:
    
    */
    /* My dear Max said:
    "I like you,
    So the first bunch of sunshine I saw in the morning is you,
    The first hurricane that passed through your ear is you,
    The first star you 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 *= 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);
    		if(s[p]==0) break;
    		p++;
    	}
    	
    	return sum;
    }
    
    int main()
    {
    	int x = f("12+18+5*4*3+10");
    	printf("%d
    ", x);
    	return 0;
    }
    
  • 相关阅读:
    004.Jquery库的用法
    update 死锁问题
    Nginx负载均衡模式
    微信公众号开启服务器配置 JAVA
    mybatis plus + AOP 多数据源自动切换
    mybatis plus 快速上手
    mybits 笔记
    java 异步
    node 垃圾回收机制
    常用正则
  • 原文地址:https://www.cnblogs.com/AlexKing007/p/12338273.html
Copyright © 2011-2022 走看看