Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # include <stdio.h>
- # include <stdlib.h>
- #include <math.h>
- struct ListNode {
- int pow;
- int coeff;
- struct ListNode *next;
- };
- int main()
- {
- int s1=0,s2=0,s3=0;
- // creating 1st polynomial
- struct ListNode *head1,*temp1;
- head1=(struct ListNode*)malloc(sizeof(struct ListNode));
- int N1;scanf("%d",&N1);
- int n1= N1;
- head1->pow=n1;
- n1--;
- int y1;
- scanf("%d",&y1);
- head1->coeff=y1;
- head1->next=NULL;
- temp1=head1;
- for(int i=1;i<N1+1;i++)
- {
- struct ListNode *current1;
- current1=(struct ListNode*)malloc(sizeof(struct ListNode));
- current1->pow=n1;
- n1--;
- int x1;
- scanf("%d",&x1);
- current1->coeff=x1;
- temp1->next=current1;
- temp1=temp1->next;
- }
- temp1->next=NULL;
- // creating 2nd polynomial
- struct ListNode *head2,*temp2;
- head2=(struct ListNode*)malloc(sizeof(struct ListNode));
- int N2; scanf("%d",&N2);
- int n2= N2; head2->pow=n2; n2--;
- int y2; scanf("%d",&y2);
- head2->coeff=y2; head2->next=NULL;
- temp2=head2;
- for(int i=1;i<N2+1;i++)
- {
- struct ListNode *current2;
- current2=(struct ListNode*)malloc(sizeof(struct ListNode));
- current2->pow=n2; n2--;
- int x2; scanf("%d",&x2);
- current2->coeff=x2;
- temp2->next=current2; temp2=temp2->next;
- }
- temp2->next=NULL;
- int x;
- scanf("%d",&x);
- printf("P1(x) = ");
- struct ListNode *temp3;
- temp3=head1;
- for(int i=0;i<N1+1;i++)
- {
- int p1= temp3->coeff;
- int q1=temp3->pow;
- s1=s1+(p1*pow(x,q1));
- temp3=temp3->next;
- if (i==0)
- {
- if (N1==0)
- {
- printf("%d",p1);
- }
- else
- {
- printf("%dx^%d",p1,q1);
- }
- }
- else
- {
- if (p1>0)
- {
- if(q1!=0)
- {
- printf(" + %dx^%d",p1,q1);
- }
- else if (q1==0)
- {
- printf(" + %d",p1);
- }
- }
- else if (p1<0)
- {
- if (q1!=0)
- {
- printf(" - %dx^%d",abs(p1),q1);
- }
- else if (q1==0)
- {
- printf(" - %d",abs(p1));
- }
- }
- }
- }
- printf("\n");
- printf("P2(x) = ");
- struct ListNode *temp4;
- temp4=head2;
- for(int i=0;i<N2+1;i++)
- {
- int p2= temp4->coeff;
- int q2=temp4->pow;
- s2=s2+(p2*pow(x,q2));
- temp4=temp4->next;
- if (i==0)
- {
- if (N2==0)
- {
- printf("%d",p2);
- }
- else
- {
- printf("%dx^%d",p2,q2);
- }
- }
- else
- {
- if (p2>0)
- {
- if(q2!=0)
- {
- printf(" + %dx^%d",p2,q2);
- }
- else if (q2==0)
- {
- printf(" + %d",p2);
- }
- }
- else if (p2<0)
- {
- if (q2!=0)
- {
- printf(" - %dx^%d",abs(p2),q2);
- }
- else if (q2==0)
- {
- printf(" - %d",abs(p2));
- }
- }
- }
- }
- printf("\n");
- struct ListNode *temp5;
- temp5=head1;
- for(int i=0;i<N1+1;i++)
- {
- temp5->pow=(temp5->pow)+N2+1;
- temp5=temp5->next;
- }
- printf("hi\n");
- struct ListNode *temp6;
- temp6=head1;
- for(int i=0;i<N1+1;i++)
- {
- temp6=temp6->next;
- if (temp6->next==NULL)
- {
- temp6->next=head2;
- }
- }
- printf("hi\n");
- struct ListNode *temp7;
- temp7=head1;
- printf("P3(x) = ");
- for(int i=0;i<N1+N2+2;i++)
- {
- int p= temp7->coeff;
- int q=temp7->pow;
- s3=s3+(p*pow(x,q));
- temp7=temp7->next;
- if (i==0)
- {
- printf("%dx^%d",p,q);
- }
- else
- {
- if (p>0)
- {
- if(q!=0)
- {
- printf(" + %dx^%d",p,q);
- }
- else if (q==0)
- {
- printf(" + %d",p);
- }
- }
- else if (p<0)
- {
- if (q!=0)
- {
- printf(" - %dx^%d",abs(p),q);
- }
- else if (q==0)
- {
- printf(" - %d",abs(p));
- }
- }
- }
- }
- printf("\n");
- printf("P1(%d) = %d\n",x,s1);
- printf("P2(%d) = %d\n",x,s2);
- printf("P3(%d) = %d\n",x,s3);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement