1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > 浙江大学 PTA习题3.6 一元多项式的乘法与加法运算 (20分)

浙江大学 PTA习题3.6 一元多项式的乘法与加法运算 (20分)

时间:2021-11-09 21:28:12

相关推荐

浙江大学 PTA习题3.6 一元多项式的乘法与加法运算 (20分)

设计函数分别求两个一元多项式的乘积与和。

输入格式:

输入分2行,每行分别先给出多项式非零项的个数,再以指数递降方式输入一个多项式非零项系数和指数(绝对值均为不超过1000的整数)。数字间以空格分隔。

输出格式:

输出分2行,分别以指数递降方式输出乘积多项式以及和多项式非零项的系数和指数。数字间以空格分隔,但结尾不能有多余空格。零多项式应输出0 0

输入样例:

4 3 4 -5 2 6 1 -2 03 5 20 -7 4 3 1

输出样例:

15 24 -25 22 30 21 -10 20 -21 8 35 6 -33 5 14 4 -15 3 18 2 -6 15 20 -4 4 -5 2 9 1 -2 0

第一次写的,修修改改花了快6个小时

/*

设计函数分别求两个一元多项式的乘积与和。

输入格式:

输入分2行,每行分别先给出多项式非零项的个数,再以指数递降方式输入一个多项式非零项系数和指数(绝对值均为不超过1000的整数)。数字间以空格分隔。

输出格式:

输出分2行,分别以指数递降方式输出乘积多项式以及和多项式非零项的系数和指数。数字间以空格分隔,但结尾不能有多余空格。零多项式应输出0 0。

*/

#include <stdio.h>

#include <stdlib.h>

typedef int ElementType;

typedef struct node *Stacknode;

struct node{

int coef;

int expon;

Stacknode next;

};

typedef Stacknode Polynomial;

void PrintPoly(Polynomial P)

{

int flag = 0;

if(!P)

{

printf("0 0\n");

return;

}

P = P->next;

while(P)

{

if(!flag)

{

flag = 1;

}

else

{

printf(" ");

}

printf("%d %d",P->coef,P->expon);

P = P->next;

}

printf("\n");

}

Polynomial Read( int n )

{

int i;

Polynomial Head = (Polynomial)malloc(sizeof(struct node));

Polynomial Rear = Head;

Head->next =NULL;

for(i=0;i<n;i++)

{

Polynomial tmp = (Polynomial)malloc(sizeof(struct node));

scanf("%d",&tmp->coef);

scanf("%d",&tmp->expon);

tmp->next = NULL;

Rear->next = tmp;

Rear = tmp;

}

return Head;

}

/*void Attach( int c, int e, Polynomial Rear)

{

Polynomial P;

P = (Polynomial)malloc(sizeof(struct node));

P->coef = c;

P->expon = e;

P->next = NULL;

Rear->next = P;

Rear = P;

}

*/

Polynomial AddPoly( Polynomial P1,Polynomial P2 )

{

Polynomial PP1,PP2,tmp,Head,Rear;

Rear = (Polynomial)malloc(sizeof(struct node));

Rear->next = NULL;

Head = Rear;

PP1 = P1->next;

PP2 = P2->next;

if(!PP1)

{

Rear->next = PP2;

return Head;

}

if(!PP2)

{

Rear->next = PP1;

return Head;

}

while( PP1 && PP2 )

{

if( PP1->expon > PP2->expon )

{

Polynomial temp = (Polynomial)malloc(sizeof(struct node));

temp->coef = PP1->coef;

temp->expon = PP1->expon;

temp->next = NULL;

Rear->next = temp;

Rear = temp;

PP1 = PP1->next;

}

else if(PP1->expon < PP2->expon )

{

Polynomial temp = (Polynomial)malloc(sizeof(struct node));

temp->coef = PP2->coef;

temp->expon = PP2->expon;

temp->next = NULL;

Rear->next = temp;

Rear = temp;

PP2 = PP2->next;

}

else if( PP1->expon == PP2->expon )

{

if( PP1->coef + PP2->coef == 0 )

{

PP1 = PP1->next;

PP2 = PP2->next;

}

else

{

Polynomial temp = (Polynomial)malloc(sizeof(struct node));

temp->coef = PP1->coef + PP2->coef;

temp->expon = PP1->expon;

temp->next = NULL;

Rear->next = temp;

Rear = temp;

PP1 = PP1->next;

PP2 = PP2->next;

}

}

}

while(PP1)

{

Polynomial temp = (Polynomial)malloc(sizeof(struct node));

temp->coef = PP1->coef;

temp->expon = PP1->expon;

temp->next = NULL;

Rear->next = temp;

Rear = temp;

PP1 = PP1->next;

}

while(PP2)

{

Polynomial temp = (Polynomial)malloc(sizeof(struct node));

temp->coef = PP2->coef;

temp->expon = PP2->expon;

temp->next = NULL;

Rear->next = temp;

Rear = temp;

PP2 = PP2->next;

}

return Head;

}

Polynomial MultPoly( Polynomial P1,Polynomial P2 )

{

Polynomial PP1,PP2,Head,Rear,temp;

int c,e;

if( !P1 || !P2 )

{

return NULL;

}

PP1 = P1->next;

PP2 = P2->next;

Head = (Polynomial)malloc(sizeof(struct node));

Head->next = NULL;

Rear = Head;

while(PP2)

{

Polynomial temp = (Polynomial)malloc(sizeof(struct node));

temp->coef = PP1->coef * PP2->coef;

temp->expon = PP1->expon + PP2->expon;

temp->next = NULL;

Rear->next = temp;

Rear = temp;

PP2 = PP2->next;

}

PP1 = PP1->next;

while(PP1)

{

PP2 = P2->next;

Rear = Head;

while(PP2)

{

e = PP1->expon + PP2->expon;

c = PP1->coef * PP2->coef;

while(Rear->next && Rear->next->expon > e )

{

Rear = Rear->next;

}

if(Rear->next && Rear->next->expon == e )

{

if(Rear->next->coef + c)

{

Rear->next->coef += c;

}

else

{

temp = Rear->next;

Rear->next = temp->next;

free(temp);

}

}

else

{

temp = (Polynomial)malloc(sizeof(struct node));

temp->coef = c;

temp->expon = e;

temp->next = Rear->next;

Rear->next = temp;

Rear = Rear->next;

}

PP2 = PP2->next;

}

PP1 = PP1->next;

}

return Head;

}

int main()

{

int m,n;

Polynomial P1,P2,PS1,PS2;

scanf("%d",&m);

P1 = Read(m);

scanf("%d", &n);

P2 = Read(n);

PS1 = AddPoly(P1,P2);

PS2 = MultPoly(P1,P2);

PrintPoly(PS2);

PrintPoly(PS1);

}

结果是这样的:

我心态崩了,好不容易调试出来又有bug,于是乎只能继续;

/*

设计函数分别求两个一元多项式的乘积与和。

输入格式:

输入分2行,每行分别先给出多项式非零项的个数,再以指数递降方式输入一个多项式非零项系数和指数(绝对值均为不超过1000的整数)。数字间以空格分隔。

输出格式:

输出分2行,分别以指数递降方式输出乘积多项式以及和多项式非零项的系数和指数。数字间以空格分隔,但结尾不能有多余空格。零多项式应输出0 0。

*/

#include <stdio.h>

#include <stdlib.h>

typedef int ElementType;

typedef struct node *Stacknode;

struct node{

int coef;

int expon;

Stacknode next;

};

typedef Stacknode Polynomial;

void PrintPoly(Polynomial P)

{

int flag = 0;

if(!P->next)

{

printf("0 0\n");

return;

}

P = P->next;

while(P)

{

if(!flag)

{

flag = 1;

}

else

{

printf(" ");

}

printf("%d %d",P->coef,P->expon);

P = P->next;

}

printf("\n");

}

Polynomial Read( int n )

{

int i;

Polynomial Head = (Polynomial)malloc(sizeof(struct node));

Polynomial Rear = Head;

Head->next =NULL;

for(i=0;i<n;i++)

{

Polynomial tmp = (Polynomial)malloc(sizeof(struct node));

scanf("%d",&tmp->coef);

scanf("%d",&tmp->expon);

tmp->next = NULL;

Rear->next = tmp;

Rear = tmp;

}

return Head;

}

/*void Attach( int c, int e, Polynomial Rear)

{

Polynomial P;

P = (Polynomial)malloc(sizeof(struct node));

P->coef = c;

P->expon = e;

P->next = NULL;

Rear->next = P;

Rear = P;

}

*/

Polynomial AddPoly( Polynomial P1,Polynomial P2 )

{

Polynomial PP1,PP2,tmp,Head,Rear;

Rear = (Polynomial)malloc(sizeof(struct node));

Rear->next = NULL;

Head = Rear;

PP1 = P1->next;

PP2 = P2->next;

if(!PP1)

{

Rear->next = PP2;

return Head;

}

if(!PP2)

{

Rear->next = PP1;

return Head;

}

while( PP1 && PP2 )

{

if( PP1->expon > PP2->expon )

{

Polynomial temp = (Polynomial)malloc(sizeof(struct node));

temp->coef = PP1->coef;

temp->expon = PP1->expon;

temp->next = NULL;

Rear->next = temp;

Rear = temp;

PP1 = PP1->next;

}

else if(PP1->expon < PP2->expon )

{

Polynomial temp = (Polynomial)malloc(sizeof(struct node));

temp->coef = PP2->coef;

temp->expon = PP2->expon;

temp->next = NULL;

Rear->next = temp;

Rear = temp;

PP2 = PP2->next;

}

else if( PP1->expon == PP2->expon )

{

if( PP1->coef + PP2->coef == 0 )

{

PP1 = PP1->next;

PP2 = PP2->next;

}

else

{

Polynomial temp = (Polynomial)malloc(sizeof(struct node));

temp->coef = PP1->coef + PP2->coef;

temp->expon = PP1->expon;

temp->next = NULL;

Rear->next = temp;

Rear = temp;

PP1 = PP1->next;

PP2 = PP2->next;

}

}

}

while(PP1)

{

Polynomial temp = (Polynomial)malloc(sizeof(struct node));

temp->coef = PP1->coef;

temp->expon = PP1->expon;

temp->next = NULL;

Rear->next = temp;

Rear = temp;

PP1 = PP1->next;

}

while(PP2)

{

Polynomial temp = (Polynomial)malloc(sizeof(struct node));

temp->coef = PP2->coef;

temp->expon = PP2->expon;

temp->next = NULL;

Rear->next = temp;

Rear = temp;

PP2 = PP2->next;

}

return Head;

}

Polynomial MultPoly( Polynomial P1,Polynomial P2 )

{

Polynomial PP1,PP2,Head,Rear,temp;

int c,e;

if( !P1 || !P2 )

{

return NULL;

}

PP1 = P1->next;

PP2 = P2->next;

Head = (Polynomial)malloc(sizeof(struct node));

Head->next = NULL;

Rear = Head;

while(PP2)

{

Polynomial temp = (Polynomial)malloc(sizeof(struct node));

temp->coef = PP1->coef * PP2->coef;

temp->expon = PP1->expon + PP2->expon;

temp->next = NULL;

Rear->next = temp;

Rear = temp;

PP2 = PP2->next;

}

PP1 = PP1->next;

while(PP1)

{

PP2 = P2->next;

Rear = Head;

while(PP2)

{

e = PP1->expon + PP2->expon;

c = PP1->coef * PP2->coef;

while(Rear->next && Rear->next->expon > e )

{

Rear = Rear->next;

}

if(Rear->next && Rear->next->expon == e )

{

if(Rear->next->coef + c)

{

Rear->next->coef += c;

}

else

{

temp = Rear->next;

Rear->next = temp->next;

free(temp);

}

}

else

{

temp = (Polynomial)malloc(sizeof(struct node));

temp->coef = c;

temp->expon = e;

temp->next = Rear->next;

Rear->next = temp;

Rear = Rear->next;

}

PP2 = PP2->next;

}

PP1 = PP1->next;

}

return Head;

}

int main()

{

int m,n;

Polynomial P1,P2,PS1,PS2;

scanf("%d",&m);

P1 = Read(m);

scanf("%d", &n);

P2 = Read(n);

PS1 = AddPoly(P1,P2);

PS2 = MultPoly(P1,P2);

PrintPoly(PS2);

PrintPoly(PS1);

}

mmp还是有一个测试点过不了,沃日,想想这个尿题就来气。

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。