fork(2) download
  1. #include<iostream>
  2. #include<climits>
  3. using namespace std;
  4.  
  5. class bad_rational{};
  6.  
  7. class rational
  8. {
  9. int num; //любое целое
  10. int denom;//должен быть >0
  11. void normalize();
  12. public:
  13. rational():num(0),denom(1){}
  14. rational(int n):num(n),denom(1){}
  15. rational(int n,int d);
  16.  
  17. int numerator() const {return num;}
  18. int denominator() const {return denom;}
  19.  
  20. rational& reduce();
  21. rational get_reduced()const;
  22.  
  23. // Операторы сравнения
  24. bool operator< (const rational& r) const;
  25. bool operator<= (const rational& r) const;
  26. bool operator> (const rational& r) const;
  27. bool operator>= (const rational& r) const;
  28. bool operator== (const rational& r) const;
  29. bool operator!= (const rational& r) const;
  30.  
  31. // Арифметические операторы
  32. rational& operator+= (const rational& r);
  33. rational& operator-= (const rational& r);
  34. rational& operator*= (const rational& r);
  35. rational& operator/= (const rational& r);
  36.  
  37. // Арифметические операторы с целыми параметрами
  38. /*rational& operator+= (int i);
  39.   rational& operator-= (int i);
  40.   rational& operator*= (int i);
  41.   rational& operator/= (int i);*/
  42.  
  43. //Присваивание целого числа
  44. rational& operator=(int n){num=n;denom=1;}
  45.  
  46. friend istream& operator>> (istream& in, rational& r);
  47. };
  48.  
  49. int gcd(int a, int b)
  50. {
  51. if (a<0) a=-a;
  52. if (b<0) b=-b;
  53. while (b!=0)
  54. {
  55. int p=a%b;
  56. a=b;
  57. b=p;
  58. }
  59. return a;
  60. }
  61.  
  62. long long gcd(long long a, long long b)
  63. {
  64. if (a<0) a=-a;
  65. if (b<0) b=-b;
  66. while (b!=0)
  67. {
  68. long long p=a%b;
  69. a=b;
  70. b=p;
  71. }
  72. return a;
  73. }
  74.  
  75. long long NOK(int a, int b)
  76. {
  77. return (long long)a/gcd(a,b)*b;
  78. }
  79.  
  80. void rational::normalize()
  81. {
  82. if (denom==0) throw bad_rational();
  83. if (denom<0){num=-num;denom=-denom;}
  84. }
  85.  
  86. rational::rational(int n,int d):num(n),denom(d)
  87. {
  88. normalize();
  89. }
  90.  
  91. rational& rational::reduce()
  92. {
  93. int d=gcd(num,denom);
  94. num/=d;
  95. denom/=d;
  96. return (*this);
  97. }
  98.  
  99. rational rational::get_reduced()const
  100. {
  101. int d=gcd(num,denom);
  102. return rational(num/d,denom/d);
  103. }
  104.  
  105. bool rational::operator< (const rational& r) const
  106. {
  107. return (long long)this->num*r.denom<(long long)r.num*this->denom;
  108. }
  109. bool rational::operator<= (const rational& r) const
  110. {
  111. return (long long)this->num*r.denom<=(long long)r.num*this->denom;
  112. }
  113. bool rational::operator> (const rational& r) const
  114. {
  115. return (long long)this->num*r.denom>(long long)r.num*this->denom;
  116. }
  117. bool rational::operator>= (const rational& r) const
  118. {
  119. return (long long)this->num*r.denom>=(long long)r.num*this->denom;
  120. }
  121. bool rational::operator== (const rational& r) const
  122. {
  123. return (long long)this->num*r.denom==(long long)r.num*this->denom;
  124. }
  125. bool rational::operator!= (const rational& r) const
  126. {
  127. return (long long)this->num*r.denom!=(long long)r.num*this->denom;
  128. }
  129.  
  130. rational& rational::operator+= (const rational& r)
  131. {
  132. long long new_denom=(long long)this->denom*r.denom;
  133. long long new_num=(long long)this->num*r.denom+(long long)r.num*this->denom;
  134. long long d=gcd(new_denom,new_num);
  135. new_denom/=d;
  136. new_num/=d;
  137. if (new_denom>INT_MAX || abs(new_num)>INT_MAX) throw bad_rational();
  138. denom=new_denom;
  139. num=new_num;
  140. return *this;
  141. }
  142.  
  143. rational& rational::operator-= (const rational& r)
  144. {
  145. long long new_denom=(long long)this->denom*r.denom;
  146. long long new_num=(long long)this->num*r.denom-(long long)r.num*this->denom;
  147. long long d=gcd(new_denom,new_num);
  148. new_denom/=d;
  149. new_num/=d;
  150. if (new_denom>INT_MAX || abs(new_num)>INT_MAX) throw bad_rational();
  151. denom=new_denom;
  152. num=new_num;
  153. return *this;
  154. }
  155.  
  156. rational& rational::operator*= (const rational& r)
  157. {
  158. return *this;
  159. }
  160.  
  161. rational& rational::operator/= (const rational& r)
  162. {
  163. return *this;
  164. }
  165.  
  166.  
  167. rational operator-(const rational& r)
  168. {
  169. return rational(-r.numerator(),r.denominator());
  170. }
  171.  
  172. rational operator+(const rational& r1,const rational& r2)
  173. {
  174. rational res(r1);
  175. res+=r2;
  176. return res;
  177. }
  178.  
  179. rational operator+(int i,const rational& r2)
  180. {
  181. rational res(i);
  182. res+=r2;
  183. return res;
  184. }
  185.  
  186. rational operator+(const rational& r1,int i)
  187. {
  188. rational res(r1);
  189. res+=i;
  190. return res;
  191. }
  192.  
  193. rational operator-(const rational& r1,const rational& r2)
  194. {
  195. rational res(r1);
  196. res-=r2;
  197. return res;
  198. }
  199.  
  200. rational operator-(int i,const rational& r2)
  201. {
  202. rational res(i);
  203. res-=r2;
  204. return res;
  205. }
  206.  
  207. rational operator-(const rational& r1,int i)
  208. {
  209. rational res(r1);
  210. res-=i;
  211. return res;
  212. }
  213.  
  214.  
  215. istream& operator>> (istream& in, rational& r)
  216. {
  217. in>>r.num; in.ignore(2,'/'); in>>r.denom;
  218. r.normalize();
  219. }
  220.  
  221. ostream& operator<< (ostream& out, const rational& r)
  222. {
  223. out <<r.numerator()<<"/"<<r.denominator();
  224. return out;
  225. }
  226.  
  227. int main()
  228. {
  229. rational r13(1,3), r24(2,4);
  230. cout <<r13 <<"+" <<r24 <<"=" <<r13+r24<<endl;
  231. cout <<r13 <<"-" <<r24 <<"=" <<r13-r24<<endl;
  232. try
  233. {
  234. rational qqq(1,2147483647), zzz(2,2147483645);
  235. cout <<qqq <<"+" <<zzz <<"=" <<qqq+zzz<<endl;
  236. }
  237. catch(bad_rational&)
  238. {
  239. cout <<"Bad rational obtained!"<<endl;
  240. }
  241.  
  242. return 0;
  243. }
  244.  
Success #stdin #stdout 0s 3232KB
stdin
Standard input is empty
stdout
1/3+2/4=5/6
1/3-2/4=-1/6
Bad rational obtained!