编译器——简单版

· · 个人记录

#include<bits/stdc++.h> 
#define pre(i, l, r)    for(int i = (l); i <= (r); ++i)
#define fpre(i, r, l)   for(int i = (r); i >= (l); --i)
#define fr(i, a) for(int _ = (a).size(), i = 0; i < _; ++i)
#define ll long long

using namespace std;

const int maxn = 2e5 + 5;
const int inf = 1e9;

char gc(void){   static char *p1, *p2, buf[1000000];return (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1000000, stdin), p1 == p2) ? EOF : *p1++);}
template<typename T> inline void S(T &x){x = 0;int f = 1;char ch = gc();while(!isdigit(ch)){if(ch == '-'){
f = -1;}ch = gc();};    while(isdigit(ch))  x = x * 10 + (ch ^ 48), ch = gc();x *= f;}
template<typename type, typename... T>
inline void S(type &x, T&...y)  {S(x); S(y...);}
template<typename T = int>T S(void) {T x;S(x);return x;}

map<string,int> f;
int cnt = 0;
ll value[maxn];

vector<any> c;

string st[maxn] = 
{

};

string str(string s, int x)
{
    string ans = "";
    int sz = s.size();
    while(x < sz && s[x] == ' ')    x++;
    while(x < sz && s[x] != ' ')    ans += s[x++];
    return ans;
}

ll strdigit(string s, int x)
{
    ll ans = 0;
    int sz = s.size();
    while(x < sz && !isdigit(s[x])) x++;
    while(x < sz && isdigit(s[x]))  ans = ans * 10 + (s[x++] ^ 48);
    return ans; 
}

int main()
{
    int n = 0;
    while(st[n] != "")  ++n;
    if(!n)
    {
        int cnt = 1;
        printf("%4d|", cnt++);
        while(getline(cin, st[n++]))
        {
            printf("%4d|", cnt++);
        }
        n--;
    }
    puts("Please Wating...");
    puts("--------------------------------");
    pre(i, 1, n)
    {
        string s = st[i - 1];
        string op = str(s, 0);
        int sz = s.size();
        if(op == "int")
        {
            string t = str(s, 3);
            f[t] = ++cnt;
            int tt = 3;
            while(tt < sz && !isdigit(s[tt]))   tt++;
            value[cnt] = strdigit(s, tt);
        }
        else
            if(op == "write")
                printf("%lld\n", value[f[str(s, 5)]]);
    } 
    return 0;
}

例如:

int a 5
int b 10
int c 5
write a
write b
write c

记得再另起一行结束符 Ctrl+Z或者其他,不同电脑不同