Hero_Broom @ 2024-05-25 09:49:36
#include<bits/stdc++.h>
using namespace std;
struct _array{
string name;
int size;
int a[110]={0};
};
vector<_array> arr;
inline int powin10(int t){
int tmp=1;
while(t--) tmp*=10;
return tmp;
}
int _stoi(string s){
int sol=0;
for(int i=s.size()-1,j=0;i>=(s[0]=='-');i--,j++) sol+=(s[i]-'0')*powin10(j);
return (s[0]=='-'? -sol : sol);
}
bool isnum(string s){
for(int i=0;i<s.size();i++) if(!(s[i]>='0'&&s[i]<='9')) return 0;
return 1;
}
void outlist(){
printf("outlist:\n");
for(int i=0;i<arr.size();i++){
cout<<arr[i].name<<"\t"<<arr[i].size;
printf("\n\t");
for(int j=0;j<arr[i].size;j++) printf("%d ",arr[i].a[j]);
printf("\n");
}
printf("\n");
}
int find(string valuename){
for(int i=0;i<arr.size();i++) if(arr[i].name==valuename) return i;
return -1;
}
int getvalue(string expr){
if(isnum(expr)) return _stoi(expr);
// cout<<"expr="<<expr<<endl;
string tmp="",name="";
int p=0,o=expr.size()-1;
while(expr[p]!='[') name+=expr[p],p++;
if(find(name)==-1) return -1;
p++;
while(expr[o]!=']') o--;
o--;
for(int i=p;i<=o;i++) tmp+=expr[i];
// cout<<name<<"\ttmp="<<tmp<<"\n";
if(isnum(tmp)){
if(_stoi(tmp)<0||_stoi(tmp)>=arr[find(name)].size) return -1;
// printf("find(name)=%d\t_stoi(tmp)=%d\t%d\n",find(name),_stoi(tmp),arr[find(name)].a[_stoi(tmp)]);
return arr[find(name)].a[_stoi(tmp)];
}
else{
if(getvalue(tmp)<0||getvalue(tmp)>=arr[find(name)].size) return -1;
return arr[find(name)].a[getvalue(tmp)];
}
}
bool definition(string s){
string tmp="",name="";
int p=0;
while(s[p]!='[') name+=s[p],p++;
p++;
while(s[p]!=']') tmp+=s[p],p++;
if(isnum(tmp)){
if(_stoi(tmp)==-1) return -1;
arr.push_back({name,_stoi(tmp)});
}else{
if(getvalue(tmp)<0) return -1;
arr.push_back({name,getvalue(tmp)});
}
return 1;
}
int main(){
string cmd;
while(getline(cin,cmd)){
if(cmd.size()>5&&cmd.substr(0,4)=="int "){
cmd.erase(0,4);
if(definition(cmd)==-1) printf("-1\n"),exit(0);
}else if(cmd.size()>6&&cmd.substr(0,5)=="cout "){
cmd.erase(0,5);
if(getvalue(cmd)==-1) printf("-1\n"),exit(0);
printf("%d\n",getvalue(cmd));
}else{
if(cmd=="") exit(0);
string name="",tmp="";
int p=0,o=cmd.size()-1;
while(cmd[p]!='[') name+=cmd[p],p++;
p++;
while(cmd[o]!=']') o--;
o--;
for(int i=p;i<=o;i++) tmp+=cmd[i];
if(find(name)==-1) printf("-1\n"),exit(0);
// cout<<"give value:\tname="<<name<<"\ttmp="<<tmp<<"\tcmd="<<cmd<<endl;
if(getvalue(tmp)==-1||getvalue(tmp)>=arr[find(name)].size) printf("-1\n"),exit(0);
p++;
cmd.erase(0,p+2);
// cout<<"give value:\tname="<<name<<"\ttmp="<<tmp<<"\tcmd="<<cmd<<endl;
arr[find(name)].a[getvalue(tmp)]=getvalue(cmd);
}
// outlist();
}
}
by WuDiFengShu12345 @ 2024-07-23 13:44:38
看不懂,根本看不懂