2017年第八届蓝桥杯C/C++ A组国赛 —— 第三题:表达式计算
生活随笔
收集整理的這篇文章主要介紹了
2017年第八届蓝桥杯C/C++ A组国赛 —— 第三题:表达式计算
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
標題:表達式計算
雖然我們學了許久的程序設計,但對于簡單的四則混合運算式,如果讓我們完全白手起家地編程來解析,還是有點棘手。
這里,我們簡化一下問題,假設只有加法和乘法,并且沒有括號來改變優先級。
再假設參加運算的都是正整數。
在這么多的限制條件下,表達式的解析似乎簡單了許多。
下面的代碼解決了這個問題。請仔細閱讀源碼,并填寫劃線部分缺少的代碼。
Code
/*^....0^ .1 ^1^.. 011.^ 1.0^ 1 ^ ^0.11 ^ ^..^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.101 10 1.1 ^^^1111110010 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. ^.11 ^ 11 1. ^^^ ^ ^ ..^^..^ ^1 ^.^ ^^^ .0 ^.00..^ ^0 01 ^^^ .. 0..^1 .. .1 ^.^ ^^^ 1 ^ ^0001^ 1. 00 0. ^^^ ^.0 ^.1. 0^. ^.^ ^.^ ^^^ ..0.01 .^^. .^ 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^ ^ 011 0 ^. 00.0^ ^00000 1.00.1 11. 1 0 1^^0.01 ^^^ 01.^ ^ 1 1^^ ^.^1 1 0... 1 ^1 1^ ^ .01 ^ 1.. 1.1 ^0.0^ 0 1..01^^100000..0^1 1 ^ 1 ^^1111^ ^^0 ^ ^ 1 1000^.1 ^.^ . 00.. 1.1 0. 01. . 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\n", 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傳入函數f2if(s[p]==0) break;//如果當前字符為空字符,直接跳出p++;}return sum; }int main() {int x = f("12+18+5*4*3+10");printf("%d\n", x);return 0; }總結
以上是生活随笔為你收集整理的2017年第八届蓝桥杯C/C++ A组国赛 —— 第三题:表达式计算的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 2017年第八届蓝桥杯C/C++ A组国
- 下一篇: 2017年第八届蓝桥杯C/C++ A组国