C++ extract polynomial coefficients

前端 未结 5 1351
迷失自我
迷失自我 2021-01-26 01:38

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 t

5条回答
  •  面向向阳花
    2021-01-26 02:05

    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.

提交回复
热议问题