jezv 11 mēneši atpakaļ
vecāks
revīzija
66ecf5366b
4 mainītis faili ar 78 papildinājumiem un 18 dzēšanām
  1. 1 1
      Fraction
  2. 56 4
      Rational.cpp
  3. 8 2
      Rational.h
  4. 13 11
      main_rational.cpp

+ 1 - 1
Fraction

@@ -1 +1 @@
-Subproject commit 55f59015aa913505d168b191f32e1ee803c4d9bb
+Subproject commit 57c6780fdb2a8f9029413c6e1cc1d5db50159e78

+ 56 - 4
Rational.cpp

@@ -1,4 +1,5 @@
 #include "Rational.h"
+#include "Fraction/Fraction.h"
 #include <iostream>
 #include <iomanip>
 #include <bitset>
@@ -12,6 +13,11 @@ Rational::Rational(){
 
 }
 
+Rational::Rational(int num1){
+    num = num1;
+    denum = 1;
+}
+
 Rational::Rational(int num1, int num2)
 {
     num = num1;
@@ -65,8 +71,23 @@ Rational::Rational(double x)
 
 Rational& Rational::operator += (const Rational& r)
 {
-    int scm = lcm(denum, r.denum);
-    num = (num * (scm/denum) + r.num * (scm/r.denum));
+    Rational newrr = r;
+    while (INT64_MAX - num < r.num or INT64_MAX - denum < r.denum) {
+        if(num > 10000 or denum > 10000) {
+            Fraction f(*this);
+            f.layers.pop();
+            Rational newr(f);
+            num = newr.num;
+            denum = newr.denum;
+        }
+        if(r.num > 10000 or r.denum > 10000) {
+            Fraction f(r);
+            f.layers.pop();
+            newrr = Rational(f);
+        }
+    }
+    int scm = lcm(denum, newrr.denum);
+    num = (num * (scm/denum) + newrr.num * (scm/newrr.denum));
     denum = scm;
     return *this;
 }
@@ -93,8 +114,23 @@ Rational Rational :: operator - (const Rational& r) const
 
 Rational& Rational::operator *= (const Rational& r)
 {
-    num *= r.num;
-    denum *= r.denum;
+    Rational newrr = r;
+    while (INT64_MAX / num < r.num or INT64_MAX / denum < r.denum) {
+        if(num > 10000 or denum > 10000) {
+            Fraction f(*this);
+            f.layers.pop();
+            Rational newr(f);
+            num = newr.num;
+            denum = newr.denum;
+        }
+        if(r.num > 10000 or r.denum > 10000) {
+            Fraction f(r);
+            f.layers.pop();
+            newrr = Rational(f);
+        }
+    }
+    num *= newrr.num;
+    denum *= newrr.denum;
     simple();
     return *this;
 }
@@ -119,6 +155,11 @@ Rational Rational :: operator / (const Rational& r) const
     return res /= r;
 }
 
+Rational Rational :: operator -() const
+{
+    return Rational(-num, denum);
+}
+
 ostream& operator <<(ostream& out, const Rational& r)
 {
     return out << '(' << r.num << ")/(" << r.denum << ')';
@@ -165,4 +206,15 @@ void Rational::simple()
     long long int gct = gcd(num, denum);
     num /= gct;
     denum /= gct; 
+}
+
+Rational Rational::sqrt()
+{
+    cout << "Real sqrt" << endl;
+    Rational a = *this;
+	Rational x = *this;
+	for (long long int i = 0; i < 10000; i++) {
+		x = (x + (a / x)) / (Rational)2;
+	}
+	return x;
 }

+ 8 - 2
Rational.h

@@ -12,8 +12,9 @@ public:
     long long int denum;
 
     Rational();
-    Rational(float num);
-    Rational(double num);
+    Rational(int num1);
+    Rational(float x);
+    Rational(double x);
     Rational(int num1, int num2);
 
     Rational& operator *=(const Rational& r);
@@ -25,11 +26,15 @@ public:
     Rational operator / (const Rational& r) const;
     Rational& operator /= (const Rational& r);
 
+    Rational operator -() const;
+
     operator int () const;
     operator double() const;
 
     friend ostream& operator <<(ostream& out, const Rational& r);
 
+    Rational sqrt();
+
 private:
     // упрощение самого себя
     void simple();
@@ -37,6 +42,7 @@ private:
     long long int lcm(long long int num1, long long int num2);
     // НОД
     long long int gcd(long long int num1, long long int num2);
+
 };
 
 #endif

+ 13 - 11
main_rational.cpp

@@ -5,15 +5,17 @@
 using namespace std;
 
 int main() {
-    Rational r(19,20);
-    cout << r << endl;
-    Rational r2(3, 727);
-    r += r2;
-    cout << r << endl;
-    float x = 1.375;
-    cout << x << endl;
-    cout << Rational(x) << endl;
-    double x2 = 1.375;
-    cout << x2 << endl;
-    cout << Rational(x2);
+    Rational a, b, c, D, x1, x2;
+	a = Rational(1.0 / 1);
+	b = Rational(-62.0 / 133);
+	c = Rational(-51.0 / 133);
+
+    cout << "sqrt" << endl;
+	D = ((b*b) -(Rational)4 * a * c).sqrt();
+
+	x1 = ((-b - D) / ((Rational)2 * a));
+	x2 = ((-b + D) / ((Rational)2 * a));
+
+	cout << (double)x1 << endl;
+	cout << (double)x2 << endl;
 }