Advertisement
Guest User

Untitled

a guest
Mar 27th, 2017
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.19 KB | None | 0 0
  1. #include <iostream>
  2. #include <conio.h>
  3. #include <math.h>
  4. using namespace std;
  5. struct DonThuc
  6. {
  7.     float heso;
  8.     int somu;
  9. };
  10. struct node
  11. {
  12.     DonThuc info;
  13.     node *next;
  14. };
  15. struct DaThuc
  16. {
  17.     node *head;
  18.     node *tail;
  19. };
  20. node* getnode(DonThuc x)
  21. {
  22.     node *p;
  23.     p=new node;
  24.     if(p==NULL)
  25.     {
  26.         cout<<"\n khong du bo nho";
  27.         return NULL;
  28.     }
  29.     p->info=x;
  30.     p->next=NULL;
  31.     return p;
  32. }
  33. void khoitao(DaThuc &L)
  34. {
  35.     L.head=NULL;
  36.     L.tail=NULL;
  37. }
  38. void addhead(DaThuc &L,node *p)
  39. {
  40.     if(L.tail==NULL)
  41.     {
  42.         L.head=p;
  43.         L.tail=p;
  44.     }
  45.     else
  46.     {
  47.         p->next=L.head;
  48.         L.head=p;
  49.     }
  50.  
  51. }
  52. void themdau(DaThuc &L,DonThuc x)
  53. {
  54.     node *p;
  55.     p=getnode(x);
  56.     addhead(L,p);
  57. }
  58.  
  59. void nhap(DonThuc &x)
  60. {
  61.     cout<<"\n Nhap Vao Don Thuc";
  62.     cout<<"\nNhap vao he so: ";
  63.     cin>>x.heso;
  64.     cout<<"\nNhap so mu: ";
  65.     cin>>x.somu;
  66. }
  67. void nhapDaThuc(DaThuc &L)
  68. {
  69.     int n;
  70.     DonThuc x;
  71.     node *p;
  72.     p=L.head;
  73.     cout<<"Nhap Da Thuc\n";
  74.     cout<<"nhap vao so luong don thuc: ";
  75.     cin>>n;
  76.     for(int i=0;i<n;i++)
  77.     {
  78.         cout<<"\n Don Thuc "<<i+1 <<" = ";
  79.         nhap(x);
  80.    
  81.     themdau(L,x);
  82.     }
  83. }
  84. void congdathuc(DaThuc L,DaThuc L1,DaThuc &T)
  85. {
  86.     node *p,*q;
  87.     int flag;
  88.     p=L.head;
  89.     while(p!=NULL)
  90.     {
  91.         themdau(T,p->info);
  92.         p=p->next;
  93.     }
  94.     p=L1.head;
  95.     q=T.head;
  96.     while (q!=NULL)
  97.     {
  98.         p=L1.head;
  99.         while (p!=NULL)
  100.         {
  101.             if(p->info.somu==q->info.somu)
  102.             q->info.heso+=p->info.heso;
  103.             p=p->next;
  104.         }
  105.         q=q->next;
  106.     }
  107.    
  108.     p=L1.head;
  109.     while (p!=NULL)
  110.     {
  111.         flag=0;
  112.         q=T.head;
  113.         while (q!=NULL)
  114.         {
  115.             if(q->info.somu==p->info.somu)
  116.             flag=1;
  117.             q=q->next;
  118.         }
  119.         if(flag==0)
  120.             themdau(T,p->info);
  121.         p=p->next;
  122.         }
  123.  
  124. }
  125. void trudathuc(DaThuc &L,DaThuc &L1,DaThuc &H)
  126. {
  127.     node *p,*q;
  128.     int flag;
  129.     p=L.head;
  130.     while(p!=NULL)
  131.     {
  132.         themdau(H,p->info);
  133.         p=p->next;
  134.     }
  135.     p=L1.head;
  136.     q=H.head;
  137.     while (q!=NULL)
  138.     {
  139.         p=L1.head;
  140.         while (p!=NULL)
  141.         {
  142.             if(p->info.somu==q->info.somu)
  143.             q->info.heso-=p->info.heso;
  144.             p=p->next;
  145.         }
  146.         q=q->next;
  147.     }
  148.    
  149.     p=L1.head;
  150.     while (p!=NULL)
  151.     {
  152.         flag=0;
  153.         q=H.head;
  154.         while (q!=NULL)
  155.         {
  156.             if(q->info.somu==p->info.somu)
  157.                 flag=1;
  158.             q=q->next;
  159.         }
  160.         if(flag==0)
  161.         {
  162.             p->info.heso*=-1;
  163.             themdau(H,p->info);
  164.             p->info.heso*=-1;
  165.         }
  166.         p=p->next;
  167.     }
  168.  
  169. }
  170. void nhandathuc(DaThuc &L,DaThuc &L1,DaThuc &Ti)
  171. {
  172.     node *p,*q;
  173.     int flag;
  174.     p=L.head;
  175.     while(p!=NULL)
  176.     {
  177.         themdau(Ti,p->info);
  178.         p=p->next;
  179.     }
  180.     p=L1.head;
  181.     q=Ti.head;
  182.     while (q!=NULL)
  183.     {
  184.         p=L1.head;
  185.         while (p!=NULL)
  186.         {
  187.             q->info.heso*=p->info.heso;
  188.             q->info.somu+=p->info.somu;
  189.             p=p->next;
  190.         }
  191.         q=q->next;
  192.     }
  193. }
  194. void xuatDaThuc(DaThuc L)
  195. {
  196.         cout<<"\n";
  197.         node *p;
  198.         p=L.head;
  199.         while(p!=NULL)
  200.         {
  201.             cout<<(p->info).heso<<"x^"<<(p->info).somu;
  202.             if(p->next!=NULL)
  203.                 cout<<"+";
  204.             p=p->next;
  205.         }  
  206. }
  207. main()
  208. {
  209.     DaThuc L,L1,T,H,Ti;
  210.     khoitao(L);
  211.     khoitao(L1);
  212.     khoitao(T);
  213.     khoitao(H);
  214.     khoitao(Ti);
  215.     DonThuc x;
  216.     nhapDaThuc(L);
  217.     cout<<"\n Da thuc can cong ";
  218.     nhapDaThuc(L1);
  219.     cout<<"\n Hai da thuc ";
  220.     xuatDaThuc(L);cout<<"\n";
  221.     xuatDaThuc(L1);
  222.     congdathuc(L,L1,T);
  223.     xuatDaThuc(T);
  224.     cout<<"\nDa thuc 1 tru da thuc 2";
  225.     trudathuc(L,L1,H);
  226.     xuatDaThuc(H);
  227.     cout<<"\n Da thuc tich \n";
  228.     nhandathuc(L,L1,Ti);
  229.     xuatDaThuc(Ti);
  230.     getch();
  231. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement