Advertisement
Guest User

Untitled

a guest
Jun 22nd, 2017
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.58 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. int n,aux,m,aux2;
  6.  
  7. struct pol {
  8. int gr, coef;
  9. pol *next;
  10. }*prim,*ultim;
  11.  
  12. struct pol2 {
  13. int gr, coef;
  14. pol2 *next;
  15. }*prim2,*ultim2;
  16.  
  17.  
  18.  
  19. void adaugare_element()
  20. {
  21.     if(prim == NULL)
  22.     {
  23.         prim = new pol;
  24.         //cout<<"Introduceti gradul:";
  25.         prim->gr=aux;
  26.         cout<<"Introduceti coeficientul lui x la "<<aux<<endl;
  27.         cin>>prim->coef;
  28.         prim->next=NULL;
  29.         ultim=prim;
  30.         aux-=1;
  31.  
  32.     }
  33.     else
  34.     {
  35.  
  36.          pol *nou;
  37.          nou = new pol;
  38.          nou->gr=aux;
  39.         cout<<"Introduceti coeficientul lui x la "<<aux<<endl;
  40.             cin>>nou->coef;
  41.          ultim->next=nou;
  42.          ultim=nou;
  43.          ultim->next=NULL;
  44.          aux-=1;
  45.      }
  46. }
  47.  
  48.  
  49.  
  50. void adaugare_element_2()
  51. {
  52.     if(prim2 == NULL)
  53.     {
  54.         prim2 = new pol2;
  55.         //cout<<"Introduceti gradul:";
  56.         prim2->gr=aux2;
  57.         cout<<"Introduceti coeficientul lui x la "<<aux2<<endl;
  58.         cin>>prim2->coef;
  59.         prim2->next=NULL;
  60.         ultim2=prim2;
  61.         aux2-=1;
  62.  
  63.     }
  64.     else
  65.     {
  66.  
  67.          pol2 *nou2;
  68.          nou2 = new pol2;
  69.          nou2->gr=aux2;
  70.         cout<<"Introduceti coeficientul lui x la "<<aux2<<endl;
  71.             cin>>nou2->coef;
  72.          ultim2->next=nou2;
  73.          ultim2=nou2;
  74.          ultim2->next=NULL;
  75.          aux2-=1;
  76.      }
  77. }
  78.  
  79. void adaugare_in_fata(int x,int y)
  80. {
  81.      pol *k;
  82.      k=new pol;
  83.      k->gr = x;
  84.      k->coef = y;
  85.      k->next = prim;
  86.      prim=k;
  87. }
  88.  
  89.  
  90.  
  91. void afisare()
  92. {
  93.      pol *k;
  94.      k=prim; if(!prim) cout <<"aa";
  95.      else{while(k!=NULL)
  96.      {
  97.                    cout<<k->coef<<"x^"<<k->gr<<"+";
  98.                    k=k->next;
  99.      }
  100.      cout<<endl;
  101.      }
  102.  
  103. }
  104.  
  105. void afisare2()
  106. {
  107.      pol2 *k;
  108.      k=prim2; if(!prim2) cout <<"aa";
  109.      else{while(k!=NULL)
  110.      {
  111.                    cout<<k->coef<<"x^"<<k->gr<<"+";
  112.                    k=k->next;
  113.      }
  114.      cout<<endl;
  115.      }
  116.  
  117. }
  118.  
  119. void suma_polinoamelor()
  120. {
  121.     if(n>m)
  122.     {
  123.          pol *k = new pol;k=prim;pol2 *k1 = new pol2;k1=prim2;
  124.         while(k->next!=NULL)
  125.         {
  126.             //cout<<"AA";
  127.             if(k->gr==k1->gr) {k->coef=k->coef+k1->coef;
  128.             k=k->next;
  129.             k1=k1->next;
  130.             }
  131.             else k=k->next;
  132.  
  133.  
  134.         }
  135.         ultim->coef+=ultim2->coef;
  136.         cout<<"P1+P2: "<<endl;afisare();
  137.     }
  138.     else if(n<m)
  139.     {
  140.         pol *k = new pol;k=prim;pol2 *k1 = new pol2;k1=prim2;
  141.         while(k1->next!=NULL)
  142.         {
  143.             //cout<<"AA";
  144.             if(k->gr==k1->gr) {k1->coef=k1->coef+k->coef;
  145.             k=k->next;
  146.             k1=k1->next;
  147.             }
  148.             else k1=k1->next;
  149.  
  150.  
  151.         }
  152.         ultim2->coef+=ultim->coef;
  153.         cout<<"P1+P2: "<<endl;afisare2();
  154.     }
  155.     else
  156.     {
  157.         pol *k = new pol;k=prim;pol2 *k1 = new pol2;k1=prim2;
  158.         while(k1->next!=NULL)
  159.         {
  160.             //cout<<"AA";
  161.             if(k->gr==k1->gr) {k1->coef=k1->coef+k->coef;
  162.             k=k->next;
  163.             k1=k1->next;
  164.             }
  165.             else k1=k1->next;
  166.  
  167.  
  168.         }
  169.         ultim2->coef+=ultim->coef;
  170.         cout<<"P1+P2: "<<endl;afisare2();
  171.     }
  172. }
  173.  
  174.  
  175. int main()
  176. {
  177.     cout<<"Introduceti gradul primului polinom: ";cin>>n;aux=n;
  178.  
  179.     for(int i=0;i<=n;i++) adaugare_element();
  180.    // afisare();
  181.      cout<<"Introduceti gradul celui de-al doilea polinom: ";cin>>m;aux2=m;
  182.     for(int j=0;j<=m;j++) adaugare_element_2();
  183.    // afisare2();
  184.     suma_polinoamelor();//cout<<"-----------------------"<<endl;
  185.   //  afisare();
  186. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement