Advertisement
Guest User

Untitled

a guest
Aug 16th, 2017
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.10 KB | None | 0 0
  1. #include <iostream>
  2. #include <iostream>
  3. #include <cstring>
  4.  
  5. template <class type>
  6. class Rational;
  7.  
  8. template <class type>
  9. std::ostream & operator << (std::ostream &os, const Rational<type> &a);
  10.  
  11. template <class type>
  12. std::istream & operator >> (std::istream &is, Rational<type> &a);
  13.  
  14. template <class type>
  15. Rational<type> operator* (type factor, const Rational<type> &a);
  16.  
  17. template <class type>
  18. class Rational
  19. {
  20.   private:
  21.     type* top;
  22.     type* bot;
  23.     int ntop,nbot; // ilosc wspolczynnikow, a nie stopnien wielomianu
  24.  
  25.   public:
  26.     Rational();
  27.     Rational(int topdeg, int botdeg);
  28.     Rational(int topdeg, const type* topvalues, int botdeg, const type* botvalues);
  29.     Rational(const Rational &);
  30.     ~Rational(void);
  31.  
  32.     Rational& operator=(const Rational &);
  33.     Rational operator*(const Rational &) const;
  34.     Rational operator+(const Rational &) const;
  35.     Rational operator-(const Rational &) const;
  36.     Rational operator*(type) const;
  37.    
  38.     type operator()(type) const;
  39.    
  40.     friend Rational (::operator * <>) (type factor, const Rational &a);
  41.    
  42.     friend std::ostream & operator << <> (std::ostream &os,const Rational &a);
  43.     friend std::istream & operator >> <> (std::istream &is, Rational &a);
  44.  
  45. };
  46.  
  47. using namespace std;
  48.  
  49. template <class type>
  50. Rational<type>::Rational()
  51. {
  52.   //cout<<"KONSTRUKTOR NULL"<<endl;
  53.   ntop=0;
  54.   nbot=0;
  55.   top = NULL;
  56.   bot = NULL;
  57. }
  58.  
  59. template <class type>
  60. Rational<type>::Rational(int topdeg, int botdeg)
  61. {
  62.   //cout<<"KONSTRUKTOR int int"<<endl;
  63.   int i;
  64.   ntop=topdeg;
  65.   nbot=botdeg;
  66.   top = new type[ntop];
  67.   for(i=0; i<ntop; i++) top[i]=type(0);
  68.   bot = new type[nbot];
  69.   for(i=0; i<nbot; i++) bot[i]=type(0);
  70. }
  71.  
  72. template <class type>
  73. Rational<type>::Rational(int topdeg, const type* topvalues, int botdeg, const type* botvalues)
  74. {
  75.   //cout<<"KONSTRUKTOR full opcja"<<endl;
  76.   ntop=topdeg;
  77.   nbot=botdeg;
  78.   top = new type[ntop];
  79.   bot = new type[nbot];
  80.   memcpy(top, topvalues, ntop*sizeof(type));
  81.   memcpy(bot, botvalues, nbot*sizeof(type));
  82. }
  83.  
  84. template<class type>
  85. Rational<type>::Rational(const Rational<type> &a)
  86. {
  87.   //cout<<"KONSTRUKTOR copypasta"<<endl;
  88.   ntop=a.ntop;
  89.   nbot=a.nbot;
  90.   top = new type[ntop];
  91.   bot = new type[nbot];
  92.   memcpy(top, a.top, ntop*sizeof(type));
  93.   memcpy(bot, a.bot, nbot*sizeof(type));
  94. }
  95.  
  96. template<class type>
  97. Rational<type>::~Rational(void)
  98. {
  99.   //cout<<"DESTRUKTOR"<<endl;
  100.   ntop=0;
  101.   nbot=0;
  102.   delete[] top;
  103.   delete[] bot;
  104. }
  105.  
  106. template <class type>
  107. Rational<type> Rational<type>::operator*(type factor) const
  108. {
  109.   int i;
  110.   Rational<type> result(*this);
  111.   for(i=0; i<ntop; ++i) result.top[i]*=factor;
  112.   return result;
  113. }
  114.  
  115. template <class type>
  116. Rational<type> operator*(type factor, const Rational<type>& a)
  117. {
  118.   return a*factor;
  119. }
  120.  
  121. template <class type>
  122. Rational<type>& Rational<type>::operator=(const Rational &a)
  123. {
  124.   ntop=a.ntop;
  125.   nbot=a.nbot;
  126.   delete[] top;
  127.   delete[] bot;
  128.   top = new type[ntop];
  129.   memcpy(top, a.top, ntop*sizeof(type));
  130.   bot = new type[nbot];
  131.   memcpy(bot, a.bot, nbot*sizeof(type));
  132.   return *this;
  133. }
  134.  
  135. template <class type>
  136. Rational<type> Rational<type>::operator*(const Rational &a) const
  137. {
  138.   int i,j;
  139.   Rational<type> temp((ntop+a.ntop-1),(nbot+a.nbot-1));
  140.   for(i=0; i<ntop; ++i) for(j=0; j<a.ntop; ++j) temp.top[i+j]+=top[i]*a.top[j];
  141.   for(i=0; i<nbot; ++i) for(j=0; j<a.nbot; ++j) temp.bot[i+j]+=bot[i]*a.bot[j];
  142.   return temp;
  143. }
  144.  
  145. template <class type>
  146. Rational<type> Rational<type>::operator+(const Rational &a) const
  147. {
  148.   int i,j;              
  149.   Rational<type> result(2*ntop,2*nbot);
  150.   Rational<type> temp(a.nbot, a.bot, a.nbot, a.bot);
  151.   result = temp * (*this);
  152.   for(i=0; i<a.ntop; ++i) for(j=0; j<nbot; ++j) result.top[i+j]+=a.top[i]*bot[j];
  153.    
  154.   return result;
  155. }
  156.  
  157. template <class type>
  158. Rational<type> Rational<type>::operator-(const Rational &a) const
  159. {
  160.   Rational<type> temp;
  161.   temp = a;
  162.   for(int i=0;i<temp.ntop;i++) temp.top[i]=-temp.top[i];
  163.   return *this + temp;
  164. }
  165.  
  166. template <class type>
  167. type Rational<type>::operator()(type value) const
  168. {
  169.   type resulttop=top[ntop-1];
  170.   for(int i=(ntop-2); i>=0; --i)
  171.   resulttop=(resulttop*value + top[i]);    
  172.   type resultbot=bot[nbot-1];
  173.   for(int i=(nbot-2); i>=0; --i)
  174.   resultbot=(resultbot*value + bot[i]);
  175.   resulttop/=resultbot;
  176.   return resulttop;
  177. }
  178.  
  179. template <class type>
  180. std::ostream & operator << (std::ostream &os, const Rational<type> &a)
  181. {
  182.   for(int i=0; i<a.ntop; ++i)
  183.   {
  184.   if (a.ntop-i>1) os<<a.top[i]<<"x^"<<a.ntop-i-1<<" + ";
  185.   else os<<a.top[i];
  186.   }
  187.  
  188.   os<<" / ";
  189.   for(int i=0; i<a.nbot; ++i)
  190.   {
  191.   if (a.nbot-i>1) os<<a.bot[i]<<"x^"<<a.nbot-i-1<<" + ";
  192.   else os<<a.bot[i];
  193.   }
  194.  
  195.   return os;
  196. }
  197.  
  198. template <class type>
  199. std::istream & operator >> (std::istream &is, Rational<type> &a)
  200. {
  201.   std::cout<<"licznik ile: ";
  202.   is>>a.ntop;
  203.   a.top = new type[a.ntop];
  204.   for(int i=0; i<a.ntop; ++i)
  205.   {std::cout<<i+1<<". wsp: ";
  206.   is>>a.top[i];}
  207.  
  208.   std::cout<<"mianownik ile: ";  
  209.   is>>a.nbot;
  210.   a.bot = new type[a.nbot];
  211.   for(int i=0; i<a.nbot; ++i)
  212.   {std::cout<<i+1<<". wsp: ";
  213.   is>>a.bot[i];}
  214.  
  215.   return is;
  216. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement