jezv vor 11 Monaten
Ursprung
Commit
34388b5dfb
3 geänderte Dateien mit 96 neuen und 14 gelöschten Zeilen
  1. 73 11
      Rational.cpp
  2. 18 0
      Rational.h
  3. 5 3
      main_rational.cpp

+ 73 - 11
Rational.cpp

@@ -40,6 +40,7 @@ Rational::Rational(float x)
 
     num = sign * (int64_t)(significand);
     denum = smth;
+    cout << "float constructor work" << endl;
     simple();
 }
 
@@ -66,21 +67,22 @@ Rational::Rational(double x)
 
     num = (long long int)(sign * significant);
     denum = (long long int)pow(2, -exp);
+    cout << "double constructor work" << endl;
     simple();
 }
 
 Rational& Rational::operator += (const Rational& r)
 {
     Rational newrr = r;
-    while (INT64_MAX - num < r.num or INT64_MAX - denum < r.denum) {
-        if(num > 10000 or denum > 10000) {
+    while (INT64_MAX - abs(num) < abs(r.num) or INT64_MAX - abs(denum) < abs(r.denum)) {
+        if(abs(num) > 10000 or abs(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) {
+        if(abs(r.num > 10000) or abs(r.denum > 10000)) {
             Fraction f(r);
             f.layers.pop();
             newrr = Rational(f);
@@ -114,16 +116,20 @@ Rational Rational :: operator - (const Rational& r) const
 
 Rational& Rational::operator *= (const Rational& r)
 {
+    cout << "suka *=" << endl;
     Rational newrr = r;
-    while (INT64_MAX / num < r.num or INT64_MAX / denum < r.denum) {
-        if(num > 10000 or denum > 10000) {
+    while (INT64_MAX / abs(num) < abs(r.num) or INT64_MAX / abs(denum) < abs(r.denum)) {
+        if(abs(num) > 10000 or abs(denum) > 10000) {
             Fraction f(*this);
+            cout << "pop ne ok" << endl;
+            f.show();
             f.layers.pop();
-            Rational newr(f);
+            cout << "pop ok" << endl;
+            Rational newr = Rational(f);
             num = newr.num;
             denum = newr.denum;
         }
-        if(r.num > 10000 or r.denum > 10000) {
+        if(abs(r.num) > 10000 or abs(r.denum) > 10000) {
             Fraction f(r);
             f.layers.pop();
             newrr = Rational(f);
@@ -137,6 +143,7 @@ Rational& Rational::operator *= (const Rational& r)
 
 Rational Rational :: operator * (const Rational& r) const
 {
+    cout << "suka *" << endl;
     Rational res(*this);
     return res *= r;
 }
@@ -160,6 +167,46 @@ Rational Rational :: operator -() const
     return Rational(-num, denum);
 }
 
+Rational& Rational :: operator += (const int r)
+{
+    return (*this)+=Rational(r);
+}
+
+Rational Rational :: operator + (const int r) const
+{
+    return (*this)+Rational(r);
+}
+
+Rational& Rational :: operator -= (const int r)
+{
+    return (*this)-=Rational(r);
+}
+
+Rational Rational :: operator - (const int r) const
+{
+    return (*this)-Rational(r);
+}
+
+Rational& Rational :: operator *= (const int r)
+{
+    return (*this)*=Rational(r);
+}
+
+Rational Rational :: operator * (const int r) const
+{
+    return (*this)*Rational(r);
+}
+
+Rational& Rational :: operator /= (const int r)
+{
+    return (*this)/=Rational(r);
+}
+
+Rational Rational :: operator / (const int r) const
+{
+    return (*this)/Rational(r);
+}
+
 ostream& operator <<(ostream& out, const Rational& r)
 {
     return out << '(' << r.num << ")/(" << r.denum << ')';
@@ -193,28 +240,43 @@ long long int Rational::lcm(long long int num1, long long int num2)
 
 long long int Rational::gcd(long long int num1, long long int num2)
 {
+    int s = 0;
     while (num1 && num2)
-        if (num1 >= num2)
+    {
+        cout << num1 << ' ' << num2 << endl;
+        if (abs(num1) >= abs(num2))
+        {
+           cout << "num1 >= num2" << endl;
            num1 %= num2;
+        }
         else
+        {
+           cout << "else" << endl;
            num2 %= num1;
-    return num1 | num2;
+        }
+        s++;
+    }
+    return abs(num1 | num2);
 }
 
 void Rational::simple()
 {
+    cout << "smth wrong in simple" << endl;
     long long int gct = gcd(num, denum);
+    cout << "gcd: " << gct << endl;
     num /= gct;
     denum /= gct; 
+    cout << "simple work norm" << endl;
 }
 
 Rational Rational::sqrt()
 {
-    cout << "Real sqrt" << endl;
+    cout << "sqrt work" << endl;
     Rational a = *this;
 	Rational x = *this;
 	for (long long int i = 0; i < 10000; i++) {
-		x = (x + (a / x)) / (Rational)2;
+		x = (x + (a / x)) / 2;
 	}
+    cout << "sqrt work normal" << endl;
 	return x;
 }

+ 18 - 0
Rational.h

@@ -26,6 +26,24 @@ public:
     Rational operator / (const Rational& r) const;
     Rational& operator /= (const Rational& r);
 
+    Rational& operator *=(const int r);
+    Rational operator *(const int r) const;
+    Rational& operator += (const int r);
+    Rational operator + (const int r) const;
+    Rational& operator -= (const int r);
+    Rational operator - (const int r) const;
+    Rational operator / (const int r) const;
+    Rational& operator /= (const int r);
+
+    Rational& operator *=(const double r);
+    Rational operator *(const double r) const;
+    Rational& operator += (const double r);
+    Rational operator + (const double r) const;
+    Rational& operator -= (const double r);
+    Rational operator - (const double r) const;
+    Rational operator / (const double r) const;
+    Rational& operator /= (const double r);
+
     Rational operator -() const;
 
     operator int () const;

+ 5 - 3
main_rational.cpp

@@ -6,15 +6,17 @@ using namespace std;
 
 int main() {
     Rational a, b, c, D, x1, x2;
+	cout << "smth" << endl;
 	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();
+	D = ((b*b) - a * 4 * c).sqrt();
 
-	x1 = ((-b - D) / ((Rational)2 * a));
-	x2 = ((-b + D) / ((Rational)2 * a));
+	cout << "there is problem?" << endl;
+	x1 = ((-b - D) / (a * 2));
+	x2 = ((-b + D) / (a * 2));
 
 	cout << (double)x1 << endl;
 	cout << (double)x2 << endl;