news 2026/5/11 19:55:11

2025年天津大学计算机考研复试机试真题(附 AC 代码 + 解题思路)

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
2025年天津大学计算机考研复试机试真题(附 AC 代码 + 解题思路)

2025年天津大学计算机考研复试机试真题

2025年天津大学计算机考研复试上机真题

历年天津大学计算机考研复试上机真题

历年天津大学计算机考研复试机试真题

更多学校题目开源地址:https://gitcode.com/verticallimit1/noobdream

N 诺 DreamJudge 题库:输入 “学校名称” 即可筛选该校历年机试真题,题目均在考纲范围内,按难度自动排序。还可搭配《计算机考研机试攻略》刷题,书中题目可通过题号直接在题库中查找。

天津大学-计算表达式

题目描述

Time Limit: 1000 ms
Memory Limit: 256 mb

输入字符串的中缀表达式

包含‘+’、‘-’、‘*’、‘/’、‘(’、‘)’,输出运算结果。

输入输出格式
输入描述:

字符串的中缀表达式

输出描述:

计算结果

输入输出样例
输入样例#:
3+(5-3)*2
输出样例#:
7

代码一

  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4. #include <iomanip>
  5. #include <set>
  6. #include <list>
  7. #include <string>
  8. #include <cmath>
  9. #include <stack>
  10. #include <map>
  11. #include <sstream>
  12. #include <queue>
  13. #include <bitset>
  14. #include <climits>
  15. using namespace std;
  16. map<char, int>mapp{
  17. {'+',1},{'-',1},{'*',2},{'/',2}
  18. };
  19. void fun(stack<int>& nums, stack<char>& ops) {
  20. int b = nums.top(); nums.pop();
  21. int a = nums.top(); nums.pop();
  22. char op = ops.top(); ops.pop();
  23. int result;
  24. if (op == '+') result = a + b;
  25. else if (op == '-')result = a - b;
  26. else if (op == '*')result = a * b;
  27. else result = a / b;
  28. nums.push(result);
  29. }
  30. int main() {
  31. string s;
  32. while (cin >> s) {
  33. stack<int>nums;
  34. stack<char>ops;
  35. char op;
  36. for(int i=0;i<s.size();i++) {
  37. op = s[i];
  38. if (s[i] >= '0' && s[i] <= '9') {
  39. int num = 0;
  40. while (i < s.length() && isdigit(s[i])) {
  41. num = num * 10 + (s[i] - '0');
  42. i++;
  43. }
  44. i--;
  45. nums.push(num);
  46. }
  47. else if (op == '(') {
  48. ops.push(op);
  49. }
  50. else if (op == ')') {
  51. while (!ops.empty() && ops.top() != '(') {
  52. fun(nums, ops);
  53. }
  54. if (!ops.empty()) {
  55. ops.pop();
  56. }
  57. }
  58. else {
  59. while (!ops.empty() && ops.top() != '(' && mapp[ops.top()]>=mapp[op]) {
  60. fun(nums, ops);
  61. }
  62. ops.push(op);
  63. }
  64. }
  65. while (!ops.empty()) {
  66. fun(nums, ops);
  67. }
  68. cout << nums.top() << endl;
  69. }
  70. return 0;
  71. }

代码二

  1. #include<iostream>
  2. #include<unordered_map>
  3. #include<algorithm>
  4. #include<string>
  5. #include<stack>
  6. using namespace std;
  7. stack<int> num;
  8. stack<char> fu;
  9. unordered_map<char, int> h{ {'+',1},{'-',1},{'*',2},{'/',2} };
  10. void jisuan() {
  11. int num1 = num.top();
  12. num.pop();
  13. int num2 = num.top();
  14. num.pop();
  15. char f = fu.top();
  16. fu.pop();
  17. int n = 0;
  18. if (f == '+')n = num1 + num2;
  19. if (f == '-')n = num2 - num1;
  20. if (f == '*')n = num1 * num2;
  21. if (f == '/')n = num2 / num1;
  22. num.push(n);
  23. }
  24. int main() {
  25. string s;
  26. cin >> s;
  27. for (int i = 0; i < s.size(); i++) {
  28. if (isdigit(s[i])) {
  29. int j = i, x = 0;
  30. while (j < s.size() && isdigit(s[j])) {
  31. x = x * 10 + s[j] - '0';
  32. j++;
  33. }
  34. num.push(x);
  35. i = j - 1;
  36. }
  37. else if (s[i] == '(') {
  38. fu.push(s[i]);
  39. }
  40. else if (s[i] == ')') {
  41. while (fu.top() != '(') {
  42. jisuan();
  43. }
  44. fu.pop();
  45. }
  46. else {
  47. while (fu.size() > 0 && h[s[i]] <= h[fu.top()]) {
  48. jisuan();
  49. }
  50. fu.push(s[i]);
  51. }
  52. }
  53. while (fu.size())jisuan();
  54. cout << num.top() << endl;
  55. return 0;
  56. }

代码三

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. struct li{//方便存储多位数字
  4. char ch='$';
  5. int number;
  6. };
  7. int main(){
  8. vector<li>res;//存后缀表达式
  9. stack<int>sta;//后缀表达式求值(操作栈)
  10. map<char,int> m;
  11. m['+']=1;
  12. m['-']=1;
  13. m['*']=2;
  14. m['/']=2;
  15. m['^']=3;
  16. string str;
  17. cin>>str;
  18. int len=str.length();
  19. stack<char>s;//中缀转后缀(操作栈)
  20. for(int i=0;i<len;){//中缀转后缀
  21. if(str[i]>='0'&&str[i]<='9'){//数字
  22. string cc;
  23. while(str[i]>='0'&&str[i]<='9'){
  24. cc+=str[i];
  25. i++;
  26. }
  27. int num=stoi(cc);
  28. li temp;
  29. temp.number=num;
  30. res.push_back(temp);
  31. continue;
  32. }else if(str[i]=='('){//左括号
  33. s.push(str[i]);
  34. }else if(str[i]==')'){//右括号
  35. while(s.top()!='('){
  36. li temp;
  37. temp.ch=s.top();
  38. res.push_back(temp);
  39. s.pop();
  40. }
  41. s.pop();
  42. }else{//+-*/^运算符
  43. if(!s.empty()){
  44. if(s.top()!='('){
  45. int b=m[str[i]];
  46. int a=m[s.top()];
  47. while(a>=b){
  48. li temp;
  49. temp.ch=s.top();
  50. res.push_back(temp);
  51. s.pop();
  52. if(s.empty()){break;}
  53. if(s.top()=='('){break;}
  54. int a=m[s.top()];
  55. }
  56. }}
  57. s.push(str[i]);
  58. }
  59. i++;
  60. }
  61. while(!s.empty()){
  62. li temp;
  63. temp.ch=s.top();
  64. res.push_back(temp);
  65. s.pop();
  66. }
  67. //后缀表达式求值
  68. for(int i=0;i<res.size();i++){
  69. if(res[i].ch=='$'){//数字
  70. sta.push(res[i].number);
  71. }else{//运算符
  72. int b=sta.top();
  73. sta.pop();
  74. int a=sta.top();
  75. sta.pop();
  76. int c;
  77. if(res[i].ch=='+'){
  78. c=a+b;
  79. }else if(res[i].ch=='-'){
  80. c=a-b;
  81. }else if(res[i].ch=='*'){
  82. c=a*b;
  83. }else if(res[i].ch=='/'){
  84. c=a/b;
  85. }else if(res[i].ch=='^'){
  86. c=(int)pow(a,b);
  87. }
  88. sta.push(c);
  89. }
  90. }
  91. cout<<sta.top();
  92. return 0;
  93. }
版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/5/10 11:45:04

从服务端视角看客户端技术演进:协同优化与架构适配

我们常说“客户端是服务端的延伸&#xff0c;用户体验的最终载体”。客户端技术的每一次迭代&#xff08;从原生到跨端&#xff0c;从单体到组件化&#xff09;&#xff0c;都需要服务端提供精准的架构适配和能力支撑。近年来&#xff0c;随着“原生跨端”融合架构成为主流&…

作者头像 李华
网站建设 2026/5/9 4:54:34

深度剖析APT43(APT-X):网络间谍组织技术与攻击架构全解析

APT43&#xff08;APT-X&#xff09;综合档案 通用信息 别名&#xff1a;APT43 亦被称为 APT-X。归属&#xff1a;与某个未具体指明的国家背景行为体有关联。起源&#xff1a;可能基于东欧或亚洲。首次识别&#xff1a;至少在2015年之前已开始活动。主要目标&#xff1a;进行…

作者头像 李华
网站建设 2026/5/11 11:28:16

从手工到全自动化:一个中型项目测试流水线在2025年的演进之路

演进之路的起点与动力在软件产业追求“更高质量、更快速度、更低成本”的永恒命题下&#xff0c;测试&#xff0c;作为交付前最后的关键闸门&#xff0c;其效率与可靠性直接决定了产品的生命力与团队的生产力。回溯到2025年的今天&#xff0c;我们清晰可见一条从技术债务的泥沼…

作者头像 李华
网站建设 2026/5/9 16:16:55

Open-AutoGLM火爆GitHub:为什么它能成为下一个Star破万的AI工具?

第一章&#xff1a;Open-AutoGLM火爆GitHub&#xff1a;下一个Star破万的AI工具&#xff1f; 近期&#xff0c;一款名为 Open-AutoGLM 的开源项目在 GitHub 上迅速走红&#xff0c;上线仅两周便收获超过 8,000 颗 star&#xff0c;社区讨论热度持续攀升。该项目旨在构建一个自动…

作者头像 李华
网站建设 2026/5/9 10:44:42

windows远程连接出现函数不支持验证错误的解决方法

[window title] 远程桌面连接 [content] 出现身份验证错误。 要求的函数不受支持 远程计算机: 192.168.88.146 这可能是由于 credssp 加密数据库修正。 若要了解详细信息&#xff0c;请访问 https://go.microsoft.com/fwlink/?linkid866660 [^] 隐藏详细信息(d) [确定] […

作者头像 李华
网站建设 2026/5/10 11:29:04

【Open-AutoGLM本地部署全攻略】:手把手教你从零搭建个人AI知识引擎

第一章&#xff1a;Open-AutoGLM本地部署的核心价值在企业级AI应用不断深化的背景下&#xff0c;将大语言模型&#xff08;LLM&#xff09;如Open-AutoGLM进行本地化部署&#xff0c;已成为保障数据安全、提升响应效率和实现系统可控的关键路径。本地部署不仅规避了公有云环境下…

作者头像 李华