{"id":2237,"date":"2009-10-04T11:44:21","date_gmt":"2009-10-04T11:44:21","guid":{"rendered":"http:\/\/192.168.0.71:9090\/?p=1939"},"modified":"2009-10-04T11:44:21","modified_gmt":"2009-10-04T11:44:21","slug":"programming-challenges-%ec%9d%b8%ed%84%b0%ed%94%84%eb%a6%ac%ed%84%b0interpreter","status":"publish","type":"post","link":"https:\/\/talsu.net\/?p=2237","title":{"rendered":"Programming Challenges &#8211; \uc778\ud130\ud504\ub9ac\ud130(Interpreter)"},"content":{"rendered":"<p><span style=\"font-size: 12pt; font-weight: bold;\"><a title=\"[http:\/\/programming-challenges.com\/pg.php?page=downloadproblem&amp;probid=110106&amp;format=html]\ub85c \uc774\ub3d9\ud569\ub2c8\ub2e4.\" target=\"_blank\" href=\"http:\/\/programming-challenges.com\/pg.php?page=downloadproblem&amp;probid=110106&amp;format=html\">\ubb38\uc81c &lt;- \ud074\ub9ad<\/a><\/span><\/p>\n<p>\uc640\uc6b0~ \uc774\uac70 \uc7ac\ubbf8 \uc788\ub294\ub370 \u314e\u314e\u314e \ub808\ubca8\uc740 2\uc778\ub370 \ud06c\uac8c \uc5b4\ub835\uc9c0\ub294 \uc54a\uc740\ub4ef..<br \/>\n\uc791\ub3d9\uc740 \uc798 \ub418\ub294\ub370 \ucd9c\ub825 \ud615\ud0dc\uac00 \ub9d8\uc5d0 \uc548\ub4e0\ub2e4\uace0 \ub85c\ubd07\ub2d8\uc774 \uadf8\ub798\uc11c \ucd9c\ub825 \ubd80\ubd84\uc740 \ucc45\uc744 \uc880 \ucc38\uace0 \u314e\u314e<\/p>\n<pre class=\"lang:c++ decode:true\">#include <iostream>\n#include <stdlib.h>\nusing namespace std;\n\nclass Memory {\nprivate:\n\tint val;\npublic:\n\tMemory() :\n\t\tval(0) {\n\t}\n\tint getVal() {\n\t\treturn val;\n\t}\n\tvoid getArg(int * arg) {\n\t\targ[0] = val \/ 100;\n\t\targ[1] = (val \/ 10) % 10;\n\t\targ[2] = val % 10;\n\t}\n\tMemory& operator+=(const int& rhs) {\n\t\tval += rhs;\n\t\tif (val > 999)\n\t\t\tval %= 1000;\n\t\treturn *this;\n\t}\n\tMemory& operator+=(const Memory& rhs) {\n\t\t*this += rhs.val;\n\t\treturn *this;\n\t}\n\tMemory& operator*=(const int& rhs) {\n\t\tval *= rhs;\n\t\tif (val > 999)\n\t\t\tval %= 1000;\n\t\treturn *this;\n\t}\n\tMemory& operator*=(const Memory& rhs) {\n\t\t*this *= rhs.val;\n\t\treturn *this;\n\t}\n\tMemory& operator=(const Memory& rhs) {\n\t\tval = rhs.val;\n\t\treturn *this;\n\t}\n\tMemory& operator=(const int& inVal) {\n\t\tval = inVal;\n\t\treturn *this;\n\t}\n};\n\nclass Interpreter {\nprivate:\n\tMemory registers[10];\n\tMemory ram[1000];\n\tint counter;\npublic:\n\tInterpreter() :\n\t\tcounter(0) {\n\t}\n\n\tvoid reset() {\n\t\tfor (int i = 0; i < 10; i++)\n\t\t\tregisters[i] = 0;\n\t\tfor (int i = 0; i < 1000; i++)\n\t\t\tram[i] = 0;\n\t\tcounter = 0;\n\t}\n\n\tvoid input(char* command) {\n\t\tram[counter++] = atoi(command);\n\t}\n\n\tint execute() {\n\t\tbool doing = true;\n\t\tint addr = 0;\n\t\tint arg[3];\n\t\tint numberOfExecuted = 0;\n\t\twhile (doing) {\n\t\t\tram[addr].getArg(arg);\n\t\t\taddr++;\n\t\t\tnumberOfExecuted++;\n\n\t\t\tswitch (arg[0]) {\n\t\t\tcase 0:\n\t\t\t\tif (registers[arg[2]].getVal() != 0)\n\t\t\t\t\taddr = registers[arg[1]].getVal();\n\t\t\t\tbreak;\n\t\t\tcase 1:\n\t\t\t\tdoing = false;\n\t\t\t\tbreak;\n\t\t\tcase 2:\n\t\t\t\tregisters[arg[1]] = arg[2];\n\t\t\t\tbreak;\n\t\t\tcase 3:\n\t\t\t\tregisters[arg[1]] += arg[2];\n\t\t\t\tbreak;\n\t\t\tcase 4:\n\t\t\t\tregisters[arg[1]] *= arg[2];\n\t\t\t\tbreak;\n\t\t\tcase 5:\n\t\t\t\tregisters[arg[1]] = registers[arg[2]];\n\t\t\t\tbreak;\n\t\t\tcase 6:\n\t\t\t\tregisters[arg[1]] += registers[arg[2]];\n\t\t\t\tbreak;\n\t\t\tcase 7:\n\t\t\t\tregisters[arg[1]] *= registers[arg[2]];\n\t\t\t\tbreak;\n\t\t\tcase 8:\n\t\t\t\tregisters[arg[1]] = ram[registers[arg[2]].getVal()];\n\t\t\t\tbreak;\n\t\t\tcase 9:\n\t\t\t\tram[registers[arg[2]].getVal()] = registers[arg[1]];\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\t\treturn numberOfExecuted;\n\t}\n\n};\n\nint main() {\n\tInterpreter inter;\n\tint numberOfCase;\n\tchar command[10];\n\n\tcin >> numberOfCase;\n\tgets(command);\n\tgets(command);\n\tfor (int caseTime = 0; caseTime < numberOfCase; caseTime++) {\n\t\tfor (int i = 0; gets(command) &#038;&#038; *command; ++i) \/\/\uc785\ub825\ubd80\ubd84\uc740 \uc5d0\ub7ec\ub098\uc11c \ucc45 \ucc38\uace0\n\t\t\tinter.input(command);\n\t\tif (caseTime > 0)\n\t\t\tputchar('n');\n\t\tcout << inter.execute() << endl; \/\/\ucd9c\ub825\ud558\uace0\n\t\tinter.reset(); \/\/\ub9ac\uc14b\ud558\uae30\n\t}\n\n\treturn 0;\n}<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\ubb38\uc81c &lt;- \ud074\ub9ad \uc640\uc6b0~ \uc774\uac70 \uc7ac\ubbf8 \uc788\ub294\ub370 \u314e\u314e\u314e \ub808\ubca8\uc740 2\uc778\ub370 \ud06c\uac8c \uc5b4\ub835\uc9c0\ub294 \uc54a\uc740\ub4ef.. \uc791\ub3d9\uc740 \uc798 \ub418\ub294\ub370 \ucd9c\ub825 \ud615\ud0dc\uac00 \ub9d8\uc5d0 \uc548\ub4e0\ub2e4\uace0 \ub85c\ubd07\ub2d8\uc774 \uadf8\ub798\uc11c \ucd9c\ub825 \ubd80\ubd84\uc740 \ucc45\uc744 \uc880 \ucc38\uace0 \u314e\u314e #include #include using namespace std; class Memory { private: int val; public: Memory() : val(0) { } int getVal() { return val; } void getArg(int * arg) [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"spay_email":""},"categories":[9],"tags":[49,67,144,203,385,414],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/pXV5a-A5","_links":{"self":[{"href":"https:\/\/talsu.net\/index.php?rest_route=\/wp\/v2\/posts\/2237"}],"collection":[{"href":"https:\/\/talsu.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/talsu.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/talsu.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/talsu.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2237"}],"version-history":[{"count":0,"href":"https:\/\/talsu.net\/index.php?rest_route=\/wp\/v2\/posts\/2237\/revisions"}],"wp:attachment":[{"href":"https:\/\/talsu.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2237"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/talsu.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2237"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/talsu.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2237"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}