【发布时间】:2018-04-23 09:10:24
【问题描述】:
我想通过其中变量的符号来拆分数学函数,如下所示:
string input = x-5y+3z=10
output--> [x,-5y,+3z,=10]
我曾经使用 input.split() 函数在 java 上执行此操作,但因为我是 c++ 初学者,所以我找不到像在 java 中那样执行此操作的方法。
有什么帮助吗?
编辑:
很长一段时间后,我找到了我想要的解决方案,也许它不是理想的解决方案,它可能会有更多改进,但正如我之前所说,我仍然是 c++ 的初学者,这就是它 ->
std::string fun="-x1+2x2=10.";
std::string *arr=new std::string[20];
int index=0;
for (int i=0; i<=20; i++){
if(fun[index]=='.'){
break;
}
for(int j=index; j<fun.length(); j++){
if(fun[j]=='+' || fun[j]=='-' || fun[j]=='='|| fun[j]== '.'){
if(j!=index){
arr[i]=fun.substr(index,(j-index));
index=j;
std::cout<<"term is : "<<arr[i]<<"\t index is : "<<index<<"\t j is : "<<j<<std::endl;
break;
}
}
}
输出是->
term is : -x1 index is : 3 j is : 3
term is : +2x2 index is : 7 j is : 7
term is : =10 index is : 10 j is : 10
如果有任何改进建议请说。
【问题讨论】:
-
只需迭代输入字符串,每当遇到运算符时,只需提取运算符及其后面的数字。
标签: c++ math split delimiter mathematical-expressions