当前位置: 首页 > news >正文

【表达式求值】

题目

代码

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
stack<LL> num;
stack<char> op;
unordered_map<char, int> pr{{'+',1}, {'-',1}, {'*',2}, {'/',2}};
void cal()
{LL b = num.top(); num.pop();LL a = num.top(); num.pop();char c = op.top(); op.pop();LL res;if(c == '+') res = a+b;else if(c == '-') res = a-b;else if(c == '*') res = a*b;else if(c == '/') res = a/b;num.push(res % 10000);
}
int main()
{string s;cin >> s;for(int i = 0; i < s.size(); i++){if(isdigit(s[i])){int j = i;stringstream ss;while(j < s.size() && isdigit(s[j])) ss << s[j++];i = j-1;LL x;ss >> x;num.push(x);}else if(s[i] == '(') op.push(s[i]);else if(s[i] == ')'){while(op.size() && op.top() != '(') cal();op.pop();}else{while(op.size() && pr[s[i]] <= pr[op.top()]) cal();op.push(s[i]);}}while(op.size()) cal();LL ans = num.top();cout << ans % 10000;return 0;
}


http://www.mrgr.cn/news/23989.html

相关文章:

  • langgraph tool如何发送自定义事件
  • Leetcode - 周赛414
  • 帧缓冲 framebuffer
  • Pytorch维度转换操作:view,reshape,permute,flatten函数详解
  • ZAB协议(算法)
  • 精选干货!分享5款免费论文生成软件
  • 【Solidity】类型
  • Vue--》视觉盛宴:提升可视化大屏分辨率适配的技巧
  • AV1 Bitstream Decoding Process Specification:约定
  • Tableau安装和使用
  • MIT6.824 课程-PrimaryBackupReplication
  • leetcode 146.LRU缓存
  • 跨国公司研发战略调整与中国IT产业的未来
  • 动态规划-分割回文串ⅡⅣ
  • C++学习笔记(16)
  • 工具知识 | Linux 常用命令参考手册
  • 在windows下抓空包(monitor网卡+wareshark+MNM)
  • Kubernetes------Service
  • framebuffer
  • 人工智能的历史:关键年份和人物