{"id":13418495,"url":"https://github.com/gatieme/CodingInterviews","last_synced_at":"2025-03-15T03:31:22.493Z","repository":{"id":47551012,"uuid":"55763831","full_name":"gatieme/CodingInterviews","owner":"gatieme","description":"剑指Offer——名企面试官精讲典型编程题","archived":false,"fork":false,"pushed_at":"2021-02-20T15:09:56.000Z","size":975,"stargazers_count":4818,"open_issues_count":10,"forks_count":1124,"subscribers_count":175,"default_branch":"master","last_synced_at":"2024-07-31T22:43:12.765Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"C++","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"gpl-3.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/gatieme.png","metadata":{"files":{"readme":"README.MD","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2016-04-08T08:38:19.000Z","updated_at":"2024-07-31T10:04:34.000Z","dependencies_parsed_at":"2022-08-03T04:45:56.023Z","dependency_job_id":null,"html_url":"https://github.com/gatieme/CodingInterviews","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gatieme%2FCodingInterviews","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gatieme%2FCodingInterviews/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gatieme%2FCodingInterviews/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gatieme%2FCodingInterviews/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/gatieme","download_url":"https://codeload.github.com/gatieme/CodingInterviews/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243681024,"owners_count":20330152,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-07-30T22:01:02.938Z","updated_at":"2025-03-15T03:31:22.487Z","avatar_url":"https://github.com/gatieme.png","language":"C++","readme":"\r\n#include  \u003ciostream\u003e\r\n#include \u003cvector\u003e\r\n#include \u003cstring\u003e\r\n#include \u003cstack\u003e\r\nusing namespace std;\r\n\r\n//判断是否为操作符\r\nbool isOperator(char op)\r\n{\r\n\treturn (op == '+' || op == '-' || op == '*' || op == '/');\r\n}\r\n\r\n//判断是否为数字\r\nbool isDigit(char op)\r\n{\r\n\treturn  (op \u003e= '0' \u0026\u0026 op \u003c= '9');\r\n}\r\n\r\n//计算表达式结果\r\nint cal(int left, int right, char op)\r\n{\r\n\tint res = 0;\r\n\tif (op == '+')\r\n\t\tres = left + right;\r\n\telse if (op == '-')\r\n\t\tres = left - right;\r\n\telse if (op == '*')\r\n\t\tres = left * right;\r\n\telse if (op == '/')\r\n\t\tres = left / right;\r\n\treturn res;\r\n}\r\n\r\n//判断运算符优先级\r\nint pirority(char op)\r\n{\r\n\tif (op == '+' || op == '-')\r\n\t\treturn 1;\r\n\tif (op == '*' || op == '/')\r\n\t\treturn 2;\r\n\telse\r\n\t\treturn 0;\r\n}\r\n\r\n//逆波兰表达式转中缀表达式\r\nint evalRPN(string \u0026tokens)\r\n{\r\n\tif (tokens.size() \u003c= 0)\r\n\t\treturn 0;\r\n\tstack\u003cint\u003e s1;//创建辅助栈\r\n\tint left = 0, right = 0;//定义左/右操作数\r\n\tint result = 0;//定义中间结果\r\n\tstring temp;\r\n\tfor (unsigned int i = 0; i \u003c tokens.size(); i++ )\r\n\t{\r\n\t\t\r\n\t\tif (isDigit(tokens[i]))//扫描数字入栈\r\n\t\t{\r\n\t\t\ttemp.push_back(tokens[i]);\r\n\t\t}\r\n\t\telse if (tokens[i] == ' ')\r\n\t\t{\r\n\t\t\tif (temp.size() \u003e 0)   //防止运算符后面跟分隔符，所以判断一下temp里面是否有数字\r\n\t\t\t{\r\n\t\t\t\ts1.push(atoi(temp.c_str()));\r\n\t\t\t\ttemp.clear();\r\n\t\t\t}\r\n\t\t}\r\n\t\telse if (isOperator(tokens[i]) \u0026\u0026 !s1.empty())\r\n\t\t{\r\n\t\t\t//防止数字后面直接跟运算符，所以这里也要判断一下temp是否还有数字没有提取出来\r\n\t\t\tif (temp.size() \u003e 0)\r\n\t\t\t{\r\n\t\t\t\ts1.push(atoi(temp.c_str()));\r\n\t\t\t\ttemp.clear();\r\n\t\t\t}\r\n\t\t\tright = s1.top();\r\n\t\t\ts1.pop();\r\n\t\t\t\r\n\t\t\tleft = s1.top();\r\n\t\t\ts1.pop();\r\n\t\t\r\n\t\t\tresult = cal(left, right, tokens[i]);\r\n\t\t\ts1.push(result);\r\n\t\t}\r\n\t\t\r\n\t}\r\n\tif (!s1.empty())\r\n\t{\r\n\t\tresult = s1.top();\r\n\t\ts1.pop();\r\n\t}\r\n\r\n\treturn result;\r\n}\r\n\r\n\r\n\r\n\r\n//中缀表达式转逆波兰表达式\r\nvector\u003cchar\u003e infixToPRN(const string \u0026 token)\r\n{\r\n\tvector\u003cchar\u003e v1;\r\n\tint len = token.size();//string的长度\r\n\tif (len == 0)\r\n\t\treturn v1;\r\n\r\n\tstack\u003cchar\u003e s1;//存放逆波兰式的结果\r\n\tint outLen = 0;\r\n\tfor (int i = 0; i \u003c len ; i++)\r\n\t{\r\n\t\tif (token[i] == ' ')//跳过空格\r\n\t\t\tcontinue;\r\n\t\tif (isDigit(token[i]) )//若是数字，直接输出\r\n\t\t{\r\n\t\t\tv1.push_back(token[i]);\r\n\t\t}\r\n\t\telse if (token[i] == '(')//如果是'('，则压栈\r\n\t\t{\r\n\t\t\ts1.push(token[i]);\r\n\t\t}else if (token[i] == ')')//如果是')'， 则栈中运算符逐个出栈并输出，直至遇到'('。 '('出栈并丢弃\r\n\t\t{\r\n\t\t\twhile (s1.top() != '(')\r\n\t\t\t{\r\n\t\t\t\tv1.push_back( s1.top());\r\n\t\t\t\ts1.pop();\r\n\t\t\t}\r\n\t\t\ts1.pop();//此时栈中为')'，跳过\r\n\t\t}\r\n\t\twhile (isOperator(token[i]))//如果是运算符\r\n\t\t{\r\n\t\t\t//空栈||或者栈顶为)||新来的元素优先级更高  \r\n\t\t\tif( s1.empty() || s1.top() == '(' || pirority(token[i]) \u003e pirority(s1.top()))\r\n\t\t\t{\r\n\t\t\t\ts1.push(token[i]);// 当前操作符优先级比栈顶高， 将栈顶操作符写入后缀表达式\r\n\t\t\t\tbreak;\r\n\t\t\t}else \r\n\t\t\t{\r\n\t\t\t\tv1.push_back(s1.top());\r\n\t\t\t\ts1.pop();\r\n\t\t\t}\r\n\t\t\t \r\n\t\t\t\r\n\t\t}\r\n\t}\r\n\twhile (!s1.empty())//输入结束，将栈中剩余操作符出栈输出\r\n\t{\r\n\t\tv1.push_back(s1.top());\r\n\t\ts1.pop();\r\n\t}\r\n\treturn v1;\r\n}\r\nint main()\r\n{\r\n\t//vector\u003cstring\u003e sRPN = {\"4\", \"13\", \"5\" , \"/\", \"+\"};//逆波兰表达式\r\n\t//cout \u003c\u003c \"逆波兰表达式结果为：\" \u003c\u003c evalRPN(sRPN) \u003c\u003cendl;\r\n\r\n\t//vector\u003cstring\u003e fix = {\"4\", \"+\", \"13\", \"/\", \"5\"};//中缀表达式\r\n\tstring fix = \"2+2*(1*2-4/2)*1\";\r\n\tvector\u003cchar\u003e RPN = infixToPRN(fix);\r\n\tstring s_fix;\r\n\tfor (auto it = RPN.begin(); it != RPN.end(); it++)\r\n\t{\r\n\t\tcout \u003c\u003c *it \u003c\u003c \" \";\r\n\t\ts_fix += *it;\r\n\t\ts_fix += \" \";\r\n\t}\r\n\tcout \u003c\u003c endl;\r\n\tcout \u003c\u003c \"逆波兰表达式结果为：\" \u003c\u003c evalRPN(s_fix) \u003c\u003c endl;\r\n\r\n\r\n\t\r\n\tsystem(\"pause\");\r\n\r\n\treturn 0;\r\n\t\r\n}\r\n\r\n","funding_links":[],"categories":["C++","Useful Links","TODO scan for Android support in followings","C++ (225)","算法题"],"sub_categories":["C++"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgatieme%2FCodingInterviews","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fgatieme%2FCodingInterviews","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgatieme%2FCodingInterviews/lists"}