Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "List.h"
- #include <string.h>
- #include <iostream>
- #include <cmath>
- #include <string>
- using namespace std;
- enum{N=200};
- Cislo::Cislo(): size(0), cifry(new Data[N]), znamenko(0)
- {init();}
- Cislo::Cislo(const Cislo &other)
- :size(0), cifry(new Data[N]), znamenko(0)
- {
- init();
- copyFrom(other);
- }
- Cislo::Cislo(Cislo &&other)
- {
- this->cifry=other.cifry;
- this->size=other.size;
- this->znamenko=other.znamenko;
- other.znamenko=0;
- other.cifry=nullptr;
- other.size=0;
- }
- void Cislo::init()
- {
- for(int i =0; i<N; i++) cifry[i]=0;
- znamenko ='+';
- size = 1;
- }
- Cislo::~Cislo(){
- delete [] cifry;
- }
- void Cislo::freeMemory()
- {
- init();
- size=0;
- znamenko=0;
- delete [] cifry;
- cifry=0;
- }
- void Cislo::naplnit(const char *d)
- {
- if(d[0]=='-')
- {
- size=strlen(d)-1;
- znamenko=d[0];
- for(int i =0; i<size; i++)
- cifry[i]=(int)d[size-i]-48;
- }
- else
- { size=strlen(d);
- znamenko='+';
- for(int i =0; i<size; i++)
- cifry[i]=(int)d[size-i-1]-48;
- }
- }
- void Cislo::print()
- {
- cout << znamenko;
- for(int i =0; i <size; i++)
- {
- cout << cifry[size-1-i];
- }
- cout << endl;
- }
- void Cislo::copyFrom(const Cislo &source)
- {
- this->size = source.size;
- this->znamenko=source.znamenko;
- for(int i=0; i <source.size; i++)
- this->cifry[i]=source.cifry[i];
- }
- Cislo& Cislo::operator=(const Cislo &rhs){
- if(this == &rhs){
- return *this;
- }
- copyFrom(rhs);
- return *this;
- }
- Cislo& Cislo::operator=(Cislo &&rhs){
- if(this == &rhs){
- return *this;
- }
- this->freeMemory();
- this->cifry = rhs.cifry;
- this->size = rhs.size;
- rhs.cifry = nullptr;
- rhs.size = 0;
- return *this;
- }
- bool operator==(const Cislo &a,const Cislo &b)
- {
- if(a.size == b.size && a.znamenko==b.znamenko)
- for(int i=0;i<a.size; i++)
- {if(a.cifry[i]!=b.cifry[i])
- return false;}
- return true;
- }
- bool operator<(const Cislo &a,const Cislo &b)
- {
- if(a.znamenko!=b.znamenko)
- {if(a.znamenko=='-')
- return true;
- else
- return false;}
- if(a.size == b.size)
- {for(int i=0;i<a.size; i++)
- {if(a.cifry[a.size-1-i]>b.cifry[b.size-1-i])
- {if(a.znamenko == '+')
- return false;
- else
- return true;
- }
- else if (a.cifry[a.size-1-i]<b.cifry[b.size-1-i])
- {if(a.znamenko == '+')
- return true;
- else
- return false;
- }
- }
- return false;
- }
- else if(a.size > b. size)
- return false;
- else
- return true;
- }
- bool operator>(const Cislo &a, const Cislo &b)
- {
- return!(a<b || a==b);
- }
- void Cislo::spocitejVelikost()
- {
- int s=199;
- while(s>=0)
- { if(cifry[s]!=0){
- size=s+1;
- break;}
- if(s==0)
- {size=1;
- break;}
- s--;
- }
- }
- Cislo Cislo::absolut(const Cislo &a)
- {
- Cislo c= a;
- c.znamenko='+';
- return c;
- }
- Cislo operator-(Cislo &a)
- {
- if(a.znamenko=='+')
- a.znamenko='-';
- else
- a.znamenko='+';
- return a;
- }
- void Cislo::scitaci_pomocnik1(Cislo &a, Cislo &b)
- {
- int s= 0;
- for(int i = 0; i < max(a.size, b.size)+1; i++){
- s=a.cifry[i]+b.cifry[i];
- if(s+this->cifry[i]>9)
- {this->cifry[i]=this->cifry[i] + s-10;
- //if i+1=200; error
- this->cifry[i+1]++;
- cout << "bof";
- }
- else {
- this->cifry[i]=this->cifry[i] +s;
- }
- }
- this->znamenko=a.znamenko;
- }
- void Cislo::scitaci_pomocnik2(Cislo &a, Cislo &b)
- {
- int s= 0;
- Cislo c;
- for(int i = 0; i < max(a.size, b.size)+2; i++){
- s=a.cifry[i]-b.cifry[i]-c.cifry[i];
- if(s<0)
- {this->cifry[i]= 10 +s;
- //if i+1=200; error
- c.cifry[i+1]++;
- }
- else {
- this->cifry[i]=s;
- }
- }
- this->spocitejVelikost();
- }
- Cislo operator+(Cislo &a,Cislo &b)
- {
- Cislo e=b;
- Cislo c;
- if(a.znamenko==b.znamenko){
- c.scitaci_pomocnik1(a, b);
- c.znamenko=a.znamenko;
- }
- else if(a.znamenko=='-')
- {if(a<-e) {
- c.scitaci_pomocnik2(a,b);
- c.znamenko='-';
- }
- else
- { c.scitaci_pomocnik2(b,a);
- c.znamenko='+';
- }
- }
- else
- { if(a>-e)
- {c.scitaci_pomocnik2(a,b);
- c.znamenko='+';
- }
- else{c.scitaci_pomocnik2(b,a);
- c.znamenko='-';
- }
- }
- c.spocitejVelikost();
- return c;
- }
- Cislo operator-(Cislo &a,Cislo &b)
- {
- Cislo c=-b;
- return a+c;
- }
- Cislo operator*(Cislo &a, Cislo &b)
- {
- Cislo c;
- c.init();
- int s=0;
- for(int k =0; k< a.size+b.size;k++)
- {Cislo d;
- d.init();
- for(int i =0; i< a.size+b.size;i++)
- {
- /* s= a.cifry[k]*b.cifry[i];
- d.cifry[i+1+k]=s/10;
- d.cifry[i+k]=d.cifry[i+k] + s-d.cifry[i+1+k]*10;
- */ s= a.cifry[k]*b.cifry[i];
- d.cifry[i+1+k]=(d.cifry[i+k]+s)/10;
- d.cifry[i+k]=d.cifry[i+k]+s-d.cifry[i+k+1]*10;
- }
- d.spocitejVelikost();
- c.znamenko = '+';
- c = c+d;
- c.spocitejVelikost();
- }
- if(a.znamenko==b.znamenko)
- c.znamenko='+';
- else
- c.znamenko='-';
- return c;
- }
- Cislo operator*(Cislo &a, int b)
- {
- Cislo c;
- char buf[16];
- sprintf(buf,"%d",b);
- const char* p = buf;
- c.naplnit(p);
- return c*a;
- }
- Cislo operator+(Cislo &a, int b)
- {
- Cislo c;
- char buf[16];
- sprintf(buf,"%d",b);
- const char* p = buf;
- c.naplnit(p);
- c=c+a;
- return c;
- }
- Cislo operator/(Cislo &a, Cislo &b)
- {
- Cislo delenec=a;
- Cislo delitel=b;
- delenec.znamenko='+';
- delitel.znamenko='+';
- Cislo pomocnik;
- Cislo vysledek;
- int idx =0;
- pomocnik.cifry[0] = delenec.cifry[delenec.size -1 -(idx)];
- pomocnik.spocitejVelikost();
- // cout << endl;
- while (pomocnik < delitel)
- {
- int pomoc = delenec.cifry[delenec.size -1 -(++idx)];
- // pomocnik.print();
- pomocnik = pomocnik * 10;
- pomocnik.znamenko='+';
- pomocnik=pomocnik + pomoc;
- pomocnik.znamenko='+';
- // cout << pomoc << endl;
- }
- while (delenec.size >idx)
- {
- ++idx;
- cout<< idx;
- int pomoc=1;
- while(pomoc<11)
- {
- if((delitel*pomoc)>pomocnik)
- { vysledek = vysledek*10;
- vysledek.znamenko='+';
- vysledek = vysledek + (pomoc-1);
- vysledek.znamenko='+';
- //
- int pomoc2 = delenec.cifry[delenec.size -1 -(idx)];
- Cislo delitel_pomoc=delitel*(pomoc-1);
- delitel_pomoc.znamenko='+';
- pomocnik= pomocnik-delitel_pomoc;
- pomocnik.znamenko='+';
- pomocnik= pomocnik*10;
- pomocnik.znamenko='+';
- pomocnik= pomocnik + pomoc2;
- pomocnik.znamenko='+';
- }
- pomoc++;
- }
- }
- vysledek.spocitejVelikost();
- // když je delitel vetsi než delenec
- if (vysledek.size == 0)
- //CHYBA
- cout << "vysoký delitel";
- if(a.znamenko==b.znamenko)
- vysledek.znamenko='+';
- else
- vysledek.znamenko='-';
- return vysledek;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement