Advertisement
apl-mhd

Bappy sir firstproblme

Mar 17th, 2017
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.65 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdio>
  3.  
  4. using namespace std;
  5.  
  6.  
  7. struct polynomial{
  8.  
  9.     int data;
  10.     int data2;
  11.     struct polynomial *next;
  12.  
  13. };
  14. typedef struct polynomial node;
  15.  
  16. void insert(node *head[]){
  17.  
  18.         node *temp, *prev;
  19.  
  20.         string str;
  21.         int len;
  22.  
  23.  
  24.     for(int i=0; i<2; i++){
  25.         head[i] = new node();
  26.         prev= head[i];
  27.         cin>>str;
  28.  
  29.         len = str.size();
  30.         str[len]='0';
  31.  
  32.         for(int j=0; j<len+1; j+=2){
  33.  
  34.             temp = new node();
  35.             temp->data = (int) str[j]-'0';
  36.             temp->data2 = (int) str[j+1]-'0';
  37.             temp->next=NULL;
  38.             prev->next = temp;
  39.             prev=temp;
  40.  
  41.         }
  42.  
  43.     }
  44.  
  45. }
  46.  
  47.  
  48. void sumPolynomial(node *head[]){
  49.  
  50.     node *temp, *temp2;
  51.     temp=head[0]->next;
  52.  
  53.     while(temp!=NULL){
  54.  
  55.         temp2=head[1]->next;
  56.  
  57.         while(temp2 !=NULL){
  58.  
  59.             if(temp->data2 == temp2->data2){
  60.                 temp->data+=temp2->data;
  61.                 break;
  62.             }
  63.  
  64.         temp2=temp2->next;
  65.  
  66.         }
  67.        // printf("%d%d ", temp->data,temp->data2);
  68.  
  69.         temp=temp->next;
  70.  
  71.     }
  72. }
  73.  
  74. void display(node *head[]){
  75.  
  76.     node *temp;
  77.  
  78.     temp=head[0]->next;
  79.  
  80.     while(temp !=NULL){
  81.  
  82.         if(temp->data2 == 0)
  83.             printf("%d",temp->data);
  84.         else
  85.             printf("%dx^%d+",temp->data, temp->data2);
  86.         temp=temp->next;
  87.     }
  88.  
  89. }
  90.  
  91. int main(int argc, char **argv)
  92. {
  93.  
  94.     node *head[2];
  95.     node *sum;
  96.  
  97.     //head= new node();
  98.  
  99.      insert(head);
  100.  
  101.     sumPolynomial(head);
  102.  
  103.     display(head);
  104.    // printf("%d", head[0]->data);
  105.  
  106.  
  107.  
  108.     return 0;
  109. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement