Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // cw08_2.cpp : Defines the entry point for the console application.
- //
- #include "stdafx.h"
- #include <iostream>
- #include <cmath>
- using namespace std;
- struct lista{
- float x;
- lista* next;
- lista* prev;
- };
- int* i_tab(int n){
- int *p;
- p=new int [n];
- return p;
- }
- void wpisywanie(int *tab, int n){
- for(int i=0;i<n;i++){
- cin>>tab[i];
- }
- }
- int ile_dzielnikow(int a){
- int temp=0;
- for(int i=1;i<=a;i++){
- if(a%i==0){
- temp++;
- }
- }
- return temp;
- }
- void dzielniki(int *tab, int a){
- int temp=0;
- for(int i=1;i<=a;i++){
- if(a%i==0){
- tab[temp]=i;
- temp++;
- }
- }
- }
- bool spr(lista* &head, float a){
- lista* p;
- p=head;
- if(head==NULL){
- return true;
- }
- do{
- if(p->x==a){
- return false;
- }
- p=p->next;
- }while(p!=NULL);
- return true;
- }
- void dodaj(lista* &head, lista* last, float x){
- lista* el;
- el=head;
- if(head==NULL){
- el=new lista;
- el->x=x;
- el->next=NULL;
- el->prev=NULL;
- head=el;
- last=el;
- }else{
- el=new lista;
- el->x=x;
- el->next=head;
- el->prev=NULL;
- head->prev=el;
- last=el;
- head=el;
- }
- }
- int dodawanie(int *tab_a, int *tab_b, int a, int b, lista* &head, lista* &last){
- int temp=0;
- for(int i=0;i<a;i++){
- for(int j=0;j<b;j++){
- if(spr(head, float(tab_a[i])/tab_b[j])){
- dodaj(head,last,float(tab_a[i])/tab_b[j]);
- temp++;
- }
- if(spr(head, float(-tab_a[i])/tab_b[j])){
- dodaj(head,last,float(-tab_a[i])/tab_b[j]);
- }
- }
- }
- return temp;
- }
- void pierwiastki_wielomianu(int *tab, lista* &head, int n, int ile){
- lista* p;
- p=head;
- float a=0.0;
- do{
- for(int j=0;j<n;j++){
- if(j==0){
- a+=tab[j];
- }else{
- a+=tab[j]*pow(float(p->x),j);
- }
- }
- if(a==0.0){
- cout<<p->x<<endl;
- }
- else{
- a=0.0;
- }
- p=p->next;
- }while(p!=NULL);
- }
- void pokaz(lista* &head){
- lista *p;
- p=head;
- do{
- cout<<p->x<<endl;
- p=p->next;
- }while(p!=NULL);
- }
- void sortowanie(lista* head, int ile){
- lista* p=head,*q;
- q=p->next;
- int licznik=0;
- for(int i=0;i<ile;i++){
- do{
- if(p->x>q->x){
- swap(p->x,q->x);
- licznik++;
- }
- p=p->next;
- q=p->next;
- }while(q!=NULL);
- if(licznik==0){
- break;
- }else{
- licznik=0;
- }
- p=head;
- q=p->next;
- }
- }
- int _tmain(int argc, _TCHAR* argv[])
- {
- lista* head=NULL,*last=NULL;
- int n,*tab_a,*tab_b,*tab,a,b,ile_a,ile_b,ile;
- printf("Podaj stopien wielomianu:");
- cin>>n;
- n+=1;
- tab=i_tab(n);
- cout<<"Podaj poszczegolne wartosci wielomianu:"<<endl;
- wpisywanie(tab,n);
- a=tab[0];
- b=tab[n-1];
- ile_a=ile_dzielnikow(abs(a));
- ile_b=ile_dzielnikow(abs(b));
- tab_a=i_tab(ile_a);
- tab_b=i_tab(ile_b);
- dzielniki(tab_a,abs(a));
- dzielniki(tab_b,abs(b));
- cout<<endl;
- ile=dodawanie(tab_a,tab_b,ile_a,ile_b,head,last);
- sortowanie(head,ile);
- cout<<"Mozliwe pierwiastki wielomianu:"<<endl;
- pokaz(head);
- cout<<endl<<endl;
- cout<<"Pierwiastki wielomianu:"<<endl;
- pierwiastki_wielomianu(tab,head,n,ile);
- return 0;
- }
Add Comment
Please, Sign In to add comment