Calculation of simple C expressions by using the WordPress 327

Source: Internet
Author: User

Question: Give a c expression and calculate its value. The operands have only 26 letters, and the initial values from A to Z are 1 to 26. The operators have two + and two-digit-, which is the same as normal meaning, then there is a bit of auto-increment ++ and a bit of auto-increment---, both of which have prefix and suffix, which are the same as what we normally understand. Then, the expression value is calculated and the final value of each variable in the formula is given. By the way, each variable appears only once in the expression.

Train of Thought: at the end of the question, I will also give a train of thought, that is, remove all one-bit operators, namely the auto-increment and auto-subtraction operators, and then auto-increment or auto-Subtract variables with prefix operators, then calculate the value of the expression, and then add or subtract the variable with the suffix operator. Here, you only need to search for the auto-increment and auto-increment operators in the input string. If you encounter a prefix, you can directly modify the corresponding variable value and mark the suffix. Because the question does not mention spaces, start to use gets input. The result is wa, indicating that the auto-increment or auto-subtraction operators may have spaces in the middle, then there may be spaces between the auto-increment or auto-subtraction operator and the variable modified. (It may be the former, the latter, or both). Therefore, getchar filters out spaces directly. But you have to input the expression as it is, so you have to save it again.

Note: space conditions. Spaces may exist in multiple locations.

Tucao: This question seems quite simple, but in the topic in the White Book, the passing rate of the two questions seems to be the lowest, and I think it's hard ~~~~ Alas, data is untrusted ~~ Do not scare yourself ~~

Sleeping in time at night ~ Even if you occasionally watch a wonderful game until one or two o'clock, you can sleep after it's over. Don't break through the jar and fall down. If you get to sleep at four or five o'clock, you can't take a rest ~~ Sleep is not good, nothing can be done, everything is irrelevant, and even movies are too lazy to watch, life will be worse ~

Code:

# Include <stdio. h> # include <string. h> # define maxn 120 char exp [maxn]; char ysexp [maxn]; int ZM [30]; int Hz [30]; bool flag [30]; int main () {// while (fgets (exp, MAXN-1, stdin )! = NULL) // while (gets (exp )! = NULL) Char ch; while (CH = getchar ())! = EOF) {int CNT = 0; int yscnt = 0; do {ysexp [yscnt ++] = CH; If (Ch! = '') Exp [CNT ++] = CH;} while (CH = getchar ())! = '\ N'); ysexp [yscnt] =' \ 0'; exp [CNT] = '\ 0'; printf ("expression: % s \ n ", ysexp); For (INT I = 0; I <26; ++ I) {ZM [I] = I + 1; Hz [I] = 0; flag [I] = 0;} Char * s = exp; while (S = strstr (S, "++ "))! = NULL) {If (* (S + 2) = '\ 0' | * (S + 2) ='-'| * (S + 2) = '') {// The suffix Hz [* (S-1)-'a'] ++; * s =''; * (S + 1) = '';} else {// prefix ZM [* (S + 2)-'a'] ++; * s = ''; * (S + 1) = '';} // s ++;} s = exp; while (S = strstr (S ,"--"))! = NULL) {If (* (S + 2) = '\ 0' | * (S + 2) =' + '| * (S + 2) = '') {Hz [* (S-1)-'a'] --; * s =''; * (S + 1) = '';} else {ZM [* (S + 2)-'a'] --; * s = ''; * (S + 1) = '';} // s ++;} // calculate S = exp; while (* S <'A' | * s> 'Z') s ++; // until * exp is a lowercase letter int val = ZM [* s-'a']; flag [* s-'a'] = 1; char C; while (C = * s )! = '\ 0') {If (C =' + ') {While (* S <'A' | * s> 'Z') s ++; // until * exp is a lowercase letter Val + = ZM [* s-'a']; flag [* s-'a'] = 1 ;} else if (C = '-') {While (* S <'A' | * s> 'Z') s ++; val-= ZM [* s-'a']; flag [* s-'a'] = 1;} s ++ ;} printf ("value = % d \ n", Val); For (INT I = 0; I <26; ++ I) if (flag [I]) {ZM [I] + = Hz [I]; printf ("% C = % d \ n", 'A' + I, ZM [I]);} return 0 ;}


Calculation of simple C expressions by using the WordPress 327

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.