Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- int n,aux,m,aux2;
- struct pol {
- int gr, coef;
- pol *next;
- }*prim,*ultim;
- struct pol2 {
- int gr, coef;
- pol2 *next;
- }*prim2,*ultim2;
- void adaugare_element()
- {
- if(prim == NULL)
- {
- prim = new pol;
- //cout<<"Introduceti gradul:";
- prim->gr=aux;
- cout<<"Introduceti coeficientul lui x la "<<aux<<endl;
- cin>>prim->coef;
- prim->next=NULL;
- ultim=prim;
- aux-=1;
- }
- else
- {
- pol *nou;
- nou = new pol;
- nou->gr=aux;
- cout<<"Introduceti coeficientul lui x la "<<aux<<endl;
- cin>>nou->coef;
- ultim->next=nou;
- ultim=nou;
- ultim->next=NULL;
- aux-=1;
- }
- }
- void adaugare_element_2()
- {
- if(prim2 == NULL)
- {
- prim2 = new pol2;
- //cout<<"Introduceti gradul:";
- prim2->gr=aux2;
- cout<<"Introduceti coeficientul lui x la "<<aux2<<endl;
- cin>>prim2->coef;
- prim2->next=NULL;
- ultim2=prim2;
- aux2-=1;
- }
- else
- {
- pol2 *nou2;
- nou2 = new pol2;
- nou2->gr=aux2;
- cout<<"Introduceti coeficientul lui x la "<<aux2<<endl;
- cin>>nou2->coef;
- ultim2->next=nou2;
- ultim2=nou2;
- ultim2->next=NULL;
- aux2-=1;
- }
- }
- void adaugare_in_fata(int x,int y)
- {
- pol *k;
- k=new pol;
- k->gr = x;
- k->coef = y;
- k->next = prim;
- prim=k;
- }
- void afisare()
- {
- pol *k;
- k=prim; if(!prim) cout <<"aa";
- else{while(k!=NULL)
- {
- cout<<k->gr<<" "<<k->coef<<endl;
- k=k->next;
- }
- cout<<endl;
- }
- }
- void afisare2()
- {
- pol2 *k;
- k=prim2; if(!prim2) cout <<"aa";
- else{while(k!=NULL)
- {
- cout<<k->gr<<" "<<k->coef<<endl;
- k=k->next;
- }
- cout<<endl;
- }
- }
- void suma_polinoamelor()
- {
- if(n>m)
- {
- pol *k = new pol;k=prim;pol2 *k1 = new pol2;k1=prim2;
- while(k->next!=NULL)
- {
- //cout<<"AA";
- if(k->gr==k1->gr) {k->coef=k->coef+k1->coef;
- k=k->next;
- k1=k1->next;
- }
- else k=k->next;
- }
- ultim->coef+=ultim2->coef;
- cout<<"P1+P2: "<<endl;afisare();
- }
- else if(n<m)
- {
- pol *k = new pol;k=prim;pol2 *k1 = new pol2;k1=prim2;
- while(k1->next!=NULL)
- {
- //cout<<"AA";
- if(k->gr==k1->gr) {k1->coef=k1->coef+k->coef;
- k=k->next;
- k1=k1->next;
- }
- else k1=k1->next;
- }
- ultim2->coef+=ultim->coef;
- cout<<"P1+P2: "<<endl;afisare2();
- }
- else
- {
- pol *k = new pol;k=prim;pol2 *k1 = new pol2;k1=prim2;
- while(k1->next!=NULL)
- {
- //cout<<"AA";
- if(k->gr==k1->gr) {k1->coef=k1->coef+k->coef;
- k=k->next;
- k1=k1->next;
- }
- else k1=k1->next;
- }
- ultim2->coef+=ultim->coef;
- cout<<"P1+P2: "<<endl;afisare2();
- }
- }
- int main()
- {
- cout<<"Introduceti gradul primului polinom: ";cin>>n;aux=n;
- for(int i=0;i<=n;i++) adaugare_element();
- afisare();
- cout<<"Introduceti gradul celui de-al doilea polinom: ";cin>>m;aux2=m;
- for(int j=0;j<=m;j++) adaugare_element_2();
- afisare2();
- suma_polinoamelor();//cout<<"-----------------------"<<endl;
- // afisare();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement