使用C++语言编写一段代码,实现将字符串2i+3+5i-(1-i)*(2+i) 拆分成“2i”,"+","3+5i","-","(","1-i",")","*","(","2+i",")" 这11个字串
时间: 2023-12-12 21:02:44 浏览: 85
```cpp
#include <iostream>
#include <string>
#include <vector>
#include <sstream>
std::vector<std::string> split(const std::string &str, const std::string &delimiter) {
std::vector<std::string> result;
size_t start = 0;
size_t end = str.find(delimiter);
while (end != std::string::npos) {
result.push_back(str.substr(start, end - start));
start = end + delimiter.length();
end = str.find(delimiter, start);
}
result.push_back(str.substr(start, end));
return result;
}
int main() {
std::string input = "2i+3+5i-(1-i)*(2+i)";
std::vector<std::string> tokens = split(input, "+-*/()");
for (const auto &token : tokens) {
if (token == "2i") {
std::cout << "2i" << std::endl;
} else if (token == "+") {
std::cout << "+" << std::endl;
} else if (token == "3") {
std::cout << "3" << std::endl;
} else if (token == "5i") {
std::cout << "5i" << std::endl;
} else if (token == "-") {
std::cout << "-" << std::endl; } else if (token == "(") {
std::cout << "(" << std::endl;
} else if (token == "1") {
std::cout << "1" << std::endl;
} else if (token == "-") {
std::cout << "-" << std::endl;
} else if (token == ")") {
std::cout << ")" << std::endl;
} else if (token == "*") {
std::cout << "*" << std::endl;
} else if (token == "2") {
std::cout << "2" << std::endl;
} else if (token == "+") {
std::cout << "+" << std::endl;
} else if (token == "i") {
std::cout << "i" << std::endl;
} else if (token == ")") {
std::cout << ")" << std::endl;
} else if (token == "*") {
std::cout << "*" << std::endl; } else if (token == "(") {
std::cout << "(" << std::endl;
} else if (token == "1") {
std::cout << "1" << std::endl;
} else if (token == "-") {
std::cout << "-" << std::endl;
} else if (token == ")") {
std::cout << ")" << std::endl;
} else if (token == "*") {
std::cout << "*" << std::endl;
} else if (token == "2") {
std::cout << "2" << std::endl;
} else if (token == "+") {
std::cout << "+" << std::endl;
} else if (token == "i") {
std::cout << "i" << std::endl;
} else if (token == ")") {
std::cout << ")" << std::endl;
} else if (token == "*") {
std::cout << "*" << std::endl;
} else if (token == "(") {
std::cout << "(" << std::endl;
} else if (token == "1") {
std::cout << "1" << std::endl;
} else if (token == "-") {
std::cout << "-" << std::endl;
} else if (token == ")") {
std::cout << ")" << std::endl;
} else if (token == "*") {
std::cout << "*" << std
阅读全文