-4

添加分数减少功能 gcd 后,我不断收到核心转储。它会正常工作,直到它在我的主文件中输出减法部分,然后它只给我一个核心转储。对于像 2000/3000 这样的数字,它会工作得非常好,它会像它应该的那样减少到 2/3,但是当我输入 10/20 的一小部分时,它会像它应该的那样减少到 1/2,直到它开始输出正如我之前所说的,减法部分只会给我一个核心转储。怎么了??

//Rational.h file
   #include <iostream>
   using namespace std;

   class Rational
{
  public:
    // constructors

    Rational();

    Rational(int num, int den);

    // accessors

    int get_numer()const;

    int get_denom()const;

    // mutators

    void set_numer(int n);

    void set_denom(int d);

    // friend functions

    friend Rational operator +(const Rational& r1, const Rational& r2);

    friend Rational operator -(const Rational& r1, const Rational& r2);

    friend Rational operator *(const Rational& r1, const Rational& r2);

    friend Rational operator /(const Rational& r1, const Rational& r2);

    friend bool operator == (const Rational& r1, const Rational& r2);

    friend ostream& operator <<(ostream& file_out, const  Rational& r);

    friend istream& operator >>(istream& file_in, Rational& r);

private:
   int numerator;
   int denominator;
   void normalize();
   void gcd();
};

// nonmember functions

bool operator <(const Rational& r1, const Rational& r2);

bool operator !=(const Rational& r1, const Rational& r2);

//Rational.cc file
#include <iostream>
#include <cstdlib>
#include "Rational.h"
using namespace std;

Rational::Rational()
{
  numerator = 0;
  denominator = 1;
}

Rational::Rational(int num, int den)
{
  numerator = num;
  denominator = den;

if(den == 0)
{
  cout << "Error denominator is 0\n";
  exit(1);
}

  gcd();
  normalize();
}

void Rational::set_numer(int n)
{
  numerator = n;
  gcd();
  normalize();
}

void Rational::set_denom(int d)
{
  denominator = d;

  if(denominator == 0)
  {
    cout << "Error denominator is 0\n";
    exit(1);
  }

  gcd();
  normalize();
  }

int Rational::get_numer() const
{
  return(numerator);
}

int Rational::get_denom() const
{
  return(denominator);
}

void Rational::normalize()
{
   if(denominator < 0)
   {
     numerator *= -1;
     denominator = abs(denominator);
   }
   else if(numerator < 0 && denominator < 0)
   {
     numerator = abs(numerator);
     denominator = abs(denominator);
   }
 }

void Rational::gcd()
{
  int big;
  int small;
  int remainder;
  int great;

  if(numerator > denominator)
  {
    big = numerator;
    small = denominator;
  }else{
    big = denominator;
    small = numerator;
  }

  do{
    remainder = big % small;

  if(remainder == 0)
    great = small;
  else
  {
    big = small;
    small = remainder;
  }
}while(remainder != 0);

  numerator /= great;
  denominator /= great;
}

Rational operator +(const Rational& r1, const Rational& r2)
{
  Rational temp;

  temp.numerator = r1.numerator * r2.denominator + r1.denominator * r2.numerator;
  temp.denominator = r1.denominator * r2.denominator;

  temp.gcd();
  temp.normalize();

  return(temp);
}

Rational operator -(const Rational& r1, const Rational& r2)
{
  Rational temp;

  temp.numerator = r1.numerator * r2.denominator - r1.denominator * r2.numerator;
  temp.denominator =  r1.denominator * r2.denominator;

  temp.gcd();
  temp.normalize();

  return(temp);
}

Rational operator *(const Rational& r1, const Rational& r2)
{
  Rational temp;

  temp.numerator = r1.numerator * r2.numerator;
  temp.denominator =  r1.denominator * r2.denominator;

  temp.gcd();
  temp.normalize();

  return(temp);
}

Rational operator /(const Rational& r1, const Rational& r2)
{
  Rational temp;

  temp.numerator = r1.numerator * r2.denominator;
  temp.denominator = r1.denominator * r2.numerator;

  temp.gcd();
  temp.normalize();

  return(temp);
}

bool operator == (const Rational& r1, const Rational& r2)
{
  return((r1.numerator * r2.denominator) == (r1.denominator * r2.numerator));
}

ostream& operator <<(ostream& file_out, const  Rational& r)
{
  file_out << r.numerator << '/' << r.denominator;

  return(file_out);
}

istream& operator >>(istream& file_in, Rational& r)
{
  char ch;

  file_in >> r.numerator >> ch >> r.denominator;

  if(r.denominator == 0)
  {
    cout << "Error denominator is 0\n";
    exit(1);
  }

  r.gcd();
  r.normalize();

  return(file_in);
}

bool operator <(const Rational& r1, const Rational& r2)
{
  return((r1.get_numer() * r2.get_denom()) < (r1.get_denom() * r2.get_numer()));
}

bool operator !=(const Rational& r1, const Rational& r2)
{
  return((r1.get_numer() * r2.get_denom()) != (r1.get_denom() * r2.get_numer()));
}

//main.cc file
#include <iostream>
#include "Rational.h"
using namespace std;

int main()
{
  Rational r1(1,2), r2, r3;

// test the constructors
  cout << "Testing the constructors  \n\n";
  cout << "Testing the constructor with 2 arguments " << r1 << endl << endl;

// test the overloaded >> operator
  cout << "Testing the overloaded >> operator \n";
  cout << "Enter a rational number in the form 3/4  ";
  cin >> r2;

  cout << r1 << " + " << r2 << " = " << (r1 + r2) << endl;

  // test the set functions
  r3.set_numer(23);
  r3.set_denom(-13);

  cout <<"\nTesting the set functions "<< r3 << endl << endl;

// Testing the overloaded << operator and the +
  cout << r1 << " + " << r2 << " = " << r1 + r2;
  cout << endl << endl;

//testing the overloaded - operator

  r3 = r1 - r2;

  cout << r1 << " - " << r2 << " = " << r3;
  cout << endl << endl;

//testing the * and / function

  r3 = r1 * r2;

  cout << r1 << " * " << r2 << " = " << r3;
  cout << endl << endl;

  r3 = r1 / r2;

  cout << r1 << " / " << r2 << " = " << r3;
  cout << endl << endl;

//testing the < and == function

  if(r1 < r2)
    cout << r2 << " is bigger than " << r1 << endl;
  else if(r2 < r1)
    cout << r1 << " is bigger than " << r2 << endl;
  else if(r1 == r2)
    cout << r1 << " and " << r2 << " are equal pairs\n";

//testing the != function

  if(r1 != r2)
    cout << r1 << " and " << r2 << " are not equal pairs\n";

  return 0;
}
4

1 回答 1

3

gcd()在我看来,就像在at中除以零风险remainder = big % small;

您肯定会知道是否使用调试器从核心转储中获取回溯。

于 2013-06-23T09:54:08.233 回答