C++带有自行设计的有理数对类的程序未按预期工作

发布于 2024-10-04 20:18:14 字数 4484 浏览 5 评论 0原文

我对下面的代码有问题。它现在不执行任何功能...既不加、也不减、也不除、也不乘。

任何帮助将不胜感激...我需要解释...以便我能够理解它并克服它:D

#include <iostream>
using namespace std;

// Class Definitions

class RationalNumber
{
public:
     RationalNumber(int, int, int, int);                    
     RationalNumber operator+(RationalNumber);
     RationalNumber operator-(RationalNumber);
     RationalNumber operator*(RationalNumber);
     RationalNumber operator/(RationalNumber);
     RationalNumber operator<(RationalNumber);
     RationalNumber operator>(RationalNumber);
     RationalNumber operator<=(RationalNumber);
     RationalNumber operator>=(RationalNumber);
     RationalNumber operator==(RationalNumber);
     RationalNumber operator!=(RationalNumber);
private:
     int numerator;
     int denominator;
     int numerator2;
     int denominator2;

};  // end RationalNumber class


// RationalNumber class member-function definitions
RationalNumber::RationalNumber(int num, int denom, int num2, int denom2)
{
     numerator = num;
     denominator = denom;
     numerator2 = num2;
     denominator2 = denom2;

//for first fraction
     if (denominator == 0 || denominator < 0)
          cout << "ERROR:Denominator can not be zero or less than zero." << "\n";

     else
          //Reduces the fraction to lowest terms.
     {
          int i = numerator > denominator ? numerator : denominator;

          while(i > 1)
          {
               if(numerator % i == 0 && denominator % i == 0)  
               {
               numerator /= i;
               denominator /= i;
               }
               --i;
          }

     }

     cout << "Simplified fraction one is: " << numerator << " / "
        << denominator << "\n";

//For second fraction
     if (denominator2 == 0 || denominator2 < 0)
          cout << "ERROR:Denominator can not be zero or less than zero" << "\n";
     else
          //Reduces the fraction to lowest terms.
     {
          int j = numerator2 > denominator2 ? numerator2 : denominator2;

          while(j > 1)
          {
               if(numerator2 % j == 0 && denominator2 % j == 0)  
               {
               numerator2 /= j;
               denominator2 /= j;
               }
               --j;
          }

     }
     cout << "Simplified fraction two is: " << numerator2 << " / "
        << denominator2 << "\n";

}

// addition operator
RationalNumber RationalNumber::operator+(RationalNumber a)
{
      RationalNumber temp=RationalNumber(1,2,3,4);
      temp.numerator = numerator + a.numerator;
      temp.denominator = denominator + a.denominator;
      temp.numerator2 = numerator2 + a.numerator2;
      temp.denominator2 = denominator2 + a.denominator2;
      return temp;
}

// subtraction operator
RationalNumber RationalNumber::operator-(RationalNumber a)
{
     RationalNumber temp=RationalNumber(1,2,3,4);
      temp.numerator = numerator - a.numerator;
      temp.denominator = denominator - a.denominator;
      temp.numerator2 = numerator2 - a.numerator2;
      temp.denominator2 = denominator2 - a.denominator2;
      return temp;
}

// multiplication operator
RationalNumber RationalNumber::operator*(RationalNumber a)
{
      RationalNumber temp=RationalNumber(1,2,3,4);
      temp.numerator = numerator * a.numerator;
      temp.denominator = denominator * a.denominator;
      temp.numerator2 = numerator2 * a.numerator2;
      temp.denominator2 = denominator2 * a.denominator2;
      return temp;
}

// division operator
RationalNumber RationalNumber::operator/(RationalNumber a)
{
      RationalNumber temp=RationalNumber(1,2,3,4);
      temp.numerator = numerator / a.numerator;
      temp.denominator = denominator / a.denominator;
      temp.numerator2 = numerator2 / a.numerator2;
      temp.denominator2 = denominator2 / a.denominator2;
      return temp;
}

int main()
{

     int top;
     int bot;
     int top2;
     int bot2;

     cout << "Please enter the Numerator for fraction one: \n";
     cin >> top;
     cout << "Please enter the Denominator for fraction one: \n";
     cin >> bot;
     cout << "Please enter the Numerator for fraction two: \n";
     cin >> top2;
     cout << "Please enter the Denominator for fraction two: \n";
     cin >> bot2;

     RationalNumber A(top, bot, top2, bot2);
     return 0;

}

I have a problem with the code below. It is not performing any function now... neither add nor subtract nor divison nor multiplication.

Any help will be appreciated... I need explanation... so that I can understand it and get over with it :D

#include <iostream>
using namespace std;

// Class Definitions

class RationalNumber
{
public:
     RationalNumber(int, int, int, int);                    
     RationalNumber operator+(RationalNumber);
     RationalNumber operator-(RationalNumber);
     RationalNumber operator*(RationalNumber);
     RationalNumber operator/(RationalNumber);
     RationalNumber operator<(RationalNumber);
     RationalNumber operator>(RationalNumber);
     RationalNumber operator<=(RationalNumber);
     RationalNumber operator>=(RationalNumber);
     RationalNumber operator==(RationalNumber);
     RationalNumber operator!=(RationalNumber);
private:
     int numerator;
     int denominator;
     int numerator2;
     int denominator2;

};  // end RationalNumber class


// RationalNumber class member-function definitions
RationalNumber::RationalNumber(int num, int denom, int num2, int denom2)
{
     numerator = num;
     denominator = denom;
     numerator2 = num2;
     denominator2 = denom2;

//for first fraction
     if (denominator == 0 || denominator < 0)
          cout << "ERROR:Denominator can not be zero or less than zero." << "\n";

     else
          //Reduces the fraction to lowest terms.
     {
          int i = numerator > denominator ? numerator : denominator;

          while(i > 1)
          {
               if(numerator % i == 0 && denominator % i == 0)  
               {
               numerator /= i;
               denominator /= i;
               }
               --i;
          }

     }

     cout << "Simplified fraction one is: " << numerator << " / "
        << denominator << "\n";

//For second fraction
     if (denominator2 == 0 || denominator2 < 0)
          cout << "ERROR:Denominator can not be zero or less than zero" << "\n";
     else
          //Reduces the fraction to lowest terms.
     {
          int j = numerator2 > denominator2 ? numerator2 : denominator2;

          while(j > 1)
          {
               if(numerator2 % j == 0 && denominator2 % j == 0)  
               {
               numerator2 /= j;
               denominator2 /= j;
               }
               --j;
          }

     }
     cout << "Simplified fraction two is: " << numerator2 << " / "
        << denominator2 << "\n";

}

// addition operator
RationalNumber RationalNumber::operator+(RationalNumber a)
{
      RationalNumber temp=RationalNumber(1,2,3,4);
      temp.numerator = numerator + a.numerator;
      temp.denominator = denominator + a.denominator;
      temp.numerator2 = numerator2 + a.numerator2;
      temp.denominator2 = denominator2 + a.denominator2;
      return temp;
}

// subtraction operator
RationalNumber RationalNumber::operator-(RationalNumber a)
{
     RationalNumber temp=RationalNumber(1,2,3,4);
      temp.numerator = numerator - a.numerator;
      temp.denominator = denominator - a.denominator;
      temp.numerator2 = numerator2 - a.numerator2;
      temp.denominator2 = denominator2 - a.denominator2;
      return temp;
}

// multiplication operator
RationalNumber RationalNumber::operator*(RationalNumber a)
{
      RationalNumber temp=RationalNumber(1,2,3,4);
      temp.numerator = numerator * a.numerator;
      temp.denominator = denominator * a.denominator;
      temp.numerator2 = numerator2 * a.numerator2;
      temp.denominator2 = denominator2 * a.denominator2;
      return temp;
}

// division operator
RationalNumber RationalNumber::operator/(RationalNumber a)
{
      RationalNumber temp=RationalNumber(1,2,3,4);
      temp.numerator = numerator / a.numerator;
      temp.denominator = denominator / a.denominator;
      temp.numerator2 = numerator2 / a.numerator2;
      temp.denominator2 = denominator2 / a.denominator2;
      return temp;
}

int main()
{

     int top;
     int bot;
     int top2;
     int bot2;

     cout << "Please enter the Numerator for fraction one: \n";
     cin >> top;
     cout << "Please enter the Denominator for fraction one: \n";
     cin >> bot;
     cout << "Please enter the Numerator for fraction two: \n";
     cin >> top2;
     cout << "Please enter the Denominator for fraction two: \n";
     cin >> bot2;

     RationalNumber A(top, bot, top2, bot2);
     return 0;

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

娇纵 2024-10-11 20:18:14

通过声明RationalNumber(int, int, int, int),您将自动删除默认构造函数,即不再可能通过调用不带参数的构造函数来创建类的实例。

换句话说,

RationalNumber number;

不再可能。您需要说

RationalNumber number(1,2,3,4);

如果您希望能够使用默认构造函数创建实例,那么除了另一个构造函数之外,您还需要定义一个构造函数:

RationalNumber()
{
    // Initialization
}

By declaring RationalNumber(int, int, int, int), you automatically remove the default constructor, i.e. it is no longer possible to create an instance of your class by invoking a constructor without parameters.

In other words,

RationalNumber number;

is no longer possible. You'd need to say

RationalNumber number(1,2,3,4);.

If you want to be able to create an instance using a default constructor, then you need to define one in addition to the other constructor:

RationalNumber()
{
    // Initialization
}
旧梦荧光笔 2024-10-11 20:18:14

因此重新定义您的运算符:

RationalNumber RationalNumber::operator+(RationalNumber a)
{
      return RationalNumber(
          numerator + a.numerator,
          denominator + a.denominator,
          numerator2 + a.numerator2,
          denominator2 + a.denominator2);
}

这可以避免创建导致您悲伤的无参数临时值。它还帮助编译器利用返回值优化(RVO)。

Redefine your operators thus:

RationalNumber RationalNumber::operator+(RationalNumber a)
{
      return RationalNumber(
          numerator + a.numerator,
          denominator + a.denominator,
          numerator2 + a.numerator2,
          denominator2 + a.denominator2);
}

This avoids creating the parameterless temporary that is causing you grief. It also helps the compiler make use of the return-value optimisation (RVO).

千鲤 2024-10-11 20:18:14

我将快速介绍一下...

#include <utility>

struct ratio {
    int numerator;
    int denominator;

    explicit ratio( int n = 0, int d = 1 )
        : numerator( n ), denominator( d ) { }

    ratio operator - () const {
        ratio x( * this );
        x.numerator = - x.numerator;
        return x;
    }

    ratio operator + () const
        { return *this; }
};

void reduce( ratio &x ) {
    int larger = x.numerator, smaller = x.denominator;
    if ( larger < smaller ) std::swap( larger, smaller );
    while ( int rem = larger % smaller ) {
        larger = smaller;
        smaller = rem;
    }
    x.numerator /= smaller;
    x.denominator /= smaller;
}


ratio &operator += ( ratio &l, ratio const &r ) {
    l.numerator = l.numerator * r.denominator
                + r.numerator * l.denominator;
    l.denominator *= r.denominator;
    reduce( l );
    return l;
}

ratio &operator -= ( ratio &l, ratio const &r )
    { return l += - r; }

ratio &operator *= ( ratio &l, ratio const &r ) {
    l.numerator *= r.numerator;
    l.denominator *= r.denominator;
    reduce( l );
    return l;
}

ratio &operator /= ( ratio &l, ratio const &r ) {
    l.numerator *= r.denominator;
    l.denominator *= r.numerator;
    reduce( l );
    return l;
}

运算符 +-*/ 的规范定义。

I'll take a quick shot at it…

#include <utility>

struct ratio {
    int numerator;
    int denominator;

    explicit ratio( int n = 0, int d = 1 )
        : numerator( n ), denominator( d ) { }

    ratio operator - () const {
        ratio x( * this );
        x.numerator = - x.numerator;
        return x;
    }

    ratio operator + () const
        { return *this; }
};

void reduce( ratio &x ) {
    int larger = x.numerator, smaller = x.denominator;
    if ( larger < smaller ) std::swap( larger, smaller );
    while ( int rem = larger % smaller ) {
        larger = smaller;
        smaller = rem;
    }
    x.numerator /= smaller;
    x.denominator /= smaller;
}


ratio &operator += ( ratio &l, ratio const &r ) {
    l.numerator = l.numerator * r.denominator
                + r.numerator * l.denominator;
    l.denominator *= r.denominator;
    reduce( l );
    return l;
}

ratio &operator -= ( ratio &l, ratio const &r )
    { return l += - r; }

ratio &operator *= ( ratio &l, ratio const &r ) {
    l.numerator *= r.numerator;
    l.denominator *= r.denominator;
    reduce( l );
    return l;
}

ratio &operator /= ( ratio &l, ratio const &r ) {
    l.numerator *= r.denominator;
    l.denominator *= r.numerator;
    reduce( l );
    return l;
}

Canonical definitions for operators +, -, *, /.

孤独难免 2024-10-11 20:18:14

您的类没有采用零参数的构造函数。因此,以下代码:

class object=class();

尝试使用不存在的构造函数。您必须编写一个采用零参数的构造函数(显式地或通过为另一个构造函数的参数提供所有默认值),或者必须在构造时提供值:

class object(1, 2, 3, 4);
class object2 = class(1, 2, 3, 4);

Your class does not have a constructor that takes zero arguments. Therefore, the following code:

class object=class();

Is attempting to use a constructor that doesn't exist. You must either write a constructor that takes zero arguments (explicitly or by providing all the default values for another constructor's arguments) or you must provide values upon construction:

class object(1, 2, 3, 4);
class object2 = class(1, 2, 3, 4);
看春风乍起 2024-10-11 20:18:14

现在的问题是...它不执行任何添加或任何其他功能...程序运行完美,但不知何故我让additon和其他功能正常工作,

谢谢

#include <iostream>
using namespace std;

// Class Definitions

class RationalNumber
{
public:
     RationalNumber(int, int, int, int);                    
     RationalNumber operator+(RationalNumber);
     RationalNumber operator-(RationalNumber);
     RationalNumber operator*(RationalNumber);
     RationalNumber operator/(RationalNumber);
     RationalNumber operator<(RationalNumber);
     RationalNumber operator>(RationalNumber);
     RationalNumber operator<=(RationalNumber);
     RationalNumber operator>=(RationalNumber);
     RationalNumber operator==(RationalNumber);
     RationalNumber operator!=(RationalNumber);
private:
     int numerator;
     int denominator;
     int numerator2;
     int denominator2;

};  // end RationalNumber class


// RationalNumber class member-function definitions
RationalNumber::RationalNumber(int num, int denom, int num2, int denom2)
{
     numerator = num;
     denominator = denom;
     numerator2 = num2;
     denominator2 = denom2;

//for first fraction
     if (denominator == 0 || denominator < 0)
          cout << "ERROR:Denominator can not be zero or less than zero." << "\n";

     else
          //Reduces the fraction to lowest terms.
     {
          int i = numerator > denominator ? numerator : denominator;

          while(i > 1)
          {
               if(numerator % i == 0 && denominator % i == 0)  
               {
               numerator /= i;
               denominator /= i;
               }
               --i;
          }

     }

     cout << "Simplified fraction one is: " << numerator << " / "
        << denominator << "\n";

//For second fraction
     if (denominator2 == 0 || denominator2 < 0)
          cout << "ERROR:Denominator can not be zero or less than zero" << "\n";
     else
          //Reduces the fraction to lowest terms.
     {
          int j = numerator2 > denominator2 ? numerator2 : denominator2;

          while(j > 1)
          {
               if(numerator2 % j == 0 && denominator2 % j == 0)  
               {
               numerator2 /= j;
               denominator2 /= j;
               }
               --j;
          }

     }
     cout << "Simplified fraction two is: " << numerator2 << " / "
        << denominator2 << "\n";

}

// addition operator
RationalNumber RationalNumber::operator+(RationalNumber a)
{
      RationalNumber temp=RationalNumber(1,2,3,4);
      temp.numerator = numerator + a.numerator;
      temp.denominator = denominator + a.denominator;
      temp.numerator2 = numerator2 + a.numerator2;
      temp.denominator2 = denominator2 + a.denominator2;
      return temp;
}

// subtraction operator
RationalNumber RationalNumber::operator-(RationalNumber a)
{
      RationalNumber temp=RationalNumber(1,2,3,4);
      temp.numerator = numerator - a.numerator;
      temp.denominator = denominator - a.denominator;
      temp.numerator2 = numerator2 - a.numerator2;
      temp.denominator2 = denominator2 - a.denominator2;
      return temp;
}

// multiplication operator
RationalNumber RationalNumber::operator*(RationalNumber a)
{
      RationalNumber temp=RationalNumber(1,2,3,4);
      temp.numerator = numerator * a.numerator;
      temp.denominator = denominator * a.denominator;
      temp.numerator2 = numerator2 * a.numerator2;
      temp.denominator2 = denominator2 * a.denominator2;
      return temp;
}

// division operator
RationalNumber RationalNumber::operator/(RationalNumber a)
{
      RationalNumber temp=RationalNumber(1,2,3,4);
      temp.numerator = numerator / a.numerator;
      temp.denominator = denominator / a.denominator;
      temp.numerator2 = numerator2 / a.numerator2;
      temp.denominator2 = denominator2 / a.denominator2;
      return temp;
}

int main()
{

     int top;
     int bot;
     int top2;
     int bot2;

     cout << "Please enter the Numerator for fraction one: \n";
     cin >> top;
     cout << "Please enter the Denominator for fraction one: \n";
     cin >> bot;
     cout << "Please enter the Numerator for fraction two: \n";
     cin >> top2;
     cout << "Please enter the Denominator for fraction two: \n";
     cin >> bot2;

     RationalNumber A(top, bot, top2, bot2);
     return 0;

}

now the problem is ... its not performing any addition nor any other function... the program is running perfect but somehow i get the additon and other functions to work

thanks

#include <iostream>
using namespace std;

// Class Definitions

class RationalNumber
{
public:
     RationalNumber(int, int, int, int);                    
     RationalNumber operator+(RationalNumber);
     RationalNumber operator-(RationalNumber);
     RationalNumber operator*(RationalNumber);
     RationalNumber operator/(RationalNumber);
     RationalNumber operator<(RationalNumber);
     RationalNumber operator>(RationalNumber);
     RationalNumber operator<=(RationalNumber);
     RationalNumber operator>=(RationalNumber);
     RationalNumber operator==(RationalNumber);
     RationalNumber operator!=(RationalNumber);
private:
     int numerator;
     int denominator;
     int numerator2;
     int denominator2;

};  // end RationalNumber class


// RationalNumber class member-function definitions
RationalNumber::RationalNumber(int num, int denom, int num2, int denom2)
{
     numerator = num;
     denominator = denom;
     numerator2 = num2;
     denominator2 = denom2;

//for first fraction
     if (denominator == 0 || denominator < 0)
          cout << "ERROR:Denominator can not be zero or less than zero." << "\n";

     else
          //Reduces the fraction to lowest terms.
     {
          int i = numerator > denominator ? numerator : denominator;

          while(i > 1)
          {
               if(numerator % i == 0 && denominator % i == 0)  
               {
               numerator /= i;
               denominator /= i;
               }
               --i;
          }

     }

     cout << "Simplified fraction one is: " << numerator << " / "
        << denominator << "\n";

//For second fraction
     if (denominator2 == 0 || denominator2 < 0)
          cout << "ERROR:Denominator can not be zero or less than zero" << "\n";
     else
          //Reduces the fraction to lowest terms.
     {
          int j = numerator2 > denominator2 ? numerator2 : denominator2;

          while(j > 1)
          {
               if(numerator2 % j == 0 && denominator2 % j == 0)  
               {
               numerator2 /= j;
               denominator2 /= j;
               }
               --j;
          }

     }
     cout << "Simplified fraction two is: " << numerator2 << " / "
        << denominator2 << "\n";

}

// addition operator
RationalNumber RationalNumber::operator+(RationalNumber a)
{
      RationalNumber temp=RationalNumber(1,2,3,4);
      temp.numerator = numerator + a.numerator;
      temp.denominator = denominator + a.denominator;
      temp.numerator2 = numerator2 + a.numerator2;
      temp.denominator2 = denominator2 + a.denominator2;
      return temp;
}

// subtraction operator
RationalNumber RationalNumber::operator-(RationalNumber a)
{
      RationalNumber temp=RationalNumber(1,2,3,4);
      temp.numerator = numerator - a.numerator;
      temp.denominator = denominator - a.denominator;
      temp.numerator2 = numerator2 - a.numerator2;
      temp.denominator2 = denominator2 - a.denominator2;
      return temp;
}

// multiplication operator
RationalNumber RationalNumber::operator*(RationalNumber a)
{
      RationalNumber temp=RationalNumber(1,2,3,4);
      temp.numerator = numerator * a.numerator;
      temp.denominator = denominator * a.denominator;
      temp.numerator2 = numerator2 * a.numerator2;
      temp.denominator2 = denominator2 * a.denominator2;
      return temp;
}

// division operator
RationalNumber RationalNumber::operator/(RationalNumber a)
{
      RationalNumber temp=RationalNumber(1,2,3,4);
      temp.numerator = numerator / a.numerator;
      temp.denominator = denominator / a.denominator;
      temp.numerator2 = numerator2 / a.numerator2;
      temp.denominator2 = denominator2 / a.denominator2;
      return temp;
}

int main()
{

     int top;
     int bot;
     int top2;
     int bot2;

     cout << "Please enter the Numerator for fraction one: \n";
     cin >> top;
     cout << "Please enter the Denominator for fraction one: \n";
     cin >> bot;
     cout << "Please enter the Numerator for fraction two: \n";
     cin >> top2;
     cout << "Please enter the Denominator for fraction two: \n";
     cin >> bot2;

     RationalNumber A(top, bot, top2, bot2);
     return 0;

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文