2.计算器实现
一.计算器实现思路二.分析这里我们要将后缀表达式,转换成为中缀表达式建立一个栈存储运算数读取后缀表达式遇到运算数入栈遇到运算符出栈顶两个数进行运算运算后将结果作为一个运算数入栈继续参与下一次的运算。读取表达式结束后最后栈顶的数值就是运算结果。三.中缀表达式转后缀表达式四.代码实现#define _CRT_SECURE_NO_WARNINGS 1 #includeiostream #includeset #includemap #includestring #includevector #includestack using namespace std; class Solution { public: mapchar,int op { {,1}, {-,1}, {*,2}, {/,2} }; //中缀转后缀 void toRPN(const string s,size_t i,vectorstring v) { stackchar st; while(i s.size()) { if(isdigit(s[i])) { //操作数直接输出 string num; while(i s.size() isdigit(s[i])) { num s[i]; } v.push_back(num); } else if(s[i] () { i; toRPN(s,i,v); } else if(s[i] )) { while(st.size()) { v.push_back(string(1,st.top())); st.pop(); } i; return; } else { if(st.empty() || op[s[i]] op[st.top()]) { st.push(s[i]); i; } else { char ch st.top(); st.pop(); v.push_back(string(1,ch)); } } } while(st.size()) { v.push_back(string(1,st.top())); st.pop(); } } }; int main() { size_t i 0; vectorstring v; //string str 12-3; string str 12-(3*45)-7; Solution().toRPN(str, i, v); for (auto e : v) { cout e ; } cout endl; return 0; }五.题目题目链接:224. 基本计算器 - 力扣LeetCode题目链接:1.先将逆波兰表达式求值这个代码写出来class Solution { public: int evalRPN(vectorstring tokens) { stackint st; int i 0; mapstring,functionint(int,int) opFuncMap { {,[](int x,int y){return x y;}}, {-,[](int x,int y){return x - y;}}, {*,[](int x,int y){return x * y;}}, {/,[](int x,int y){return x / y;}} }; for (auto str : tokens) { if (opFuncMap.count(str)) { int right st.top(); st.pop(); int left st.top(); st.pop(); int ret opFuncMap[str](left,right); st.push(ret); } else{ st.push(stoi(str)); } } return st.top(); } };2.代码结合class Solution { public: mapchar,int op { {,1}, {-,1}, {*,2}, {/,2} }; //中缀转后缀 void toRPN(const string s,size_t i,vectorstring v) { stackchar st; while(i s.size()) { if(isdigit(s[i])) { //操作数直接输出 string num; while(i s.size() isdigit(s[i])) { num s[i]; } v.push_back(num); } else if(s[i] () { i; toRPN(s,i,v); } else if(s[i] )) { while(st.size()) { v.push_back(string(1,st.top())); st.pop(); } i; return; } else { if(st.empty() || op[s[i]] op[st.top()]) { st.push(s[i]); i; } else { char ch st.top(); st.pop(); v.push_back(string(1,ch)); } } } while(st.size()) { v.push_back(string(1,st.top())); st.pop(); } } int evalRPN(vectorstring tokens) { stackint st; int i 0; mapstring,functionint(int,int) opFuncMap { {,[](int x,int y){return x y;}}, {-,[](int x,int y){return x - y;}}, {*,[](int x,int y){return x * y;}}, {/,[](int x,int y){return x / y;}} }; for (auto str : tokens) { if (opFuncMap.count(str)) { int right st.top(); st.pop(); int left st.top(); st.pop(); int ret opFuncMap[str](left,right); st.push(ret); } else{ st.push(stoi(str)); } } return st.top(); } int calculate(string s) { size_t i 0; vectorstring v; toRPN(s,i,v); return evalRPN(v); } };这么实现是不行的,因为会存在空格class Solution { public: mapchar,int op { {,1}, {-,1}, {*,2}, {/,2} }; //中缀转后缀 void toRPN(const string s,size_t i,vectorstring v) { stackchar st; while(i s.size()) { if(isdigit(s[i])) { //操作数直接输出 string num; while(i s.size() isdigit(s[i])) { num s[i]; } v.push_back(num); } else if(s[i] () { i; toRPN(s,i,v); } else if(s[i] )) { while(st.size()) { v.push_back(string(1,st.top())); st.pop(); } i; return; } else { if(st.empty() || op[s[i]] op[st.top()]) { st.push(s[i]); i; } else { char ch st.top(); st.pop(); v.push_back(string(1,ch)); } } } while(st.size()) { v.push_back(string(1,st.top())); st.pop(); } } int evalRPN(vectorstring tokens) { stackint st; int i 0; mapstring,functionint(int,int) opFuncMap { {,[](int x,int y){return x y;}}, {-,[](int x,int y){return x - y;}}, {*,[](int x,int y){return x * y;}}, {/,[](int x,int y){return x / y;}} }; for (auto str : tokens) { if (opFuncMap.count(str)) { int right st.top(); st.pop(); int left st.top(); st.pop(); int ret opFuncMap[str](left,right); st.push(ret); } else{ st.push(stoi(str)); } } return st.top(); } int calculate(string s) { string news; for(auto ch : s) { if(ch ! ) { news ch; } } size_t i 0; vectorstring v; toRPN(news,i,v); return evalRPN(v); } };但是这样还是过不了如果是(-3)这种,我们将其变成(0-3)class Solution { public: mapchar,int op { {,1}, {-,1}, {*,2}, {/,2} }; //中缀转后缀 void toRPN(const string s,size_t i,vectorstring v) { stackchar st; while(i s.size()) { if(isdigit(s[i])) { //操作数直接输出 string num; while(i s.size() isdigit(s[i])) { num s[i]; } v.push_back(num); } else if(s[i] () { i; toRPN(s,i,v); } else if(s[i] )) { while(st.size()) { v.push_back(string(1,st.top())); st.pop(); } i; return; } else { if(st.empty() || op[s[i]] op[st.top()]) { st.push(s[i]); i; } else { char ch st.top(); st.pop(); v.push_back(string(1,ch)); } } } while(st.size()) { v.push_back(string(1,st.top())); st.pop(); } } int evalRPN(vectorstring tokens) { stackint st; int i 0; mapstring,functionint(int,int) opFuncMap { {,[](int x,int y){return x y;}}, {-,[](int x,int y){return x - y;}}, {*,[](int x,int y){return x * y;}}, {/,[](int x,int y){return x / y;}} }; for (auto str : tokens) { if (opFuncMap.count(str)) { int right st.top(); st.pop(); int left st.top(); st.pop(); int ret opFuncMap[str](left,right); st.push(ret); } else{ st.push(stoi(str)); } } return st.top(); } int calculate(string s) { string news; for(auto ch : s) { if(ch ! ) { news ch; } } news.swap(s); news.clear(); for(size_t i 0;i s.size();i) { if(s[i] - !isdigit(s[i - 1])) { news 0-; } else { news s[i]; } } cout news endl; size_t i 0; vectorstring v; toRPN(news,i,v); return evalRPN(v); } };但是还是过不了class Solution { public: mapchar,int op { {,1}, {-,1}, {*,2}, {/,2} }; //中缀转后缀 void toRPN(const string s,size_t i,vectorstring v) { stackchar st; while(i s.size()) { if(isdigit(s[i])) { //操作数直接输出 string num; while(i s.size() isdigit(s[i])) { num s[i]; } v.push_back(num); } else if(s[i] () { i; toRPN(s,i,v); } else if(s[i] )) { while(st.size()) { v.push_back(string(1,st.top())); st.pop(); } i; return; } else { if(st.empty() || op[s[i]] op[st.top()]) { st.push(s[i]); i; } else { char ch st.top(); st.pop(); v.push_back(string(1,ch)); } } } while(st.size()) { v.push_back(string(1,st.top())); st.pop(); } } long long evalRPN(vectorstring tokens) { // 1. 栈类型改为 long long存储大数避免溢出 stacklong long st; // 2. 运算函数的参数和返回值都改为 long long mapstring, functionlong long(long long, long long) opFuncMap { {, [](long long x, long long y) { return x y; }}, {-, [](long long x, long long y) { return x - y; }}, {*, [](long long x, long long y) { return x * y; }}, {/, [](long long x, long long y) { return x / y; }} }; for (auto str : tokens) { if (opFuncMap.count(str)) { // 3. 弹出的操作数改为 long long long long right st.top(); st.pop(); long long left st.top(); st.pop(); // 4. 运算结果改为 long long long long ret opFuncMap[str](left, right); st.push(ret); } else { // 5. 字符串转数字改用 stoll转 long long而非 stoi转 int st.push(stoll(str)); } } // 返回栈顶的 long long 结果 return st.top(); } int calculate(string s) { string news; for(auto ch : s) { if(ch ! ) { news ch; } } news.swap(s); news.clear(); for(size_t i 0;i s.size();i) { if(s[i] - (i 0 || (!isdigit(s[i - 1]) s[i - 1] ! )))) { news 0-; } else { news s[i]; } } cout news endl; size_t i 0; vectorstring v; toRPN(news,i,v); return evalRPN(v); } };还要进行特判,然后用long long代替int,防止我们溢出

相关新闻

Spark大数据分析:解锁海量数据价值的核心利器

Spark大数据分析:解锁海量数据价值的核心利器

Spark大数据分析:解锁海量数据价值的核心利器一、引言:Spark重塑大数据分析新格局在数字化浪潮下,全球数据量呈指数级爆发式增长,传统大数据处理框架因计算效率低、响应延迟高、功能单一等短板,难以满足海量数据的实时…

2026/5/17 10:19:30 阅读更多 →
SUPER COLORIZER效果对比专题:不同参数下的色彩饱和度与风格差异研究

SUPER COLORIZER效果对比专题:不同参数下的色彩饱和度与风格差异研究

SUPER COLORIZER效果对比专题:不同参数下的色彩饱和度与风格差异研究 最近在玩线稿上色,发现一个挺有意思的现象:同一张黑白线稿,用同一个上色工具,最后出来的效果却能天差地别。有时候色彩鲜艳活泼,有时候…

2026/5/17 10:19:30 阅读更多 →
【Vue3】初学Vue3:从‘’手动操作DOM‘’到‘’数据驱动视图‘’

【Vue3】初学Vue3:从‘’手动操作DOM‘’到‘’数据驱动视图‘’

前言:本文将简单介绍一下Vue3的setup()函数,以及如何通过数据驱动视图的方式来实现数据响应式的界面效果 一,从“搬砖”到“自动化” Vue3在前端开发中与html js有什么区别。又为什么会成为主流框架之一呢&#xff1f…

2026/7/3 4:15:56 阅读更多 →

最新新闻

抖店AI标题优化怎么用标题违规和低质标题怎么改

抖店AI标题优化怎么用标题违规和低质标题怎么改

抖店AI标题优化怎么用?标题违规和低质标题怎么改 抖店商品标题写不好,会影响审核、搜索理解和买家点击。很多商家从 1688 搬标题时,原标题里带批发词、品牌词、极限词、无关热词,直接上架容易违规,也不一定适合抖店买家…

2026/7/5 4:29:15 阅读更多 →
如何3分钟完成通达信缠论插件部署:终极自动化分析指南

如何3分钟完成通达信缠论插件部署:终极自动化分析指南

如何3分钟完成通达信缠论插件部署:终极自动化分析指南 【免费下载链接】ChanlunX 缠中说禅炒股缠论可视化插件 项目地址: https://gitcode.com/gh_mirrors/ch/ChanlunX 还在为复杂的缠论分析而烦恼吗?面对繁琐的笔段划分和中枢识别,传…

2026/7/5 4:27:15 阅读更多 →
接口自动化测试项目框架详解

接口自动化测试项目框架详解

🍅 点击文末小卡片,免费获取软件测试全套资料,资料在手,涨薪更快 在选择接口测试自动化框架时,需要根据团队的技术栈和项目需求来综合考虑。对于测试团队来说,使用Python相关的测试框架更为便捷。无论选…

2026/7/5 4:25:15 阅读更多 →
单片机IWIP 原子云实验

单片机IWIP 原子云实验

单片机 :STM32F407 开发板:DMF407电机开发板 平台:keil V5.31HSE 为8MHZ HSI为16MHZ主函数int main(void) {HAL_Init(); /* 初始化HAL库 */sys_stm32_clock_init(336, 8, 2, 7); /* 设置时钟,168Mhz */delay_init…

2026/7/5 4:25:15 阅读更多 →
Nano Banana部署Gemini 2.5 Flash:ARM+NPU边缘多模态推理实战指南

Nano Banana部署Gemini 2.5 Flash:ARM+NPU边缘多模态推理实战指南

1. 项目概述:这不是一个“升级包”,而是一套可落地的嵌入式AI推理工作流 你手头有一块 Nano Banana 开发板——它不是树莓派,也不是 Jetson Nano,而是基于全志 H616 芯片、带双千兆网口、4GB LPDDR4、支持 PCIe 2.0 x1 的国产小钢…

2026/7/5 4:23:15 阅读更多 →
3分钟掌握Crontab UI:告别命令行恐惧的Linux定时任务可视化管理神器

3分钟掌握Crontab UI:告别命令行恐惧的Linux定时任务可视化管理神器

3分钟掌握Crontab UI:告别命令行恐惧的Linux定时任务可视化管理神器 【免费下载链接】crontab-ui Easy and safe way to manage your crontab file 项目地址: https://gitcode.com/gh_mirrors/cr/crontab-ui 还在为复杂的crontab语法而烦恼吗?Cro…

2026/7/5 4:19:14 阅读更多 →

日新闻

B站视频下载神器BiliTools:5分钟学会轻松保存任何B站内容

B站视频下载神器BiliTools:5分钟学会轻松保存任何B站内容

B站视频下载神器BiliTools:5分钟学会轻松保存任何B站内容 【免费下载链接】BiliTools A cross-platform bilibili toolbox. 跨平台哔哩哔哩工具箱,支持下载视频、番剧等等各类资源 项目地址: https://gitcode.com/GitHub_Trending/bilit/BiliTools …

2026/7/5 0:03:34 阅读更多 →
威胁模型全解析:从新手入门到实战应用,助你构建安全产品!

威胁模型全解析:从新手入门到实战应用,助你构建安全产品!

威胁模型的陌生现状在忙碌疲惫的一天里,参与了关于混合后量子密码学的讨论,应付端点攻击找茬的人,还参与留言板讨论后,发现“威胁模型”对多数人仍是陌生概念,且多被当作时髦用语。有趣的相关画作有一幅由 Embyr 创作的…

2026/7/5 0:03:34 阅读更多 →
渗透测试入门指南:从零基础到实战环境搭建

渗透测试入门指南:从零基础到实战环境搭建

1. 从“看热闹”到“入门”:我理解的渗透测试到底是什么?每次看到新闻里说某个大公司的数据被“黑”了,或者某个网站被攻击导致服务瘫痪,你是不是和我一样,心里会冒出两个念头:一是“这黑客真厉害”&#x…

2026/7/5 0:07:38 阅读更多 →

周新闻

B站视频下载神器BiliTools:5分钟学会轻松保存任何B站内容

B站视频下载神器BiliTools:5分钟学会轻松保存任何B站内容

B站视频下载神器BiliTools:5分钟学会轻松保存任何B站内容 【免费下载链接】BiliTools A cross-platform bilibili toolbox. 跨平台哔哩哔哩工具箱,支持下载视频、番剧等等各类资源 项目地址: https://gitcode.com/GitHub_Trending/bilit/BiliTools …

2026/7/5 0:03:34 阅读更多 →
威胁模型全解析:从新手入门到实战应用,助你构建安全产品!

威胁模型全解析:从新手入门到实战应用,助你构建安全产品!

威胁模型的陌生现状在忙碌疲惫的一天里,参与了关于混合后量子密码学的讨论,应付端点攻击找茬的人,还参与留言板讨论后,发现“威胁模型”对多数人仍是陌生概念,且多被当作时髦用语。有趣的相关画作有一幅由 Embyr 创作的…

2026/7/5 0:03:34 阅读更多 →
渗透测试入门指南:从零基础到实战环境搭建

渗透测试入门指南:从零基础到实战环境搭建

1. 从“看热闹”到“入门”:我理解的渗透测试到底是什么?每次看到新闻里说某个大公司的数据被“黑”了,或者某个网站被攻击导致服务瘫痪,你是不是和我一样,心里会冒出两个念头:一是“这黑客真厉害”&#x…

2026/7/5 0:07:38 阅读更多 →

月新闻