{"id":173291,"date":"2020-03-05T08:24:49","date_gmt":"2020-03-05T00:24:49","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=173291"},"modified":"2020-02-24T09:25:59","modified_gmt":"2020-02-24T01:25:59","slug":"python-implementing-calculator","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/python-implementing-calculator.html","title":{"rendered":"python\u901a\u8fc7\u95ed\u5305\u5b9e\u73b0\u8ba1\u7b97\u5668\u7684\u529f\u80fd"},"content":{"rendered":"
\u5bfc\u8bfb<\/td>\n | \u8fd9\u7bc7\u6587\u7ae0\u4e3b\u8981\u4ecb\u7ecd\u4e86python\u5982\u4f55\u901a\u8fc7\u95ed\u5305\u5b9e\u73b0\u8ba1\u7b97\u5668\u7684\u529f\u80fd,\u6587\u4e2d\u901a\u8fc7\u793a\u4f8b\u4ee3\u7801\u4ecb\u7ecd\u7684\u975e\u5e38\u8be6\u7ec6\uff0c\u5bf9\u5927\u5bb6\u7684\u5b66\u4e60\u6216\u8005\u5de5\u4f5c\u5177\u6709\u4e00\u5b9a\u7684\u53c2\u8003\u5b66\u4e60\u4ef7\u503c,\u9700\u8981\u7684\u670b\u53cb\u53ef\u4ee5\u53c2\u8003\u4e0b<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n \u8fd9\u7bc7\u6587\u7ae0\u4e3b\u8981\u4ecb\u7ecd\u4e86python\u5982\u4f55\u901a\u8fc7\u95ed\u5305\u5b9e\u73b0\u8ba1\u7b97\u5668\u7684\u529f\u80fd,\u6587\u4e2d\u901a\u8fc7\u793a\u4f8b\u4ee3\u7801\u4ecb\u7ecd\u7684\u975e\u5e38\u8be6\u7ec6\uff0c\u5bf9\u5927\u5bb6\u7684\u5b66\u4e60\u6216\u8005\u5de5\u4f5c\u5177\u6709\u4e00\u5b9a\u7684\u53c2\u8003\u5b66\u4e60\u4ef7\u503c,\u9700\u8981\u7684\u670b\u53cb\u53ef\u4ee5\u53c2\u8003\u4e0b<\/p>\n \u6ee1\u8db3\u95ed\u5305\u7684\u6761\u4ef6\uff1a \n<\/strong><\/div>\n 1\u3001\u51fd\u6570\u4e2d\u5d4c\u5957\u4e00\u4e2a\u51fd\u6570 \u95ed\u5305\u7684\u4f5c\u7528\uff1a<\/strong><\/div>\n \u5b9e\u73b0\u6570\u636e\u9501\u5b9a<\/p>\n \u88c5\u9970\u5668\u7684\u4f5c\u7528\uff1a<\/strong><\/div>\n \u5c31\u662f\u4e3a\u5df2\u7ecf\u5b58\u5728\u7684\u5bf9\u8c61\u6dfb\u52a0\u989d\u5916\u7684\u529f\u80fd\u3002<\/p>\n \u5e38\u7528\u573a\u666f\uff1a<\/strong><\/div>\n \u63d2\u5165\u65e5\u5fd7\u3001\u6027\u80fd\u6d4b\u8bd5\u3001\u4e8b\u52a1\u5904\u7406\u3001\u7f13\u5b58\u3001\u6743\u9650\u6821\u9a8c\u7b49<\/p>\n \u4ee3\u7801\u793a\u4f8b\uff1a<\/strong><\/div>\n \r\ndef scope(func):\r\n def calc(a, b):\r\n try:\r\n _a = float(a)\r\n _b = float(b)\r\n except:\r\n _a = 0\r\n _b = 0\r\n return func(_a, _b)\r\n return calc\r\n \r\n \r\n@scope\r\ndef add(a, b):\r\n return a + b\r\n \r\n \r\n@scope\r\ndef sub(a, b):\r\n return a - b\r\n \r\n \r\n@scope\r\ndef mul(a, b):\r\n return a * b\r\n \r\n \r\n@scope\r\ndef div(a, b):\r\n try:\r\n return a \/ b\r\n except ZeroDivisionError as error:\r\n print(error)\r\n return 0\r\n \r\n \r\n# \u8c03\u7528\u95ed\u5305\u51fd\u6570\r\nres = scope(add)(\"1\", 3)\r\nprint(res)\r\n \r\nres = add(\"1\", \"2\")\r\nprint(res)\r\n \r\nres = sub(\"1\", \"2\")\r\nprint(res)\r\n \r\nres = mul(\"1\", \"2\")\r\nprint(res)\r\n \r\nres = div(\"1\", \"2\")\r\nprint(res)\r\n<\/pre>\n |