栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

zoj 3683 Simple Language

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

zoj 3683 Simple Language

#include <iostream>#include <cstdio>#include <cstring>#include <string>#include <vector>#include <bitset>#include <algorithm>#include <sstream>#include <map>using namespace std;#define rep(i,a,b) for(int i = a; i < b; ++i)typedef long long ll;const int N = 2000;const int MOD = 1000000007;ll reg[N];int n;char s[20000];int True_V(char x) { if(x >= '0' && x <= '9') return x - '0'; if(x >='A' && x <= 'F') return x - 'A' + 10; if(x >='a' && x <= 'f') return x - 'a' + 10; cerr <<"True_V Error" << endl; return -1;}struct V { int sg; ll v; V() {} V(string s) { int t = 1; int bg = 0; if(s[bg] == '-'){ t = -1; ++bg; } if(s[bg] == '$') { sg = 0; ++bg; int bas = 10; v = 0; int len = s.size(); if(s[len-1] == 'D') { --len; } else if(s[len-1] == 'H') { --len; bas = 16; } else if(s[len-1] == 'B') { --len; bas = 2; } rep(i,bg,len) { v = v*bas + True_V(s[i]); } } else { sg = 1; int bas = 10; v = 0; int len = s.size(); if(s[len-1] == 'D') { --len; } else if(s[len-1] == 'H') { --len; bas = 16; } else if(s[len-1] == 'B') { --len; bas = 2; } rep(i,bg,len) { v = v*bas + True_V(s[i]); } } if(t < 0) v = -v; } ll value() { if(sg == 0) return reg[v]; else return v; }};struct T { int sg; V v[3]; string tag[3]; T() {}};vector<T> adj;int total_op = 0;map<string,int> dic;string op_set[] = {"SET", "ADD", "SUB", "MUL","DIV","MOD","SETTAG","GOTO","IF","PRINT"};int v_cnt[] = {2,3,3,3,3,3,1,1,3,1};const int op_num = 10;void deal(char *s) { int len = strlen(s); rep(i,0,len) { if(s[i] == ';') s[i] = ' '; if(s[i] == '#') { s[i] = 0; break; } } istringstream in(s); string a,b,c,d; T tmp; string op; V tv; while(in >> op) { //cout << op << endl; bool has = false; rep(i,0,op_num) { if(op == op_set[i]) { has = true; tmp.sg = i; if(i == 6) { in >> a; dic[a] = total_op; tmp.tag[0] = a; } else if(i == 7) { in >> a; tmp.tag[0] = a; } else if(i == 8) { in >> a; tmp.v[0] = V(a); in >> a; tmp.tag[0] = a; in >> a; tmp.tag[1] = a; } else { rep(j,0,v_cnt[i]) { in >> a; tmp.v[j] = V(a); } } break; } } if(!has) cerr << "OP find errer" << endl; adj.push_back(tmp); ++total_op; }}void run() { memset(reg, 0, sizeof(reg)); int cur = 0; T cur_op; while(cur < total_op) { cur_op = adj[cur]; if(cur_op.sg == 0) { if(cur_op.v[0].v) { reg[cur_op.v[0].v] = cur_op.v[1].value(); } } else if(cur_op.sg == 1) { if(cur_op.v[0].v) { reg[cur_op.v[0].v] = cur_op.v[1].value() + cur_op.v[2].value(); } } else if(cur_op.sg == 2) { if(cur_op.v[0].v) { reg[cur_op.v[0].v] = cur_op.v[1].value() - cur_op.v[2].value(); } } else if(cur_op.sg == 3) { if(cur_op.v[0].v) { reg[cur_op.v[0].v] = cur_op.v[1].value() * cur_op.v[2].value(); } } else if(cur_op.sg == 4) { if(cur_op.v[0].v) { reg[cur_op.v[0].v] = cur_op.v[1].value() / cur_op.v[2].value(); } } else if(cur_op.sg == 5) { if(cur_op.v[0].v) { reg[cur_op.v[0].v] = cur_op.v[1].value() % cur_op.v[2].value(); } } else if(cur_op.sg == 6) { } else if(cur_op.sg == 7) { cur = dic[cur_op.tag[0]]; continue; } else if(cur_op.sg == 8) { if(cur_op.v[0].value() > 0) cur = dic[cur_op.tag[0]]; else cur = dic[cur_op.tag[1]]; continue; } else if(cur_op.sg == 9) { printf("%lldn",cur_op.v[0].value()); } ++cur; }}int main() { while(cin >> n) { getchar(); total_op = 0; adj.clear(); dic.clear(); rep(i,0,n) { gets(s); deal(s); } run(); puts(""); }}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/380129.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号