boolget(stack<string>& st,int& num){ while (!st.empty()) { if (num >= dat[st.top()].size) returnfalse; if (!dat[st.top()].dim.count(num)) returnfalse; num = dat[st.top()].dim[num]; st.pop(); } returntrue; }
booldeclare(){//定义数组 for (auto& r : line) if (r == '[' || r == ']') r = ' '; istringstreamin(line); string na; int nu; in >> na >> nu; dat[na].size = nu; return nu >= 0; }
boolsolve(){//处理赋值 for (auto& r : line) if (r == '[' || r == ']' || r == '=') r = ' '; stack<string> stl, str; int numl, numr; istringstreamin(line); string now,sl; in >> sl; while (in >> now && now[0] > '9') stl.push(now); istringstreaminl(now);inl >> numl; while (in >> now && now[0] > '9') str.push(now); istringstreaminr(now);inr >> numr; if (!get(stl, numl) || !get(str, numr)) returnfalse; if (numl >= dat[sl].size) returnfalse; dat[sl].dim[numl] = numr; returntrue; }
intmain() { //freopen("in.txt", "r", stdin);// while (cin>>line && line[0] != '.') { unsigned flag = 0, linenum = 0; dat.clear(); do { ++linenum; if (flag) continue; if (!(line.find('=') == string::npos ? declare() : solve())) flag = linenum; } while (cin>>line && line[0] != '.'); cout << flag << '\n'; } return0; }
intget(){//从qu中get当前index的值,通过递归实现 string now = qu.front(); qu.pop(); if (now[0] > '9') {//若为变量名,说明有嵌套 int n = get();//得到当前变量的index qu.pop(); if (n >= dat[now].len || n < 0) return-1;//数组越界 if (qu.empty() || qu.front()[0] == '}') {//不是赋值,直接返回 if (!dat[now].def.count(n)) return-1;//没初始化 return dat[now].def[n]; } return dat[now].def[n] = get();//赋值 } int num;//若为数字 istringstreamiin(now); iin >> num; return num; }
booldeclare(){//定义数组 for (auto& r : line) if (r == '[' || r == ']') r = ' '; istringstreamin(line); string na; int nu; in >> na >> nu; dat[na].len = nu; return nu >= 0; }
boolsolve(){//预处理当前行 for (auto& r : line) if (r == '[' || r == '=') r = ' '; istringstreamin(line); string now; int num; while (in >> now) { if (now[0] > '9') qu.push(now); else { int i = 0; for (auto& r : now) if (r == ']') r = ' ', ++i; qu.push(now); while (i--) qu.push("}");//为什么要用}不用],一开始想的是}的ASCII是125,而]夹在大小写字母之间。然而后来发现并没有什么卵用 } } return get() >=0; }
intmain() { //freopen("in.txt", "r", stdin);// while (getline(cin, line) && line[0] != '.') { unsigned flag = 0, linenum = 0; dat.clear(); while (!qu.empty()) qu.pop();//queue不自带clear()的?! do { ++linenum; if (flag) continue; if (!(line.find('=') == string::npos ? declare() : solve())) flag = linenum; } while (getline(cin, line) && line[0] != '.'); cout << flag << '\n'; } return0; }