{"id":783,"date":"2025-05-30T16:46:32","date_gmt":"2025-05-30T08:46:32","guid":{"rendered":"https:\/\/www.notown.top\/?p=783"},"modified":"2025-05-30T16:46:40","modified_gmt":"2025-05-30T08:46:40","slug":"%e9%a2%86%e5%9f%9f%e8%a7%84%e5%88%99%e6%a8%a1%e5%bc%8f%ef%bc%88%e8%a7%a3%e6%9e%90%e5%99%a8%ef%bc%89","status":"publish","type":"post","link":"https:\/\/www.notown.top\/?p=783","title":{"rendered":"\u9886\u57df\u89c4\u5219\u6a21\u5f0f\uff08\u89e3\u6790\u5668\uff09"},"content":{"rendered":"<blockquote><p>\n  \u9886\u57df\u89c4\u5219\u6a21\u5f0f<br \/>\n  \u5728\u7279\u5b9a\u9886\u57df\u4e2d\uff0c\u67d0\u4e9b\u53d8\u5316\u867d\u7136\u9891\u7e41\uff0c\u4f46\u53ef\u4ee5\u62bd\u8c61\u4e3a\u67d0\u79cd\u89c4\u5219\u3002\u8fd9\u65f6\u5019\uff0c\u7ed3\u5408\u7279\u5b9a\u9886\u57df\uff0c\u5c06\u95ee\u9898\u62bd\u8c61\u4e3a\u8bed\u6cd5\u89c4\u5219\uff0c\u4ece\u800c\u7ed9\u51fa\u5728\u8be5\u9886\u57df\u4e0b\u7684\u4e00\u822c\u6027\u89e3\u51b3\u65b9\u6848\u3002<br \/>\n  \u5c5e\u4e8e\u9886\u57df\u89c4\u5219\u6a21\u5f0f\u7684\u6709Interpreter\n<\/p><\/blockquote>\n<pre><code class=\"language-cpp line-numbers\">class Expression{\npublic:\n    virtual int interpreter(map&lt;char, int&gt; var) = 0;\n    virtual ~Expression() {}\n};\n\n\/\/ \u53d8\u91cf\u8868\u8fbe\u5f0f\nclass VarExpression : public Expression{\n    char key;\npublic:\n    VarExpression(const char &amp;key){\n        this-&gt;key = key;\n    }\n\n    int interpreter(map&lt;char, int&gt; var) override{\n        return var[key];\n    }\n}\n\n\/\/ \u7b26\u53f7\u8868\u8fbe\u5f0f\nclass SymbolExpression : public Expression{\nprotected:\n    Expression *left;\n    Expression *right;\n\npublic:\n    SymbolExpression(Expression *x, Expression *y) : left(x), right(y) {}\n}\n\nclass AddExpression : public SymbolExpression {\npublic:\n    AddExpression(Expression *x, Expression *y) : SymbolExpression(x,y) {}\n\n    int interpreter(map&lt;char, int&gt; var){\n        return left-&gt;interpreter(var) + right-&gt;interpreter(var);\n    }\n}\n\nclass SubExpression : public SymbolExpression{\npublic:\n    SubExpression(Expression *x, Expression *y) : SymbolExpression(x,y) {}\n\n    int interpreter(map&lt;char, int&gt; var){\n        return left-&gt;interpreter(var) - right-&gt;interpreter(var);\n    }\n};\n\nExpression *analyse(string expStr){\n    stack&lt;Expression *&gt; expStack; \/\/ \u8fd9\u91cc\u7684\u6808\u64cd\u4f5c\u5c5e\u5b9e\u6ca1\u4ec0\u4e48\u610f\u4e49\uff0c\u53ea\u8981\u4fdd\u7559\u4e00\u4efd\u6700\u9876\u4e0a\u7684\u5143\u7d20\u5373\u53ef\u3002\n    Expression *left = nullptr;\n    Expression *right = nullptr;\n    for(int i = 0; i &lt; expStr.size(); ++i){\n        switch( expStr[i] ){\n            case '+':\n                left = expStack.top();\n                right = new VarExpression(expStr[++i]);\n                expStack.push(new AddExpression(left, right) );\n                break;\n            case '-':\n                left = expStack.top();\n                right = new VarExpression(expStr[++i]);\n                expStack.push(new SubExpressionn(left, right) );\n                break;\n            default:\n                expStack.push(new VarExpression(expStr[i]));\n        }\n    }\n    Expression *expression = expStack.top();\n    return expression;\n}\n\nvoid release(Expression *exp){\n    \/\/ \u91ca\u653e\u8868\u8fbe\u5f0f\u6811\u7684\u8282\u70b9\u5185\u5b58\n}\n\nint main(){\n    string expStr = \"a+b-c+d\"; \/\/ \u8fd9\u4e2a\u53ef\u4ee5\u4efb\u610f\u52a0\u51cf\u4e0b\u53bb \n    map&lt;char, int&gt; var;\n    var.insert(make_pair('a', 5) );\n    var.insert(make_pair('b', 2) );\n    var.insert(make_pair('c', 1) );\n    var.insert(make_pair('d', 6) );\n\n    Expression *expression = analyse(expStr);   \/\/ \u6784\u5efa\u8868\u8fbe\u5f0f\u6811\uff0c\u5e76\u8fd4\u56de\u6839\u8282\u70b9\n    int result = expression-&gt;interpreter(var);  \/\/ \u6839\u8282\u70b9\u5f00\u59cb\u89e3\u6790\uff0c\u4f1a\u4f7f\u7528\u540e\u5e8f\u904d\u5386\u7684\u65b9\u5f0f\u8ba1\u7b97\u7ed3\u679c\u3002\n    cout &lt;&lt; result &lt;&lt; endl;\n}\n\n<\/code><\/pre>\n<p>\u8981\u70b9\u603b\u7ed3\uff1a<br \/>\n&#8211; Interpreter\u6a21\u5f0f\u7684\u5e94\u7528\u573a\u5408\u662fInterpreter\u6a21\u5f0f\u5e94\u7528\u4e2d\u7684\u96be\u70b9\uff0c\u53ea\u6709\u6ee1\u8db3\u201c\u4e1a\u52a1\u89c4\u5219\u9891\u7e41\u53d8\u5316\uff0c\u4e14\u7c7b\u4f3c\u7684\u7ed3\u6784\u4e0d\u65ad\u91cd\u590d\u51fa\u73b0\uff0c\u5e76\u4e14<strong>\u5bb9\u6613\u62bd\u8c61\u4e3a\u8bed\u6cd5\u89c4\u5219<\/strong>\u7684\u95ee\u9898\u201d\u624d\u9002\u5408\u4f7f\u7528Interpreter\u6a21\u5f0f<br \/>\n&#8211; \u4f7f\u7528Interpreter\u6a21\u5f0f\u6765\u8868\u793a\u6587\u6cd5\u89c4\u5219\uff0c\u4ece\u800c\u53ef\u4ee5\u4f7f\u7528\u9762\u5411\u5bf9\u8c61\u6280\u5de7\u6765\u65b9\u4fbf\u5730\u201c\u6269\u5c55\u201d\u6587\u6cd5<br \/>\n&#8211; Interpreter\u6a21\u5f0f\u6bd4\u8f83<strong>\u9002\u5408\u7b80\u5355\u7684\u6587\u6cd5\u8868\u793a<\/strong>\uff0c\u5bf9\u4e8e\u590d\u6742\u7684\u6587\u6cd5\u8868\u793a\uff0cInterpreter\u6a21\u5f0f\u4f1a\u4ea7\u751f\u6bd4\u8f83\u5927\u7684\u7c7b\u5c42\u6b21\u7ed3\u6784\uff0c\u9700\u8981\u6c42\u52a9\u4e8e\u8bed\u6cd5\u5206\u6790\u751f\u6210\u5668\u8fd9\u6837\u7684\u6807\u51c6\u5de5\u5177<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u9886\u57df\u89c4\u5219\u6a21\u5f0f \u5728\u7279\u5b9a\u9886\u57df\u4e2d\uff0c\u67d0\u4e9b\u53d8\u5316\u867d\u7136\u9891\u7e41\uff0c\u4f46\u53ef\u4ee5\u62bd\u8c61\u4e3a\u67d0\u79cd\u89c4\u5219\u3002\u8fd9\u65f6\u5019\uff0c\u7ed3\u5408\u7279\u5b9a\u9886\u57df\uff0c\u5c06\u95ee\u9898\u62bd\u8c61\u4e3a\u8bed\u6cd5\u89c4\u5219&hellip;<a href=\"https:\/\/www.notown.top\/?p=783\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">\u9886\u57df\u89c4\u5219\u6a21\u5f0f\uff08\u89e3\u6790\u5668\uff09<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_import_markdown_pro_load_document_selector":0,"_import_markdown_pro_submit_text_textarea":"","footnotes":""},"categories":[22],"tags":[],"class_list":["post-783","post","type-post","status-publish","format-standard","hentry","category-22"],"_links":{"self":[{"href":"https:\/\/www.notown.top\/index.php?rest_route=\/wp\/v2\/posts\/783","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.notown.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.notown.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.notown.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.notown.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=783"}],"version-history":[{"count":1,"href":"https:\/\/www.notown.top\/index.php?rest_route=\/wp\/v2\/posts\/783\/revisions"}],"predecessor-version":[{"id":784,"href":"https:\/\/www.notown.top\/index.php?rest_route=\/wp\/v2\/posts\/783\/revisions\/784"}],"wp:attachment":[{"href":"https:\/\/www.notown.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=783"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.notown.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=783"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.notown.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=783"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}