table of Contents
First, understand the meaning of problems
Design of the sum of two functions are univariate polynomial with the product and, for example:
\ [\ text known two polynomials {:} \\ \ begin {align} & 3x ^ 4-5x ^ 2 + 6x-2 \\ & ^ {} -7X 20 is 5X. 4 + 3x ^ \ End align = left {} \]
\ [\ {polynomials and text as:} \\ \ begin {align} 5x ^ {20} -4x ^ 4-5x ^ 2 + 9x -2 \ end {align} \]
Product polynomial is assumed \ ((A + B) (C + D) = AC + AD + BC + BD \) , then the product polynomial as follows:
\ [\} 15x the begin align = left {^ {^ {24} -25X 22} + 30x ^ {21}
-10x ^ {20} -21x ^ 8 + 35x ^ 6-33x ^ 5 + 14x ^ 4-15x ^ 3 + 18x ^ 2-6x \ end {align} \] by the above-described problems Italian understanding, we can design function are seeking two univariate polynomial of the product of and.
Sample input:
\ [\} the begin {align = left & 4-5x ^ 2 + 3x ^ 6X-2 \ Quad -> \ Quad \ text {4} th \ 3 \ 4 \, --5 \ 2 \ , 6 \, 1 \, - 2 \, 0 \\ & 5x ^ {20} -7x ^ 4 + 3x \ quad -> \ quad \ text {3 } th \ 5 \ 20 \, --7 \ , 4 \, 3 \, 1 \\ \ end {align} \\ \]
output sample:
\ [\} the begin {align = left & 15x ^ ^ {24} {22 is -25X + 30X}} ^ {21 is -10X ^ {20} -21x ^ 8 + 35x ^ 6-33x ^ 5 + 14x ^ 4-15x ^ 3 + 18x ^ 2-6x \\ & 15 \, 24 \, -25 \, 22 \, 30 \, 21 \, -10 \ 20 \, -21 \ 8 \ 35 \ 6 \, -33 \ 5 \, 14 \ 4 \, -15 \ 3 \ 18 \ 2 \, -6 \, 1 \, 5 \, 20 \, -4 \, 4 \, -5 \, 2 \, 9 \, 1 \, -2 \, 0 \ end {align} \]
Second, the idea of solving
- Polynomial representation
- Framework program
- Reading polynomial
- Addition to achieve
- Multiply
- Polynomial output
Third, the polynomial representation
It represents the only non-zero entries
3.1 Array
Advantages: simple programming, debugging simple
Disadvantages: need to determine in advance the array size
A better approach is to achieve: dynamic array (dynamically change the size of the array)
3.2 list
Advantages: strong dynamic
Disadvantages: slightly more complex programming, debugging more difficult
Data structure design:
/* c语言实现 */
typedef struct PolyNode *Polynomial;
struct PolyNode{
int coef;
int expon;
Polynomial link;
}
Fourth, build the framework program
/* c语言实现 */
int main()
{
读入多项式1;
读入多项式2;
乘法运算并输出;
加法运算并输出;
return 0;
}
int main()
{
Polynomial P1, P2, PP, PS;
P1 = ReadPoly();
P2 = ReadPoly();
PP = Mult(P1, P2);
PrintPoly(PP);
PS = Add(P1, P2);
PrintPoly(PS);
return 0;
}
We need to design function:
- Reading a polynomial
- Multiplying two polynomials
- Sum of two polynomials
- Polynomial output
5, how to read the polynomial
/* c语言实现 */
Polynomial ReadPoly()
{
...;
scanf("%d", &N);
...;
while (N--) {
scanf("%d %d", &c, &e);
Attach(c, e, &Rear);
}
...;
return P;
}
Rear initial value is how much?
Handled in two ways:
- Rear initial value is NULL: Rear whether the Attach function do different processing according to NULL
- Rear points to an empty node
/* c语言实现 */
void Attach(int c, int e, Polynomial *pRear)
{
Polynomial P;
P = (Polynomial)malloc(sizeof(struct PolyNode));
p->coef = c; /* 对新结点赋值 */
p->expon = e;
p->link = NULL;
(*pRear)->link = P;
(*pRear) = P; /* 修改pRear值 */
/* c语言实现 */
Polynomial ReadPoly()
{
Polynomial P, Rear, t;
int c, e, N;
scanf("%d", &N);
P = (Polynomial)malloc(sizeof(struct PolyNode)); // 链表头空结点
P->link = NULL;
Rear = P;
while (N--) {
scanf("%d %d", &c, &e);
Attach(c, e, &Rear); // 将当前项插入多项式尾部
}
t = P; P = P->link; free(t); // 删除临时生成的头结点
return P;
}
Sixth, how will the sum of two polynomials
/* c语言实现 */
Polynomial Add(Polynomial P1, Polynomial P2)
{
...;
t1 = P1; t2 = P2;
P = (Polynomial)malloc(sizeof(struct PolyNode));
P->link = NULL;
Rear = P;
while (t1 && t2){
if (t1->expon == t2->expon){
...;
}
else if (t1->expon > t2->expon){
...;
}
else{
...;
}
}
while (t1){
...;
}
while (t2){
...;
}
...;
return P;
}
Seven, how to multiplying two polynomials
method:
- Converting the multiplication to adder
将P1当前项(ci, ei)乘P2多项式,再加到结果多项式里
/* c语言实现 */
t1 = P1; t2 = P2;
P = (Polynomial)malloc(sizeof(struct PolyNode)); P->link = NULL;
Rear = P;
while (t2){
Attach(t1->coef * t2->coef, t1->expon + t2->expon, &Rear);
t2 = t2->link;
}
- 逐项插入
将P1当前项(c1_i, e1_i)乘P2当前项(c2_i, e2_i),并插入到结果多项式中。关键是要找到插入位置
初始结果多项式可由P1第一项乘P2获得(如上)
/* c语言实现 */
Polynomial Mult(Polynomial P1, Polynomial P2)
{
...;
t1 = P1; t2 = P2;
...;
while (t2){ // 先用P1的第一项乘以P2,得到P
...;
}
t1 = t1->link;
while (t1){
t2 = P2; Rear = P;
while (t2){
e = t1->expon + t2->expon;
c = t1->coef * t2->coef;
...;
t2 = t2->link;
}
t1 = t1->link;
}
...;
}
/* c语言实现 */
Polynomial Mult(Polynomial P1, Polynomial P2)
{
Polynomial P, Rear, t1, t2, t;
int c, e;
if (!P1 || !P2) return NULL;
t1 = P1; t2 = P2;
P = (Polynomial)malloc(sizeof(struct PolyNOde)); P->link = NULL;
Rear = P;
while (t2){ // 先用P1的第一项乘以P2,得到P
Attach(t1->coef * t2->coef, t1->expon + t2->expon, &Rear);
t2 = t2->link;
}
t1 = t1->link;
while (t1){
t2 = P2; Rear = P;
while (t2){
e = t1->expon + t2->expon;
c = t1->coef * t2->coef;
...;
t2 = t2->link;
}
t1 = t1->link;
}
...;
}
/* c语言实现 */
Polynomial Mult(Polynomial P1, Polynomial P2)
{
Polynomial P, Rear, t1, t2, t;
int c, e;
if (!P1 || !P2) return NULL;
t1 = P1; t2 = P2;
P = (Polynomial)malloc(sizeof(struct PolyNOde)); P->link = NULL;
Rear = P;
while (t2){ // 先用P1的第一项乘以P2,得到P
Attach(t1->coef * t2->coef, t1->expon + t2->expon, &Rear);
t2 = t2->link;
}
t1 = t1->link;
while (t1) {
t2 = P2; Rear = P;
while (t2) {
e = t1->expon + t2->expon;
c = t2->coef * t2->coef;
while (Rear->link && Rear->link->expon > e)
Rear = Rear->link;
if (Rear->link && Rear->link->expon == e){
...;
}
else{
...;
}
t2 = t2->link;
}
t1 = t1->link;
}
...;
}
/* c语言实现 */
Polynomial Mult(Polynomial P1, Polynomial P2)
{
Polynomial P, Rear, t1, t2, t;
int c, e;
if (!P1 || !P2) return NULL;
t1 = P1; t2 = P2;
P = (Polynomial)malloc(sizeof(struct PolyNOde)); P->link = NULL;
Rear = P;
while (t2){ // 先用P1的第一项乘以P2,得到P
Attach(t1->coef * t2->coef, t1->expon + t2->expon, &Rear);
t2 = t2->link;
}
t1 = t1->link;
while (t1) {
t2 = P2; Rear = P;
while (t2) {
e = t1->expon + t2->expon;
c = t2->coef * t2->coef;
while (Rear->link && Rear->link->expon > e)
Rear = Rear->link;
if (Rear->link && Rear->link->expon == e){
if (Rear->link->coef + c)
Rear->link->coef += c;
else{
t = Rear->link;
Rear->link = t->link;
free(t);
}
}
else{
t = (Polynomial)malloc(sizeof(struct PolyNode));
t->coef = c; t->expon = e;
t->link = Rear->link;
Rear->link = t; Rear = Rear->link;
}
t2 = t2->link;
}
t1 = t1->link;
}
...;
}
/* c语言实现 */
Polynomial Mult(Polynomial P1, Polynomial P2)
{
Polynomial P, Rear, t1, t2, t;
int c, e;
if (!P1 || !P2) return NULL;
t1 = P1; t2 = P2;
P = (Polynomial)malloc(sizeof(struct PolyNOde)); P->link = NULL;
Rear = P;
while (t2){ // 先用P1的第一项乘以P2,得到P
Attach(t1->coef * t2->coef, t1->expon + t2->expon, &Rear);
t2 = t2->link;
}
t1 = t1->link;
while (t1) {
t2 = P2; Rear = P;
while (t2) {
e = t1->expon + t2->expon;
c = t2->coef * t2->coef;
while (Rear->link && Rear->link->expon > e)
Rear = Rear->link;
if (Rear->link && Rear->link->expon == e){
if (Rear->link->coef + c)
Rear->link->coef += c;
else{
t = Rear->link;
Rear->link = t->link;
free(t);
}
}
else{
t = (Polynomial)malloc(sizeof(struct PolyNode));
t->coef = c; t->expon = e;
t->link = Rear->link;
Rear->link = t; Rear = Rear->link;
}
t2 = t2->link;
}
t1 = t1->link;
}
t2 = P; P = P->link; free(t2);
return P;
}
八、如何将多项式输出
/* c语言实现 */
void PrintPoly(Polynomial P)
{
// 输出多项式
int flag = 0; // 辅助调整输出格式用,判断输出加法还是乘法
if (!P) {printf("0 0\n"); return ;}
while (P) {
if (!flag)
flag = 1;
else
printf(" ");
printf("%d %d", P->coef, P->expon);
P = P->link;
}
printf("\n");
}