如果输入是: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?
我试图做一个循环来获得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)
如果您希望稍后使用解析的结果执行计算,您可能希望根据优先级顺序生成树结构,并考虑括号。基本原理是分流场算法
相关文章:
- 我可以重新分配/覆盖std::字符串吗
- 如何将这个std::字符串转换为std::基本字符串
- std::字符串与 PCWSTR 的对话
- 重载 std::字符串运算符+ 用于打印枚举名称
- 从std::字符串创建OssBitString
- std::字符串擦除以删除最后一个字母
- 如何使用 C++ 中的原语初始化类(如 std::字符串从 const char* 初始化)
- std::字符串串联操作
- 如何有效地将一个大std::字符串的一部分转换为float
- 转义std::字符串中的特殊字符
- 用C++替换std::字符串中的一个子字符串,但不能全部替换
- C++std::字符串流到常量字符*的转换
- 如何将std::字符串转换为不同的字符串类
- 对齐C++字符串类型问题 std::字符串到 TStr
- std::字符串到字符*而不切割字节
- 编写具有非ASCII数据的std ::字符串
- 将 Delphi 的字符串转换为 std::字符串用于C++
- std::字符串添加到字符*
- 字符* 和 std::字符串和常量字符*之间的转换
- 用指针访问std ::字符串中的元素