问题

软件 >> Calculator >> 符号运算
Questions in category: 符号运算 (Symbolic operations).

多项式的加法

Posted by haifeng on 2022-11-28 22:02:15 last update 2022-11-28 22:02:15 | Answers (0) | 收藏


如果使用单链表存储多项式, 则在进行多项式的加法时, 应事先降幂排列.

例如下面的 polyn.exe 测试程序, 可以输入多个参数, 若要执行 $2x^3-x^2+8$ 加上 $4x^4-3x^3+6x+9$, 则可以输入

polyn.exe "2x^3-x^2+8" "4x^4-3x^3+6x+9"

这里已经降幂排列.

D:\work\cs\c++\datastruct\Polynomial\polyn_vc\polyn\Debug>polyn.exe "2x^3-x^2+8" "4x^4-3x^3+6x+9"
argv[1] is 2x^3-x^2+8
        2x^3-1x^2+8x^0

argv[2] is 4x^4-3x^3+6x+9
        4x^4-3x^3+6x^1+9x^0


---------
add 2x^3-x^2+8 and 4x^4-3x^3+6x+9
2x^3-1x^2+8x^0
4x^4-3x^3+6x^1+9x^0
[1] p2-> 4x^4
[2] p1=p2 -1x^3
[3] p1-> -1x^2
[4] p2-> 6x^1
[5] p1=p2 17x^0

------
4x^4-1x^3-1x^2+6x^1+17x^0

 

如果输入的多项式并非已降幂排列, 则 add 程序的算法要么改进, 要么改进多项式存储的数据结构, 比如改为线索树存储.

D:\work\cs\c++\datastruct\Polynomial\polyn_vc\polyn\Debug>polyn.exe "2x^3-x^2+8" "4x^4+6x-3x^3+9"
argv[1] is 2x^3-x^2+8
        2x^3-1x^2+8x^0

argv[2] is 4x^4+6x-3x^3+9
        4x^4+6x^1-3x^3+9x^0


---------
add 2x^3-x^2+8 and 4x^4+6x-3x^3+9
2x^3-1x^2+8x^0
4x^4+6x^1-3x^3+9x^0
[1] p2-> 4x^4
[2] p1-> 2x^3
[3] p1-> -1x^2
[4] p2-> 6x^1
[5] p2-> -3x^3
[6] p1=p2 17x^0

------
4x^4+2x^3-1x^2+6x^1-3x^3+17x^0