Luogu C++ language | P1597 statement analysis

Learn C++ from a baby! Record the questions in the process of Luogu C++ learning and test preparation, and record every moment.

Attached is a summary post: Luogu Brush Questions C++ Language | Summary


[Title description]

A string of PASCAL language codes with a length not exceeding 255, and only assignment statements. The assignment can only be a one-digit number or a variable. The format of each assignment statement is [variable]:=[variable or one-digit integer]; . The unassigned variable value is 0 and  the values ​​of a , b , c are output  .

【enter】

A string of grammatical PASCAL language has only   three variables a , b , c , and only assignment statements. The assignment can only be a one-digit number or a variable. The value of an unassigned variable is 0.

【Output】

Output   the final values ​​of a , b , c .

【Input sample】

a:=3;b:=4;c:=5;

【Output sample】

3 4 5

[Detailed code explanation]

#include <bits/stdc++.h>
using namespace std;

int main()
{
    int a=0, b=0, c=0;
    string s;
    cin >> s;
    for (int i=0; i<s.length(); i++) {
        if (s[i]=='a' && s[i+1]==':' && s[i+2]=='=') {
            if (s[i+3]>='0' && s[i+3]<='9') a=s[i+3]-'0';
            if (s[i+3]=='b') a=b;
            if (s[i+3]=='c') a=c;
        }
        if (s[i]=='b' && s[i+1]==':' && s[i+2]=='=') {
            if (s[i+3]>='0' && s[i+3]<='9') b=s[i+3]-'0';
            if (s[i+3]=='a') b=a;
            if (s[i+3]=='c') b=c;
        }
        if (s[i]=='c' && s[i+1]==':' && s[i+2]=='=') {
            if (s[i+3]>='0' && s[i+3]<='9') c=s[i+3]-'0';
            if (s[i+3]=='a') c=a;
            if (s[i+3]=='b') c=b;
        }
    }
    cout << a << " " << b << " " << c;
    return 0;
}

【operation result】

a:=3;b:=4;c:=5;
3 4 5

Guess you like

Origin blog.csdn.net/guolianggsta/article/details/132773973