如果输入是:y=1(x^3)+0.05(x^2)-2(x^1)-1如何从这个std::字符串中得到数字

If the input is: y=1(x^3)+0.05(x^2)-2(x^1)-1 how to get the numbers form this std::string?

本文关键字:std 字符串 数字 输入 如果      更新时间:2023-10-16

我试图做一个循环来获得y=后的第一个数字但有时term1coffs的值是:65(有时值是:65,不带括号?!

std::string fx = cwin.get_string("Enter a polynomial formula that has a form like this:     y=1(x^3)+2(x^2)+0.5(x^1)+1");
std::string j;
 //j is a condition to end for loop
int n=2; 
std::string term1coffs;
 //to get the number before the bracket y= ?? ( X^.....
for (j=fx.substr(n,1);j=="(";n+0)
{
n=n+1;
}
term1coffs=fx.substr(2,n);
double term1coff= atof(term1coffs.c_str());

传统的解析器是这样做的(用伪代码编写):

do
{
   get a character
   if (character is operator)
   {
        store character as operator
        convert operand to double
        if conversion succeeded
            store operand value
        else
            store operand as variable name
        operand = "";
   }
   else
   {
        add current character to operand
   }
} while(there is more to do)

如果您希望稍后使用解析的结果执行计算,您可能希望根据优先级顺序生成树结构,并考虑括号。基本原理是分流场算法