我需要作业帮助。
我对这一切还是陌生的。
我想

创建具有以下功能的类RationalNumber(分数):

创建一个构造函数,该构造函数可防止分数中的0分母,减少或简化非归约形式的分数并避免负分母。
重载此类的加法,减法,乘法和除法运算符。
重载此类的关系和相等运算符。

将分数加在一起时,我很难降低分数。例如,如果我的第一个分数是1/3,第二个也是1/3,答案应该是2/3。但是无论我何时运行程序,无论输入多少分数,答案都是2/1。

这是我的RationalNumber头文件:

#pragma once
#include <iostream>
using namespace std;

//RationalNumber class
class RationalNumber
{
public:
    RationalNumber(); //constructor
    ~RationalNumber(); //destructor

    void retrieveInput();
    int GreatestCommonDenom(int num1, int remainder);
    void reduceFraction(int &num, int &denom);
    void operator+(RationalNumber o1);
    void operator-(RationalNumber o1);
    void operator*(RationalNumber o1);
    //void operator/(RationalNumber o1);

private:
    int num, denom;
};


这是我的RationalNumber函数:

#include "RationalNumber.h"
#include <iostream>
using namespace std;

//RationalNumber Class Functions

RationalNumber::RationalNumber() //constructor
{
    //initializes objects
    num = 1;
    denom = 1;
}

RationalNumber::~RationalNumber() //destructor
{
    //de-allocates memory
}

void RationalNumber::retrieveInput() //creates user fractions
{
    int num, denom;

    cout << "\nEnter a numerator: \n\n";
    cin >> num;
    cout << "\nEnter a denominator: \n\n";
    cin >> denom;
    cout << endl;

    //denominator
    while (denom == 0)
    {
        cout << "Please enter a denominator: \n\n";
        cin >> denom;
    }

    while (denom < 0)
    {
        num *= -1;
        denom *= -1;
    }

    cout << num << "/" << denom << endl;
}

int RationalNumber::GreatestCommonDenom(int num1, int remainder) //gets lowest common denominatior
{
    if (remainder == 0)
    {
        return(num1);
    }
    else
    {
        return(GreatestCommonDenom(remainder, num1%remainder));
    }
}

void RationalNumber::reduceFraction(int &num, int &denom) //reduces the fraction to its lowest form
{
    int reduceFrac = 0;
    if (denom > num)
    {
        reduceFrac = GreatestCommonDenom(denom, num);
    }
    else if (denom < num)
    {
    reduceFrac = GreatestCommonDenom(num, denom);
    }
    else
    {
        reduceFrac = GreatestCommonDenom(num, denom);
    }

    num /= reduceFrac;
    denom /= reduceFrac;
    cout << "After reduction, the answer is " << num << "/" << denom << endl;
}
void RationalNumber::operator+(RationalNumber o1) //adds the fractions
{
    RationalNumber temp;
    temp.num = (this -> num * o1.denom) + (o1.num * this -> denom);
    temp.denom = (this -> denom * o1.denom);
    reduceFraction(temp.num, temp.denom);
}
//void operator-(RationalNumber o1)
//{}
//void operator*(RationalNumber o1)
//{}
//void operator/(RationalNumber o1){}


到目前为止,这是我的主要程序:

#include <iostream>
#include "RationalNumber.h"
using namespace std;

int main()
{
    cout << "Welcome!\n" << endl;
    cout << "Today we shall preform some math functions on fractions.\n" << endl;

    //create two objects
    RationalNumber frac1;
    RationalNumber frac2;

    //retrieve user input for their fractions
    cout << "Enter your first fraction: \n\n";
    frac1.retrieveInput();
    cout << "\nEnter your second fraction: \n\n";
    frac2.retrieveInput();

    //using this to see the output (only temporary)
    frac1.operator+(frac2);

    int answer;

    cout << "Menu" << endl;
    cout << "\nWhich would you like to preform on your fractions: \n" << endl;
    cout << "1: Addition\n" << endl;
    cout << "2: Subtraction\n" << endl;
    cout << "3: Multiplication\n" << endl;
    cout << "4: Division\n" << endl;
    cout << "Enter your option: \n" << endl;
    cin >> answer;

    return answer;


    switch (answer)
    {
    case 1:
        frac1.operator+(frac2);
        break;

    default:
        cout << "Invalid option." << endl;
    }

    cout << "\n" << endl;
    system("pause");
    return 0;
}


为了完成输出的答案,我在开关方面也遇到了一些麻烦,但是之后它几乎关闭了程序,所以我不知道它的输出是什么。任何帮助是极大的赞赏!谢谢!

最佳答案

我将创建一个总是从构造函数调用的reduce函数:

RationalNumber::RationalNumber(int a, int b)
{
    // do error checking that denominator b is non-zero etc.

    this->a_ = a;
    this->b_ = b;
    reduce(this->a_, this->b_);
}


然后,我们所有的运算符(如add)都将使用构造函数创建一个新的有理数。天真的reduce函数的示例可能是(对其进行修改以处理负数):

void reduce(int& a, int& b)
{
    int k = 2;
    while(k <= ((a <= b) ? a : b)){
        if(a%k == 0 && b%k == 0){
            a /= k;
            b /= k;
        }
        else
            ++k;
    }
}


关键是要从构造函数中调用它,以使其最简化的形式始终有理数。因此,添加功能可能类似于

RationalNumber RationalNumber::add(const RationalNumber& r) const
{
    return RationalNumber(this->a_ * r.b_ + this->b_ * r.a_, this->b_ * r.b_);
}

关于c++ - 用C++编写RationalNumber类,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/29185818/

10-10 16:29