代码加密

· · 个人记录

#include<bits/stdc++.h>
using namespace std;
char nam[105];
string s,z,b[1000005],e;
char a[1000005],d[1000005];
map<string,int> hsh;
set<string> S;
int tot;
int cnt=0;
bool isA(char x){
    return (x>='a'&&x<='z')||(x>='A'&&x<='Z')||(x>='0'&&x<='9')||x=='_'||x=='.';
}
bool isF(char x){
    return !isA(x);
}
void name(){
    printf("Please enter the program\'s name\n\n");
    scanf("%s",nam);
    freopen("result.cpp","w",stdout);
}
int n;
void cpy(){
    b[++tot]=z;
    n=z.size();
    for(int i=0;i<n;i++) a[i]=z[i];
}
void insert(){
    if(z[0]=='#') return;
    int m=0;
    if(!n) return;
    for(int i=0;i<n;){
        s.clear();
        int j=0;
        if(a[i]==34){
            i++;
            s.push_back(34);
            while(i<n&&a[i]!=34){
                d[m++]=a[i];
                s.push_back(a[i]);
                i++;
            }
            i++;
            s.push_back(34);
            int x=hsh[s];
            if(!x) hsh[s]=++cnt,S.insert(s);
            continue;
        }
        if(a[i]==39){
            i++;
            s.push_back(39);
            while(i<n&&a[i]!=39){
                z[j++]=a[i];
                s.push_back(a[i]);
                i++;
            }
            i++;
            s.push_back(39);
            int x=hsh[s];
            if(!x) hsh[s]=++cnt,S.insert(s);
            continue;
        }
        if(i<n&&a[i]!=34&&a[i]!=39&&!isA(a[i])){
            while(i<n&&a[i]!=34&&a[i]!=39&&!isA(a[i])){
                s.push_back(a[i]);
                int x=hsh[s];
                if(!x) hsh[s]=++cnt,S.insert(s);
                i++;
            }
            int x=hsh[s];
            if(!x) hsh[s]=++cnt,S.insert(s);
            continue;
        }
        while(i<n&&a[i]!=34&&a[i]!=39&&isA(a[i])){
            z[j++]=a[i];
            s.push_back(a[i]);
            i++;
        }
        int x=hsh[s];
        if(!x) hsh[s]=++cnt,S.insert(s);
    }
}
void code(){
    freopen(nam,"r",stdin);
    for(int l=1;getline(cin,z);l++){
        cpy();
        insert();
    }
}
void checkdef(){
    for(int l=1;l<=tot;l++){
        if(b[l][0]=='#') cout<<b[l]<<endl;
        else continue;
    }
}
void ps(int x){
    while(x){
        putchar('a'+x%26);
        x/=26;
    }
    putchar('_');
    putchar(' ');
}
void def(){
    for(set<string,int>::iterator it=S.begin();it!=S.end();++it){
        cout<<"#define ";
        ps(hsh[*it]);
        cout<<*it<<endl;
    }
}
void wl(int l){
    n=b[l].size();
    for(int i=0;i<n;i++) a[i]=b[l][i];
    if(b[l][0]=='#') return;
    if(!n) return;
    for(int i=0;i<n;){
        s.clear();
        int j=0;
        int m=0;
        if(a[i]==34){
            i++;
            s.push_back(34);
            while(i<n&&a[i]!=34){
                d[m++]=a[i];
                s.push_back(a[i]);
                i++;
            }
            i++;
            s.push_back(34);
            ps(hsh[s]);
            continue;
        }
        if(a[i]==39){
            i++;
            s.push_back(39);
            while(i<n&&a[i]!=39){
                z[j++]=a[i];
                s.push_back(a[i]);
                i++;
            }
            i++;
            s.push_back(39);
            ps(hsh[s]);
            continue;
        }
        if(i<n&&a[i]!=34&&a[i]!=39&&!isA(a[i])){
            while(i<n&&a[i]!=34&&a[i]!=39&&!isA(a[i])){
                s.push_back(a[i]);
                int x=hsh[s];
                if(!x) hsh[s]=++cnt,S.insert(s);
                i++;
            }
            ps(hsh[s]);
            continue;
        }
        while(i<n&&a[i]!=34&&a[i]!=39&&isA(a[i])){
            d[m++]=a[i];
            s.push_back(a[i]);
            i++;
        }
        ps(hsh[s]);
    }
}
void write(){
    for(int l=1;l<=tot;l++){
        wl(l);
    }
}
int main(){
    name();
    code();
    checkdef();
    def();
    write();
    return 0;
}