fork(3) download
  1. #include <iostream>
  2. #include <stdexcept>
  3. #include <sstream>
  4.  
  5. struct IComparable {
  6. virtual bool Equal(const IComparable&) const = 0;
  7. };
  8.  
  9. template <typename T>
  10. T gcd (const T& m, const T& n) {
  11. return n == 0 ? m : gcd (n, m % n);
  12. }
  13.  
  14. class Frac : public IComparable {
  15. int p, q;
  16.  
  17. void reduce () {
  18. int d = gcd (p,q);
  19. p /= d;
  20. q /= d;
  21. if (q < 0) {
  22. q = -q;
  23. p = -p;
  24. }
  25. }
  26.  
  27. public:
  28. Frac (int p=0,int q=1) : p(p), q(q) {
  29. if (q == 0) throw std::invalid_argument("Делитель равен 0");
  30. reduce ();
  31. }
  32.  
  33. std::string ToString() const {
  34. std::stringstream s;
  35. if (q!=1) s << '(';
  36. if (p>q && p%q)
  37. s << p/q << ' ' << p%q;
  38. else
  39. s << p;
  40. if (q!=1) s << '/' << q << ')';
  41. return s.str();
  42. }
  43.  
  44. bool operator==(const Frac& other) const {
  45. return p == other.p && q == other.q;
  46. }
  47.  
  48. bool operator!=(const Frac& other) const {
  49. return !(*this == other);
  50. }
  51.  
  52. bool Equal(const IComparable& other) const {
  53. const Frac* o = dynamic_cast<const Frac*>(&other);
  54. return o ? *o == *this : false;
  55. }
  56.  
  57. explicit operator double() const { return double(p)/q; }
  58.  
  59. Frac operator ~() const { return Frac(q,p); }
  60. Frac operator -() const { return Frac(-p,q); }
  61.  
  62. Frac& operator +=(const Frac& other) {
  63. p = p*other.q + other.p*q;
  64. q *= other.q;
  65. reduce();
  66. return *this;
  67. }
  68. Frac& operator -=(const Frac& other) {
  69. return *this += -other;
  70. }
  71. Frac& operator *=(const Frac& other) {
  72. p *= other.p;
  73. q *= other.q;
  74. reduce();
  75. return *this;
  76. }
  77. Frac& operator /=(const Frac& other) {
  78. return *this *= ~other;
  79. }
  80. };
  81.  
  82. Frac operator+(const Frac& lhs, const Frac& rhs) { return Frac (lhs) += rhs; }
  83. Frac operator-(const Frac& lhs, const Frac& rhs) { return Frac (lhs) -= rhs; }
  84. Frac operator*(const Frac& lhs, const Frac& rhs) { return Frac (lhs) *= rhs; }
  85. Frac operator/(const Frac& lhs, const Frac& rhs) { return Frac (lhs) /= rhs; }
  86.  
  87. Frac polynomial (Frac coef[], size_t n, const Frac& x) {
  88. if (n==0) return 0;
  89. Frac result=coef[n-1];
  90. for (size_t i=n-2; i < n; --i) {
  91. result *= x;
  92. result += coef[i];
  93. }
  94. return result;
  95. }
  96.  
  97. std::string PolynomialToString (Frac poly[], size_t n) {
  98. std::stringstream s;
  99. for (size_t i=n-1; i < n; --i) {
  100. if (poly[i] != 0) {
  101. if (poly[i] != 1 || i == 0) {
  102. s << poly[i].ToString();
  103. if (i > 0)
  104. s << '*';
  105. }
  106. if (i > 0) {
  107. s << 'x';
  108. if (i > 1)
  109. s << "^" << i;
  110. s << "+";
  111. }
  112. }
  113. }
  114. return s.str();
  115. }
  116.  
  117. int main () {
  118.  
  119. Frac poly[] = { Frac(3,2), Frac(1,-3), 0, -Frac (2,7) };
  120. const size_t n = sizeof poly / sizeof poly[0];
  121.  
  122. Frac x(2,8);
  123.  
  124. Frac result = polynomial (poly, n, x);
  125.  
  126. std::cout << PolynomialToString (poly, n) << '=' << result.ToString();
  127. std::cout << '=' << static_cast<double>(result);
  128. std::cout << " при x=" << x.ToString();
  129.  
  130. std::cout << "\n" << (Frac(2,3)/Frac(2,3)).ToString();
  131. }
Success #stdin #stdout 0s 3440KB
stdin
Standard input is empty
stdout
(-2/7)*x^3+(-1/3)*x+(1 1/2)=(1 277/672)=1.4122 при x=(1/4)
1