2025年天津大学计算机考研复试机试真题
2025年天津大学计算机考研复试上机真题
历年天津大学计算机考研复试上机真题
历年天津大学计算机考研复试机试真题
更多学校题目开源地址:https://gitcode.com/verticallimit1/noobdream
N 诺 DreamJudge 题库:输入 “学校名称” 即可筛选该校历年机试真题,题目均在考纲范围内,按难度自动排序。还可搭配《计算机考研机试攻略》刷题,书中题目可通过题号直接在题库中查找。
天津大学-计算表达式
题目描述
Time Limit: 1000 ms
Memory Limit: 256 mb
输入字符串的中缀表达式
包含‘+’、‘-’、‘*’、‘/’、‘(’、‘)’,输出运算结果。
输入输出格式
输入描述:
字符串的中缀表达式
输出描述:
计算结果
输入输出样例
输入样例#:
3+(5-3)*2
输出样例#:
7
代码一
- #include <iostream>
- #include <vector>
- #include <algorithm>
- #include <iomanip>
- #include <set>
- #include <list>
- #include <string>
- #include <cmath>
- #include <stack>
- #include <map>
- #include <sstream>
- #include <queue>
- #include <bitset>
- #include <climits>
- using namespace std;
- map<char, int>mapp{
- {'+',1},{'-',1},{'*',2},{'/',2}
- };
- void fun(stack<int>& nums, stack<char>& ops) {
- int b = nums.top(); nums.pop();
- int a = nums.top(); nums.pop();
- char op = ops.top(); ops.pop();
- int result;
- if (op == '+') result = a + b;
- else if (op == '-')result = a - b;
- else if (op == '*')result = a * b;
- else result = a / b;
- nums.push(result);
- }
- int main() {
- string s;
- while (cin >> s) {
- stack<int>nums;
- stack<char>ops;
- char op;
- for(int i=0;i<s.size();i++) {
- op = s[i];
- if (s[i] >= '0' && s[i] <= '9') {
- int num = 0;
- while (i < s.length() && isdigit(s[i])) {
- num = num * 10 + (s[i] - '0');
- i++;
- }
- i--;
- nums.push(num);
- }
- else if (op == '(') {
- ops.push(op);
- }
- else if (op == ')') {
- while (!ops.empty() && ops.top() != '(') {
- fun(nums, ops);
- }
- if (!ops.empty()) {
- ops.pop();
- }
- }
- else {
- while (!ops.empty() && ops.top() != '(' && mapp[ops.top()]>=mapp[op]) {
- fun(nums, ops);
- }
- ops.push(op);
- }
- }
- while (!ops.empty()) {
- fun(nums, ops);
- }
- cout << nums.top() << endl;
- }
- return 0;
- }
代码二
- #include<iostream>
- #include<unordered_map>
- #include<algorithm>
- #include<string>
- #include<stack>
- using namespace std;
- stack<int> num;
- stack<char> fu;
- unordered_map<char, int> h{ {'+',1},{'-',1},{'*',2},{'/',2} };
- void jisuan() {
- int num1 = num.top();
- num.pop();
- int num2 = num.top();
- num.pop();
- char f = fu.top();
- fu.pop();
- int n = 0;
- if (f == '+')n = num1 + num2;
- if (f == '-')n = num2 - num1;
- if (f == '*')n = num1 * num2;
- if (f == '/')n = num2 / num1;
- num.push(n);
- }
- int main() {
- string s;
- cin >> s;
- for (int i = 0; i < s.size(); i++) {
- if (isdigit(s[i])) {
- int j = i, x = 0;
- while (j < s.size() && isdigit(s[j])) {
- x = x * 10 + s[j] - '0';
- j++;
- }
- num.push(x);
- i = j - 1;
- }
- else if (s[i] == '(') {
- fu.push(s[i]);
- }
- else if (s[i] == ')') {
- while (fu.top() != '(') {
- jisuan();
- }
- fu.pop();
- }
- else {
- while (fu.size() > 0 && h[s[i]] <= h[fu.top()]) {
- jisuan();
- }
- fu.push(s[i]);
- }
- }
- while (fu.size())jisuan();
- cout << num.top() << endl;
- return 0;
- }
代码三
- #include<bits/stdc++.h>
- using namespace std;
- struct li{//方便存储多位数字
- char ch='$';
- int number;
- };
- int main(){
- vector<li>res;//存后缀表达式
- stack<int>sta;//后缀表达式求值(操作栈)
- map<char,int> m;
- m['+']=1;
- m['-']=1;
- m['*']=2;
- m['/']=2;
- m['^']=3;
- string str;
- cin>>str;
- int len=str.length();
- stack<char>s;//中缀转后缀(操作栈)
- for(int i=0;i<len;){//中缀转后缀
- if(str[i]>='0'&&str[i]<='9'){//数字
- string cc;
- while(str[i]>='0'&&str[i]<='9'){
- cc+=str[i];
- i++;
- }
- int num=stoi(cc);
- li temp;
- temp.number=num;
- res.push_back(temp);
- continue;
- }else if(str[i]=='('){//左括号
- s.push(str[i]);
- }else if(str[i]==')'){//右括号
- while(s.top()!='('){
- li temp;
- temp.ch=s.top();
- res.push_back(temp);
- s.pop();
- }
- s.pop();
- }else{//+-*/^运算符
- if(!s.empty()){
- if(s.top()!='('){
- int b=m[str[i]];
- int a=m[s.top()];
- while(a>=b){
- li temp;
- temp.ch=s.top();
- res.push_back(temp);
- s.pop();
- if(s.empty()){break;}
- if(s.top()=='('){break;}
- int a=m[s.top()];
- }
- }}
- s.push(str[i]);
- }
- i++;
- }
- while(!s.empty()){
- li temp;
- temp.ch=s.top();
- res.push_back(temp);
- s.pop();
- }
- //后缀表达式求值
- for(int i=0;i<res.size();i++){
- if(res[i].ch=='$'){//数字
- sta.push(res[i].number);
- }else{//运算符
- int b=sta.top();
- sta.pop();
- int a=sta.top();
- sta.pop();
- int c;
- if(res[i].ch=='+'){
- c=a+b;
- }else if(res[i].ch=='-'){
- c=a-b;
- }else if(res[i].ch=='*'){
- c=a*b;
- }else if(res[i].ch=='/'){
- c=a/b;
- }else if(res[i].ch=='^'){
- c=(int)pow(a,b);
- }
- sta.push(c);
- }
- }
- cout<<sta.top();
- return 0;
- }