Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <iostream>
- #include <cstring>
- template <class type>
- class Rational;
- template <class type>
- std::ostream & operator << (std::ostream &os, const Rational<type> &a);
- template <class type>
- std::istream & operator >> (std::istream &is, Rational<type> &a);
- template <class type>
- Rational<type> operator* (type factor, const Rational<type> &a);
- template <class type>
- class Rational
- {
- private:
- type* top;
- type* bot;
- int ntop,nbot; // ilosc wspolczynnikow, a nie stopnien wielomianu
- public:
- Rational();
- Rational(int topdeg, int botdeg);
- Rational(int topdeg, const type* topvalues, int botdeg, const type* botvalues);
- Rational(const Rational &);
- ~Rational(void);
- Rational& operator=(const Rational &);
- Rational operator*(const Rational &) const;
- Rational operator+(const Rational &) const;
- Rational operator-(const Rational &) const;
- Rational operator*(type) const;
- type operator()(type) const;
- friend Rational (::operator * <>) (type factor, const Rational &a);
- friend std::ostream & operator << <> (std::ostream &os,const Rational &a);
- friend std::istream & operator >> <> (std::istream &is, Rational &a);
- };
- using namespace std;
- template <class type>
- Rational<type>::Rational()
- {
- //cout<<"KONSTRUKTOR NULL"<<endl;
- ntop=0;
- nbot=0;
- top = NULL;
- bot = NULL;
- }
- template <class type>
- Rational<type>::Rational(int topdeg, int botdeg)
- {
- //cout<<"KONSTRUKTOR int int"<<endl;
- int i;
- ntop=topdeg;
- nbot=botdeg;
- top = new type[ntop];
- for(i=0; i<ntop; i++) top[i]=type(0);
- bot = new type[nbot];
- for(i=0; i<nbot; i++) bot[i]=type(0);
- }
- template <class type>
- Rational<type>::Rational(int topdeg, const type* topvalues, int botdeg, const type* botvalues)
- {
- //cout<<"KONSTRUKTOR full opcja"<<endl;
- ntop=topdeg;
- nbot=botdeg;
- top = new type[ntop];
- bot = new type[nbot];
- memcpy(top, topvalues, ntop*sizeof(type));
- memcpy(bot, botvalues, nbot*sizeof(type));
- }
- template<class type>
- Rational<type>::Rational(const Rational<type> &a)
- {
- //cout<<"KONSTRUKTOR copypasta"<<endl;
- ntop=a.ntop;
- nbot=a.nbot;
- top = new type[ntop];
- bot = new type[nbot];
- memcpy(top, a.top, ntop*sizeof(type));
- memcpy(bot, a.bot, nbot*sizeof(type));
- }
- template<class type>
- Rational<type>::~Rational(void)
- {
- //cout<<"DESTRUKTOR"<<endl;
- ntop=0;
- nbot=0;
- delete[] top;
- delete[] bot;
- }
- template <class type>
- Rational<type> Rational<type>::operator*(type factor) const
- {
- int i;
- Rational<type> result(*this);
- for(i=0; i<ntop; ++i) result.top[i]*=factor;
- return result;
- }
- template <class type>
- Rational<type> operator*(type factor, const Rational<type>& a)
- {
- return a*factor;
- }
- template <class type>
- Rational<type>& Rational<type>::operator=(const Rational &a)
- {
- ntop=a.ntop;
- nbot=a.nbot;
- delete[] top;
- delete[] bot;
- top = new type[ntop];
- memcpy(top, a.top, ntop*sizeof(type));
- bot = new type[nbot];
- memcpy(bot, a.bot, nbot*sizeof(type));
- return *this;
- }
- template <class type>
- Rational<type> Rational<type>::operator*(const Rational &a) const
- {
- int i,j;
- Rational<type> temp((ntop+a.ntop-1),(nbot+a.nbot-1));
- for(i=0; i<ntop; ++i) for(j=0; j<a.ntop; ++j) temp.top[i+j]+=top[i]*a.top[j];
- for(i=0; i<nbot; ++i) for(j=0; j<a.nbot; ++j) temp.bot[i+j]+=bot[i]*a.bot[j];
- return temp;
- }
- template <class type>
- Rational<type> Rational<type>::operator+(const Rational &a) const
- {
- int i,j;
- Rational<type> result(2*ntop,2*nbot);
- Rational<type> temp(a.nbot, a.bot, a.nbot, a.bot);
- result = temp * (*this);
- for(i=0; i<a.ntop; ++i) for(j=0; j<nbot; ++j) result.top[i+j]+=a.top[i]*bot[j];
- return result;
- }
- template <class type>
- Rational<type> Rational<type>::operator-(const Rational &a) const
- {
- Rational<type> temp;
- temp = a;
- for(int i=0;i<temp.ntop;i++) temp.top[i]=-temp.top[i];
- return *this + temp;
- }
- template <class type>
- type Rational<type>::operator()(type value) const
- {
- type resulttop=top[ntop-1];
- for(int i=(ntop-2); i>=0; --i)
- resulttop=(resulttop*value + top[i]);
- type resultbot=bot[nbot-1];
- for(int i=(nbot-2); i>=0; --i)
- resultbot=(resultbot*value + bot[i]);
- resulttop/=resultbot;
- return resulttop;
- }
- template <class type>
- std::ostream & operator << (std::ostream &os, const Rational<type> &a)
- {
- for(int i=0; i<a.ntop; ++i)
- {
- if (a.ntop-i>1) os<<a.top[i]<<"x^"<<a.ntop-i-1<<" + ";
- else os<<a.top[i];
- }
- os<<" / ";
- for(int i=0; i<a.nbot; ++i)
- {
- if (a.nbot-i>1) os<<a.bot[i]<<"x^"<<a.nbot-i-1<<" + ";
- else os<<a.bot[i];
- }
- return os;
- }
- template <class type>
- std::istream & operator >> (std::istream &is, Rational<type> &a)
- {
- std::cout<<"licznik ile: ";
- is>>a.ntop;
- a.top = new type[a.ntop];
- for(int i=0; i<a.ntop; ++i)
- {std::cout<<i+1<<". wsp: ";
- is>>a.top[i];}
- std::cout<<"mianownik ile: ";
- is>>a.nbot;
- a.bot = new type[a.nbot];
- for(int i=0; i<a.nbot; ++i)
- {std::cout<<i+1<<". wsp: ";
- is>>a.bot[i];}
- return is;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement