Guest User

Untitled

a guest
Apr 23rd, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.94 KB | None | 0 0
  1. // cw08_2.cpp : Defines the entry point for the console application.
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include <iostream>
  6. #include <cmath>
  7. using namespace std;
  8.  
  9. struct lista{
  10.     float x;
  11.     lista* next;
  12.     lista* prev;
  13. };
  14.  
  15.  
  16. int* i_tab(int n){
  17.     int *p;
  18.     p=new int [n];
  19.     return p;
  20. }
  21. void wpisywanie(int *tab, int n){
  22.     for(int i=0;i<n;i++){
  23.         cin>>tab[i];
  24.     }
  25. }
  26.  
  27. int ile_dzielnikow(int a){
  28.     int temp=0;
  29.     for(int i=1;i<=a;i++){
  30.         if(a%i==0){
  31.             temp++;
  32.         }
  33.     }
  34.     return temp;
  35. }
  36. void dzielniki(int *tab, int a){
  37.     int temp=0;
  38.     for(int i=1;i<=a;i++){
  39.         if(a%i==0){
  40.             tab[temp]=i;
  41.             temp++;
  42.         }
  43.     }
  44. }
  45.  
  46.  
  47. bool spr(lista* &head, float a){
  48.     lista* p;
  49.     p=head;
  50.     if(head==NULL){
  51.         return true;
  52.     }
  53.     do{
  54.     if(p->x==a){
  55.         return false;
  56.     }
  57.     p=p->next;
  58.     }while(p!=NULL);
  59.     return true;
  60. }
  61.  
  62. void dodaj(lista* &head, lista* last, float x){
  63.     lista* el;
  64.     el=head;
  65.     if(head==NULL){
  66.         el=new lista;
  67.         el->x=x;
  68.         el->next=NULL;
  69.         el->prev=NULL;
  70.         head=el;
  71.         last=el;
  72.     }else{
  73.         el=new lista;
  74.         el->x=x;
  75.         el->next=head;
  76.         el->prev=NULL;
  77.         head->prev=el;
  78.         last=el;
  79.         head=el;
  80.     }
  81. }
  82.  
  83. int dodawanie(int *tab_a, int *tab_b, int a, int b, lista* &head, lista* &last){
  84.     int temp=0;
  85.     for(int i=0;i<a;i++){
  86.         for(int j=0;j<b;j++){
  87.             if(spr(head, float(tab_a[i])/tab_b[j])){
  88.                 dodaj(head,last,float(tab_a[i])/tab_b[j]);
  89.                 temp++;
  90.             }
  91.             if(spr(head, float(-tab_a[i])/tab_b[j])){
  92.                 dodaj(head,last,float(-tab_a[i])/tab_b[j]);
  93.             }
  94.         }
  95.     }
  96.     return temp;
  97. }
  98.  
  99. void pierwiastki_wielomianu(int *tab, lista* &head, int n, int ile){
  100.     lista* p;
  101.     p=head;
  102.     float a=0.0;
  103.     do{
  104.             for(int j=0;j<n;j++){
  105.                 if(j==0){
  106.                     a+=tab[j];
  107.                 }else{
  108.                     a+=tab[j]*pow(float(p->x),j);
  109.                 }
  110.             }
  111.             if(a==0.0){
  112.                 cout<<p->x<<endl;
  113.             }
  114.             else{
  115.                 a=0.0;
  116.         }
  117.         p=p->next;
  118.     }while(p!=NULL);
  119. }
  120.  
  121. void pokaz(lista* &head){
  122.     lista *p;
  123.     p=head;
  124.     do{
  125.         cout<<p->x<<endl;
  126.         p=p->next;
  127.     }while(p!=NULL);
  128. }
  129.  
  130. void sortowanie(lista* head, int ile){
  131.     lista* p=head,*q;
  132.     q=p->next;
  133.     int licznik=0;
  134.     for(int i=0;i<ile;i++){
  135.     do{
  136.         if(p->x>q->x){
  137.             swap(p->x,q->x);
  138.             licznik++;
  139.         }
  140.         p=p->next;
  141.         q=p->next;
  142.     }while(q!=NULL);
  143.     if(licznik==0){
  144.         break;
  145.     }else{
  146.         licznik=0;
  147.     }
  148.     p=head;
  149.     q=p->next;
  150.     }
  151. }
  152.  
  153. int _tmain(int argc, _TCHAR* argv[])
  154. {
  155.     lista* head=NULL,*last=NULL;
  156.     int n,*tab_a,*tab_b,*tab,a,b,ile_a,ile_b,ile;
  157.     printf("Podaj stopien wielomianu:");
  158.     cin>>n;
  159.     n+=1;
  160.     tab=i_tab(n);
  161.     cout<<"Podaj poszczegolne wartosci wielomianu:"<<endl;
  162.     wpisywanie(tab,n);
  163.     a=tab[0];
  164.     b=tab[n-1];
  165.     ile_a=ile_dzielnikow(abs(a));
  166.     ile_b=ile_dzielnikow(abs(b));
  167.     tab_a=i_tab(ile_a);
  168.     tab_b=i_tab(ile_b);
  169.     dzielniki(tab_a,abs(a));
  170.     dzielniki(tab_b,abs(b));
  171.     cout<<endl;
  172.     ile=dodawanie(tab_a,tab_b,ile_a,ile_b,head,last);
  173.     sortowanie(head,ile);
  174.     cout<<"Mozliwe pierwiastki wielomianu:"<<endl;
  175.     pokaz(head);
  176.     cout<<endl<<endl;
  177.     cout<<"Pierwiastki wielomianu:"<<endl;
  178.     pierwiastki_wielomianu(tab,head,n,ile);
  179.     return 0;
  180. }
Add Comment
Please, Sign In to add comment