{"id":358,"date":"2011-08-23T14:48:00","date_gmt":"2011-08-23T06:48:00","guid":{"rendered":"http:\/\/wangkaixuan.tech\/?p=358"},"modified":"2020-06-06T14:49:43","modified_gmt":"2020-06-06T06:49:43","slug":"%e5%9f%ba%e4%ba%8e%e5%a0%86%e6%a0%88%e7%9a%84%e8%ae%a1%e7%ae%97%e5%99%a8%e4%bb%a3%e7%a0%81","status":"publish","type":"post","link":"http:\/\/www.wangkaixuan.tech\/?p=358","title":{"rendered":"\u57fa\u4e8e\u5806\u6808\u7684\u8ba1\u7b97\u5668\u4ee3\u7801"},"content":{"rendered":"\n<pre class=\"wp-block-code\"><code>#include&lt;iostream>\n#include&lt;cstdio>\n#include&lt;cstring>\n#include&lt;string>\n#include&lt;stack>\n#include&lt;cmath>\n\nusing namespace std;\n\nstack&lt;double> b;   \/\/\u64cd\u4f5c\u6570\u6808\n\nstack&lt;int> c;   \/\/\u64cd\u4f5c\u7b26\u6808\n\nint main()\n{\n\tputs(\n\t\t\t\"**********************************************************************\");\n\tputs(\n\t\t\t\"*                                                                    *\");\n\tputs(\"*                  \u7b80     \u6613     \u8ba1     \u7b97      \u5668                   *\");\n\tputs(\n\t\t\t\"*                                                                    *\");\n\tputs(\"*                       \u8f6f\u4ef62\u73ed      \u738b\u51ef\u65cb                          *\");\n\tputs(\n\t\t\t\"**********************************************************************\\n\\n\");\n\tstring a;\n\tlong l, i, j;\n\tdouble sum, t, sum1;\n\tdouble deal(int);\n\trecycle:   \/\/\u5faa\u73af\u8d77\u70b9\n\twhile (getline(cin, a))\n\t{\n\t\tl = a.size();   \/\/\u5f97\u5230\u7b97\u5f0f\n\t\tif (l == 0)   \/\/\u7279\u6b8a\u60c5\u51b5\u5904\u7406\n\t\t{\n\t\t\tprintf(\"\u60a8\u6ca1\u6709\u8f93\u5165\u6570\u636e\\n\u8bf7\u8f93\u5165\u6570\u636e:\\n\\n\");\n\t\t\tcontinue;\n\t\t}\n\t\twhile (!b.empty())\n\t\t\tb.pop();\n\t\twhile (!c.empty())\n\t\t\tc.pop();   \/\/\u6e05\u7a7a\u6808\n\t\ta&#91;l] = '#';\n\t\ti = 0;\n\t\tc.push(1);   \/\/\u6808\u521d\u59cb\u5316\n\t\tint neg = 0;\n\t\twhile (1)\n\t\t{\n\t\t\tif ((a&#91;i] >= '0' &amp;&amp; a&#91;i] &lt;= '9') &amp;&amp; i &lt; l)   \/\/\u5f97\u5230\u6570\u636e\n\t\t\t{\n\t\t\t\tsum = 0;\n\t\t\t\twhile ((a&#91;i] >= '0' &amp;&amp; a&#91;i] &lt;= '9') &amp;&amp; i &lt; l)\n\t\t\t\t{\n\t\t\t\t\tsum = sum * 10 + a&#91;i] - '0';\n\t\t\t\t\ti++;\n\t\t\t\t}\n\t\t\t\tsum1 = 0;\n\t\t\t\tif (a&#91;i] == '.')\n\t\t\t\t{\n\t\t\t\t\ti++;\n\t\t\t\t\tt = 0.1;\n\t\t\t\t\twhile ((a&#91;i] >= '0' &amp;&amp; a&#91;i] &lt;= '9') &amp;&amp; i &lt; l)\n\t\t\t\t\t{\n\t\t\t\t\t\tsum1 = sum1 + (a&#91;i] - '0') * t;\n\t\t\t\t\t\tt = t * 0.1;\n\t\t\t\t\t\ti++;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tif (neg)\n\t\t\t\t{\n\t\t\t\t\tif (sum + sum1 == 0)\n\t\t\t\t\t\tb.push(0);\n\t\t\t\t\telse\n\t\t\t\t\t\tb.push((-1.0) * (sum + sum1));\n\t\t\t\t\tneg = 0;\n\t\t\t\t}\n\t\t\t\telse\n\t\t\t\t\tb.push(sum + sum1);\n\t\t\t}\n\t\t\tswitch (a&#91;i])\n\t\t\t\/\/\u64cd\u4f5c\u7b26\u5904\u7406\n\t\t\t{\n\t\t\tcase '#':\n\t\t\t{\n\t\t\t\twhile ((c.top()) != 1)\n\t\t\t\t{\n\t\t\t\t\tb.push(deal(c.top()));\n\t\t\t\t\tc.pop();\n\t\t\t\t}\n\t\t\t\tif (b.top() == 0)\n\t\t\t\t\tprintf(\"=0\\n\\n\");\n\t\t\t\telse\n\t\t\t\t\tprintf(\"=%g\\n\\n\", b.top());\n\t\t\t\tb.pop();\n\t\t\t\tgoto recycle;\n\t\t\t}\n\t\t\tcase '+':\n\t\t\t{\n\t\t\t\tif (c.top() &lt; 3)\n\t\t\t\t\tc.push(3);\n\t\t\t\telse\n\t\t\t\t{\n\t\t\t\t\twhile (c.top() >= 3)\n\t\t\t\t\t{\n\t\t\t\t\t\tb.push(deal(c.top()));\n\t\t\t\t\t\tc.pop();\n\t\t\t\t\t}\n\t\t\t\t\tc.push(3);\n\t\t\t\t}\n\t\t\t}\n\t\t\t\tbreak;\n\t\t\tcase '-':\n\t\t\t{\n\t\t\t\tif (i == 0 || a&#91;i - 1] == '(')\n\t\t\t\t\tneg = 1;\n\t\t\t\telse if (c.top() &lt; 3)\n\t\t\t\t\tc.push(4);\n\t\t\t\telse\n\t\t\t\t{\n\t\t\t\t\twhile (c.top() >= 3)\n\t\t\t\t\t{\n\t\t\t\t\t\tb.push(deal(c.top()));\n\t\t\t\t\t\tc.pop();\n\t\t\t\t\t}\n\t\t\t\t\tc.push(4);\n\t\t\t\t}\n\t\t\t}\n\t\t\t\tbreak;\n\t\t\tcase '*':\n\t\t\t{\n\t\t\t\tif (c.top() &lt; 5)\n\t\t\t\t\tc.push(5);\n\t\t\t\telse\n\t\t\t\t{\n\t\t\t\t\twhile (c.top() >= 5)\n\t\t\t\t\t{\n\t\t\t\t\t\tb.push(deal(c.top()));\n\t\t\t\t\t\tc.pop();\n\t\t\t\t\t}\n\t\t\t\t\tc.push(5);\n\t\t\t\t}\n\t\t\t}\n\t\t\t\tbreak;\n\t\t\tcase '\/':\n\t\t\t{\n\t\t\t\tif (c.top() &lt; 5)\n\t\t\t\t\tc.push(6);\n\t\t\t\telse\n\t\t\t\t{\n\t\t\t\t\twhile (c.top() >= 5)\n\t\t\t\t\t{\n\t\t\t\t\t\tb.push(deal(c.top()));\n\t\t\t\t\t\tc.pop();\n\t\t\t\t\t}\n\t\t\t\t\tc.push(6);\n\t\t\t\t}\n\t\t\t}\n\t\t\t\tbreak;\n\t\t\tcase '(':\n\t\t\t\tc.push(2);\n\t\t\t\tbreak;\n\t\t\t\tright: case ')':\n\t\t\t\t{\n\t\t\t\t\twhile (c.top() != 2)\n\t\t\t\t\t{\n\t\t\t\t\t\tif (c.top() == 4)\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\tj = i - 1;\n\t\t\t\t\t\t\twhile (a&#91;j] != '-')\n\t\t\t\t\t\t\t\tj--;\n\t\t\t\t\t\t\tj--;\n\t\t\t\t\t\t\tif (a&#91;j] == '(')\n\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\tc.pop();\n\t\t\t\t\t\t\t\tb.push(b.top() * (-1));\n\t\t\t\t\t\t\t\tb.pop();\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\telse\n\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\tb.push(deal(c.top()));\n\t\t\t\t\t\t\t\tc.pop();\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\t\t\t\t\t\telse\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\tb.push(deal(c.top()));\n\t\t\t\t\t\t\tc.pop();\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t\tc.pop();\n\t\t\t\t}\n\t\t\t\tbreak;\n\t\t\tcase '^':\n\t\t\t{\n\t\t\t\tif (c.top() &lt; 7)\n\t\t\t\t\tc.push(7);\n\t\t\t\telse\n\t\t\t\t{\n\t\t\t\t\twhile (c.top() >= 7)\n\t\t\t\t\t{\n\t\t\t\t\t\tb.push(deal(c.top()));\n\t\t\t\t\t\tc.pop();\n\t\t\t\t\t}\n\t\t\t\t\tc.push(7);\n\t\t\t\t}\n\t\t\t}\n\t\t\t\tbreak;\n\t\t\t}\n\t\t\ti++;\n\t\t}\n\t}\n}\ndouble deal(int n)   \/\/\u8ba1\u7b97\u5904\u7406\n{\n\tdouble x, y;\n\tx = b.top();\n\tb.pop();\n\ty = b.top();\n\tb.pop();\n\tswitch (n)\n\t{\n\tcase 3:\n\t\treturn (x + y);\n\t\tbreak;\n\tcase 4:\n\t\treturn (y - x);\n\t\tbreak;\n\tcase 5:\n\t\treturn (x * y);\n\t\tbreak;\n\tcase 6:\n\t{\n\t\tif (x == 0)\n\t\t{\n\t\t\tprintf(\"\u6570\u636e\u9519\u8bef\uff08\u9664\u6570\u4e0d\u80fd\u4e3a\u96f6\uff09\u4e0b\u9762\u7684\u7ed3\u679c\u65e0\u610f\u4e49\\n\");\n\t\t\treturn 0;\n\t\t}\n\t\treturn (y \/ x);\n\t}\n\t\tbreak;\n\tcase 7:\n\t{\n\t\tif (x == 0 &amp;&amp; y == 0)\n\t\t{\n\t\t\tprintf(\"\u6570\u636e\u9519\u8bef\uff080^0\u65e0\u610f\u4e49\uff09\u4e0b\u9762\u7684\u7ed3\u679c\u65e0\u610f\u4e49\\n\");\n\t\t\treturn 0;\n\t\t}\n\t\telse\n\t\t\treturn (pow(y, x));\n\t}\n\t\tbreak;\n\t};\n}\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[12],"tags":[],"class_list":["post-358","post","type-post","status-publish","format-standard","hentry","category-06-03-play-ground"],"_links":{"self":[{"href":"http:\/\/www.wangkaixuan.tech\/index.php?rest_route=\/wp\/v2\/posts\/358","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.wangkaixuan.tech\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.wangkaixuan.tech\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.wangkaixuan.tech\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/www.wangkaixuan.tech\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=358"}],"version-history":[{"count":0,"href":"http:\/\/www.wangkaixuan.tech\/index.php?rest_route=\/wp\/v2\/posts\/358\/revisions"}],"wp:attachment":[{"href":"http:\/\/www.wangkaixuan.tech\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=358"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.wangkaixuan.tech\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=358"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.wangkaixuan.tech\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=358"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}