1684

来源:互联网 发布:mac appstore 太慢 编辑:程序博客网 时间:2024/06/06 10:48
#include <iostream>#include <cstdio>#include <string>#include <cctype>using namespace std;struct node{node(): has_dcl(false), has_ref(false), value(0){};bool has_dcl: 1;bool has_ref: 1;int value: 20;};node map[128];const int N = 101;void input(int &len, string(&statement)[N]){char tmp[1000];cin >> len;cin.get();for(int i = 1; i <= len; ++i){cin.getline(tmp, sizeof(tmp));const char *p = tmp;for(; ' ' == *p; ++p);statement[i].assign(p);}}int main(){int ks, len;string statement[N];char tmp[8];cin >> ks;for(int i = 1; i <= ks; ++i){input(len, statement);printf("%d\n", i);memset(map, 0, sizeof(map));char c;for(int flow = 1; true; ){const char *exe = statement[flow].c_str();sscanf(exe, "%[^= ]", tmp);if(0 == _stricmp(tmp, "DCL")){sscanf(exe + 3, "%*[ ]%c", &c);if(!map[c].has_dcl || map[c].has_ref){map[c].has_dcl = true;map[c].has_ref = false;map[c].value = 0;}else{printf("%d %d\n", flow, 1);}++flow;}else if(0 == _stricmp(tmp, "GOTO")){sscanf(exe + 4, "%s", tmp);if(isdigit(tmp[0]) != 0){flow = atoi(tmp);}else{c = tmp[0];if(map[c].has_dcl){map[c].has_ref = true;if(map[c].value > 0){sscanf(exe, "%*s%*s%d", &flow);}else{++flow;}}else{printf("%d %d\n", flow, 2);++flow;}}}else if(0 == _stricmp(tmp, "INC")){sscanf(exe + 3, "%*[ ]%c", &c);if(map[c].has_dcl){map[c].has_ref = true;++(map[c].value);}else{printf("%d %d\n", flow, 2);}++flow;}else if(0 == _stricmp(tmp, "DEC")){sscanf(exe + 3, "%*[ ]%c", &c);if(map[c].has_dcl){map[c].has_ref = true;--(map[c].value);}else{printf("%d %d\n", flow, 2);}++flow;}else if(0 == _stricmp(tmp, "END")){break;}else{c = tmp[0];if(map[c].has_dcl){map[c].has_ref = true;int num;sscanf(exe, "%*[^= ]%*[^0-9]%d", &num);map[c].value = num;}else{printf("%d %d\n", flow, 2);}++flow;}}}return 0;}

 
原创粉丝点击