在 C++ 中使用符号拆分数学函数
Split mathemtical function using signs in c++
我想按照其中变量的符号拆分数学函数,如下所示:
string input = x-5y+3z=10
output--> [x,-5y,+3z,=10]
我曾经在 java 上通过使用 input.split() 函数来执行此操作,但是因为我是 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
如果有任何改进建议,请说。
扫描字符串,直到找到 '+'
、'-'
、'='
之一或到达结尾。存储命中的索引(包括 0
和 len-1
),这将分隔子字符串。
要处理第一个词,只需注意如果它确实有符号,第一个子字符串将为空。
x-5y+3z=10
^ ^ ^
|x|-5y|+3z|=10|
-x-5y+3z=10
^ ^ ^ ^
||-x|-5y|+3z|=10|
我想按照其中变量的符号拆分数学函数,如下所示:
string input = x-5y+3z=10
output--> [x,-5y,+3z,=10]
我曾经在 java 上通过使用 input.split() 函数来执行此操作,但是因为我是 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
如果有任何改进建议,请说。
扫描字符串,直到找到 '+'
、'-'
、'='
之一或到达结尾。存储命中的索引(包括 0
和 len-1
),这将分隔子字符串。
要处理第一个词,只需注意如果它确实有符号,第一个子字符串将为空。
x-5y+3z=10
^ ^ ^
|x|-5y|+3z|=10|
-x-5y+3z=10
^ ^ ^ ^
||-x|-5y|+3z|=10|