C++提取多项式系数
所以我有一个如下所示的多项式:-4x^0 + x^1 + 4x^3 - 3x^4
我可以通过空格和“+”将其标记为:-4x^0, x^1, 4x^3, -, 3x^4
我怎样才能得到带有负号的系数:-4, 1, 0, 4 , -3
x 是唯一会出现的变量,并且它将始终按顺序出现
我计划将系数存储在数组中,数组索引为指数
所以:-4 位于索引 0,1 位于索引 1,0 位于索引 2,4 位于索引 3,-3 位于索引 4
So I have a polynomial that looks like this: -4x^0 + x^1 + 4x^3 - 3x^4
I can tokenize this by space and '+' into: -4x^0, x^1, 4x^3, -, 3x^4
How could I just get the coefficients with the negative sign: -4, 1, 0, 4, -3
x is the only variable that will appear and this will alway appear in order
im planning on storing the coefficients in an array with the array index being the exponent
so: -4 would be at index 0, 1 would be at index 1, 0 at index 2, 4 at index 3, -3 at index 4
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
一旦您将其标记为“-4x^0”、“x^1”等,您就可以使用 strtol() 将文本表示形式转换为数字。 strtol 将自动停止在第一个非数字字符处,因此“x”将停止它; strtol 会给你一个指向阻止它的字符的指针,所以如果你想变得偏执,你可以验证该字符是一个 x。
您将需要处理隐式的 1(即特别在“x^1”中)。我会做这样的事情:
Once you have tokenized to "-4x^0", "x^1", etc. you can use strtol() to convert the textual representation into a number. strtol will automatically stop at the first non-digit character so the 'x' will stop it; strtol will give you a pointer to the character that stoped it, so if you want to be paranoid, you can verify the character is an x.
You will need to treat implicit 1's (i.e. in "x^1" specially). I would do something like this:
编辑:获取系数(包括符号)的简单方法:
EDIT: Simple method to get the coefficient (including the sign):
扫描字符串中的“x”,然后向后存储系数的每个字符,直到遇到空格。例如:
scan the string for an 'x', then go backward storing each character of the coefficient until you hit white space. eg:
为了快速解决方案,我的方法是编写一个递归下降解析器。在字符串中向前移动并提取所需的组件。有很多编写这样的表达式的解析器的示例。
如果您想使用库,可以使用 boost::regex 或 boost::spirit,具体取决于您想要采用哪种方法。
For a quick solution, my approach would be to write a recursive descent parser. Move forward in the string and extract the components you want. There are many examples around for writing a parser of an expression like this.
If you want to use a library, you could use boost::regex or boost::spirit, depending on what kind of approach you want to take.
编写一个简单的分词器。定义数字标记 (
/[-0123456789][0123456789]+/
)、指数标记 (/x^(::number::)/
)。忽略空格和+
。按照您的预期持续读取标记,直到字符串末尾。然后以您想要的任何形式(例如整数)吐出令牌。
在具有一些抽象的类中,这可能一切都会很好(例如从流而不是普通字符串中读取)。
Write a simple tokenizer. Define a number token (
/[-0123456789][0123456789]+/
), an exponent token (/x^(::number::)/
). Ignore whitespace and+
.Continually read tokens as you'd expect them until the end of the string. Then spit out the tokens in whatever form you want (e.g. integers).
This would probably all go nicely in a class with some abstraction (e.g. read from a stream instead of a plain string).