Advertisement
lodha1503

Untitled

Jan 17th, 2022
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.18 KB | None | 0 0
  1. # include <stdio.h>
  2. # include <stdlib.h>
  3. #include <math.h>
  4.  
  5. struct ListNode {
  6.     int pow;
  7.     int coeff;
  8.     struct ListNode *next;
  9. };
  10.  
  11. int main()
  12. {
  13.    
  14.     int s1=0,s2=0,s3=0;
  15.     // creating 1st polynomial        
  16.     struct ListNode *head1,*temp1;
  17.     head1=(struct ListNode*)malloc(sizeof(struct ListNode));  
  18.     int N1;scanf("%d",&N1);
  19.     int n1= N1;
  20.     head1->pow=n1;
  21.     n1--;
  22.     int y1;
  23.     scanf("%d",&y1);
  24.     head1->coeff=y1;
  25.     head1->next=NULL;
  26.     temp1=head1;
  27.     for(int i=1;i<N1+1;i++)
  28.     {
  29.         struct ListNode *current1;
  30.         current1=(struct ListNode*)malloc(sizeof(struct ListNode));
  31.         current1->pow=n1;
  32.         n1--;
  33.         int x1;
  34.         scanf("%d",&x1);
  35.         current1->coeff=x1;
  36.         temp1->next=current1;
  37.         temp1=temp1->next;  
  38.     }
  39.     temp1->next=NULL;
  40.  
  41.     // creating 2nd polynomial
  42.     struct ListNode *head2,*temp2;
  43.     head2=(struct ListNode*)malloc(sizeof(struct ListNode));
  44.     int N2; scanf("%d",&N2);
  45.     int n2= N2; head2->pow=n2; n2--;
  46.     int y2; scanf("%d",&y2);
  47.     head2->coeff=y2; head2->next=NULL;
  48.     temp2=head2;
  49.     for(int i=1;i<N2+1;i++)
  50.     {
  51.         struct ListNode *current2;
  52.         current2=(struct ListNode*)malloc(sizeof(struct ListNode));
  53.         current2->pow=n2; n2--;
  54.         int x2; scanf("%d",&x2);
  55.         current2->coeff=x2;
  56.         temp2->next=current2; temp2=temp2->next;  
  57.     }
  58.     temp2->next=NULL;
  59.  
  60.    int x;
  61.    scanf("%d",&x);
  62.  
  63.    printf("P1(x) = ");  
  64.    struct ListNode *temp3;
  65.    temp3=head1;
  66.    for(int i=0;i<N1+1;i++)
  67.    {
  68.         int p1= temp3->coeff;
  69.         int q1=temp3->pow;
  70.        
  71.         s1=s1+(p1*pow(x,q1));
  72.         temp3=temp3->next;
  73.         if (i==0)
  74.         {
  75.             if (N1==0)
  76.             {
  77.                 printf("%d",p1);
  78.             }
  79.             else
  80.             {
  81.                 printf("%dx^%d",p1,q1);
  82.             }
  83.         }
  84.         else
  85.         {  
  86.             if (p1>0)
  87.             {
  88.                 if(q1!=0)
  89.                 {
  90.                     printf(" + %dx^%d",p1,q1);
  91.                 }
  92.                 else if (q1==0)
  93.                 {
  94.                     printf(" + %d",p1);
  95.                 }
  96.                
  97.             }
  98.             else if (p1<0)
  99.             {
  100.                 if (q1!=0)
  101.                 {
  102.                 printf(" - %dx^%d",abs(p1),q1);
  103.                 }
  104.                 else if (q1==0)
  105.                 {
  106.                     printf(" - %d",abs(p1));
  107.                 }
  108.                
  109.             }
  110.         }  
  111.    }
  112.    printf("\n");
  113.  
  114.  
  115.    printf("P2(x) = ");  
  116.    struct ListNode *temp4;
  117.    temp4=head2;
  118.    for(int i=0;i<N2+1;i++)
  119.    {
  120.         int p2= temp4->coeff;
  121.         int q2=temp4->pow;
  122.         s2=s2+(p2*pow(x,q2));
  123.         temp4=temp4->next;
  124.         if (i==0)
  125.         {
  126.             if (N2==0)
  127.             {
  128.                 printf("%d",p2);
  129.             }
  130.             else
  131.             {
  132.                 printf("%dx^%d",p2,q2);
  133.             }
  134.         }
  135.         else
  136.         {  
  137.             if (p2>0)
  138.             {
  139.                 if(q2!=0)
  140.                 {
  141.                     printf(" + %dx^%d",p2,q2);
  142.                 }
  143.                 else if (q2==0)
  144.                 {
  145.                     printf(" + %d",p2);
  146.                 }
  147.                
  148.             }
  149.             else if (p2<0)
  150.             {
  151.                 if (q2!=0)
  152.                 {
  153.                 printf(" - %dx^%d",abs(p2),q2);
  154.                 }
  155.                 else if (q2==0)
  156.                 {
  157.                     printf(" - %d",abs(p2));
  158.                 }
  159.                
  160.             }
  161.         }  
  162.    }
  163.    printf("\n");
  164.  
  165.    struct ListNode *temp5;
  166.    temp5=head1;
  167.     for(int i=0;i<N1+1;i++)
  168.     {
  169.         temp5->pow=(temp5->pow)+N2+1;
  170.         temp5=temp5->next;
  171.     }
  172.     printf("hi\n");
  173.  
  174.  
  175.     struct ListNode *temp6;
  176.     temp6=head1;
  177.    
  178.     for(int i=0;i<N1+1;i++)
  179.     {
  180.        
  181.         temp6=temp6->next;
  182.         if (temp6->next==NULL)
  183.         {
  184.             temp6->next=head2;
  185.         }
  186.     }
  187.    
  188.     printf("hi\n");
  189.     struct ListNode *temp7;
  190.     temp7=head1;
  191.     printf("P3(x) = ");
  192.     for(int i=0;i<N1+N2+2;i++)
  193.     {
  194.         int p= temp7->coeff;
  195.         int q=temp7->pow;
  196.         s3=s3+(p*pow(x,q));
  197.         temp7=temp7->next;
  198.         if (i==0)
  199.         {
  200.             printf("%dx^%d",p,q);
  201.         }
  202.         else
  203.         {  
  204.             if (p>0)
  205.             {
  206.                 if(q!=0)
  207.                 {
  208.                     printf(" + %dx^%d",p,q);
  209.                 }
  210.                 else if (q==0)
  211.                 {
  212.                     printf(" + %d",p);
  213.                 }
  214.                
  215.             }
  216.             else if (p<0)
  217.             {
  218.                 if (q!=0)
  219.                 {
  220.                 printf(" - %dx^%d",abs(p),q);
  221.                 }
  222.                 else if (q==0)
  223.                 {
  224.                     printf(" - %d",abs(p));
  225.                 }
  226.                
  227.             }
  228.         }  
  229.     }
  230.     printf("\n");
  231.  
  232.     printf("P1(%d) = %d\n",x,s1);  
  233.     printf("P2(%d) = %d\n",x,s2);  
  234.     printf("P3(%d) = %d\n",x,s3);
  235.  
  236.  
  237.          
  238.    
  239.  
  240.     return 0;
  241.  
  242.    
  243.  
  244.  
  245.  
  246.    
  247. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement