用C++ 语言如何实现表达式拆分“1*2*3*(4+(5+6)*(7+8))+9”
来源:互联网 发布:旺宝免费淘宝收藏软件 编辑:程序博客网 时间:2024/06/06 09:20
问题:用C++ 语言如何实现表达式拆分
把一个字符串形式的数学表达式以拆加号为原则,一级一级拆分,最终汇总为一个字符串数组。数学表达式是由”+”、”(“、”)”和数字组成。
示例如下:
1、字符串表达式为:1*2*3*(4+(5+6)*(7+8))+9
拆分最终的字符串数组结果为(数组元素排序没有要求):
1*2*3*4
1*2*3*5*7
1*2*3*5*8
1*2*3*6*7
1*2*3*6*8
9
// C++11
代码如下:
#include <iostream>#include <string>#include <vector>std::vector<std::string> processexp(const std::string &sexp);int main(){ std::string sexp; std::getline(std::cin, sexp); std::cout << sexp << std::endl; std::vector<std::string> svret = processexp(sexp); for (const auto &i : svret) { std:: cout << i << std::endl; } return 0;}std::vector<std::string> processexp(const std::string &sexp){ std::vector<std::string> svec; std::vector<std::string> destsvec; std::vector<std::string> midsvec; std::string read; char c; for (std::string::size_type i = 0; i < sexp.size(); i++) { c = sexp[i]; switch (c) { case '*': { if (!read.empty()) { svec.push_back(read); read.clear(); } svec.emplace_back(1, c); } break; case '(': { std::string::size_type m, n, p1, p2; m = i; while (true) { m += 1; p1 = sexp.find_first_of('(', m); p2 = sexp.find_first_of(')', m); if (p1 != std::string::npos && p1 < p2) { m = p2; continue; } n = p2; break; } std::string subexp = sexp.substr(i+1, n - i - 1); std::vector<std::string> sv = processexp(subexp); if (i > 1 && sexp[i-1] != '*' && n + 1 < sexp.size() && sexp[n+1] == '*') { midsvec = sv; i = n; break; } std::string bfexp; for (const auto &e : svec) { bfexp += e; } svec.clear(); std::vector<std::string> desttemp{midsvec}; midsvec.clear(); for (const auto &e : sv) { if (!desttemp.empty()) { for (const auto &e2 : desttemp) { auto e3 = e2 + bfexp + e; midsvec.push_back(e3); } } else { midsvec.push_back(bfexp+e); } } if (n + 1 < sexp.size() && sexp[n+1] == '*') {#if 0 for (const auto &e : destsvec) { std::cout << "|<<<" << e << "|"; } std::cout << std::endl;#endif } else { destsvec.insert(destsvec.end(), midsvec.begin(), midsvec.end()); midsvec.clear(); } i = n; } break; case '+': { if (!read.empty()) { svec.push_back(read); } std::string all; for (const auto &e : svec) { all += e; } if (!all.empty()) { std::vector<std::string> desttemp{midsvec}; midsvec.clear(); if (!desttemp.empty()) { for (const auto &e2 : desttemp) { auto e3 = e2 + all; destsvec.push_back(e3); } } else { destsvec.push_back(all); } } svec.clear(); read.clear(); } break; default: read += c; break; } } if (!read.empty()) { svec.push_back(read); } std::string all; for (const auto &e : svec) { all += e; } if (!all.empty()) { std::vector<std::string> desttemp{midsvec}; midsvec.clear(); if (!desttemp.empty()) { for (const auto &e2 : desttemp) { auto e3 = e2 + all; destsvec.push_back(e3); } } else { destsvec.push_back(all); } } return destsvec;}
0 0
- 用C++ 语言如何实现表达式拆分“1*2*3*(4+(5+6)*(7+8))+9”
- C语言 数组拆分
- C语言字符串拆分
- c 语言字符串拆分
- C语言 问号表达式记忆 表达式1?表达式2:表达式3
- 用C语言实现求1+2+3+4+5+6的值
- 如何在C语言中巧用正则表达式 (1)
- C语言 递归拆分数字
- 分数拆分(C语言)
- 用C语言实现正则表达式匹配器
- 如何在C语言中巧用正则表达式
- 如何在C语言中巧用正则表达式
- 如何在C语言中巧用正则表达式
- 如何在C语言中巧用正则表达式
- 如何在C语言中巧用正则表达式
- 如何在C语言中巧用正则表达式
- 如何在C语言中巧用正则表达式
- 如何在C语言中巧用正则表达式
- 欢迎使用CSDN-markdown编辑器
- 虚拟机的那些事儿
- SoapUI自动化--Groovy脚本实现随机抽取DataSource(Excel)
- Java单元测试工具:JUnit4(一)——概述及简单例子
- SpringMVC & Struts2
- 用C++ 语言如何实现表达式拆分“1*2*3*(4+(5+6)*(7+8))+9”
- .netプロジェクト名を取得する方法
- 发生android.view.ViewRoot$CalledFromWrongThreadException异常和loop循环的添加
- SoapUI读取外部excel数据以及script断言
- 安卓开发中非常炫的效果集合
- Eclipse Tomcat 启动报错 45s
- Mac 下添加路径的办法,以及conda无法使用的解决办法
- PHP,C# 和JAVARSA签名及验签
- HTMl5的sessionStorage和localStorage