C++ 添加分数和类-Woot

C++ 添加分数和类-Woot,c++,class,fractions,C++,Class,Fractions,嘿,伙计们,首先我要说的是,在发布这个问题之前,我已经研究了很多类似的程序,仍然需要一些帮助。我的问题在于加法分数类函数,我需要将一个分数加到另一个分数上。我有一个类,目前正在处理该类的实例(fractionObject和fractionObject2)。我分别存储分数,一个在fractionObject中,一个在fractionObject2中。如何将这些添加到分数类函数“add”中 任何提示都将不胜感激!谢谢你的时间 #include <iostream> #include &l

嘿,伙计们,首先我要说的是,在发布这个问题之前,我已经研究了很多类似的程序,仍然需要一些帮助。我的问题在于加法分数类函数,我需要将一个分数加到另一个分数上。我有一个类,目前正在处理该类的实例(fractionObject和fractionObject2)。我分别存储分数,一个在fractionObject中,一个在fractionObject2中。如何将这些添加到分数类函数“add”中

任何提示都将不胜感激!谢谢你的时间

#include <iostream>
#include <string>
#include <sstream>
using namespace std;

// Regular prototypes
int stringToNumber(const string &Text);
int GCD(int, int);
int LCM(int, int);

class fraction{

public: // Access Specifier
    int numerator;
    int  denominator; // Can never be 0



    // Function Prototypes
    fraction();
    void setNumDen();
    void reduce();
    void add();


};

// Member functions definitions
fraction::fraction()
{
    numerator = 0;
    denominator = 0;
}

void fraction::setNumDen()
{
    string numString;
    string denString;
    do{
        cout << "Enter a numerator and denominator of fraction 1 separated by whitespace: ";
        getline(cin, numString, ' ');
        getline(cin, denString);
        if (denString == "0")
            cout << endl << "Please enter a number that isn't zero." << endl;
    } while (denString == "0"); // Making sure denominator is not zero

    numerator = stringToNumber(numString);
    denominator = stringToNumber(denString);

}

void fraction::reduce()
{
    int leastCommonMultiple = 0;

    leastCommonMultiple = LCM(numerator, denominator);

    numerator /= leastCommonMultiple;
    denominator /= leastCommonMultiple;
}

void fraction::add()
{
    int leastComDen;

}

int main()
{

    fraction fractionObject, fractionObject2;

    fractionObject.setNumDen();
    fractionObject2.setNumDen();
    // Reducing and displaying the reduced fractions
    fractionObject.reduce();
    fractionObject2.reduce();
    cout << "Reduced Fraction 1 = " << fractionObject.numerator << "/" << fractionObject.denominator << "\t" << "Reduced Fraction 2 = " << fractionObject2.numerator << "/" << fractionObject2.denominator << endl;
    // Adding and displaying the fractions



    system("pause");
    return 0;
}

// Function to convert string to number
int stringToNumber(const string &Text)//Text not by const reference so that the function can be used with a 
{                               //character array as argument
    stringstream ss(Text);
    int result;
    return ss >> result ? result : 0;
}

// result=GCD(a,b)
int LCM(int a, int b) {
    int temp = 0;

    while (b != 0) {
        temp = b;
        b = a%b;
        a = temp;
    }
    return a;
}

// result=LCM(a,b);
int GCD(int a, int b) {
    int result = 0;

    result = a * (b / LCM(a, b));
    return result;
}
#包括
#包括
#包括
使用名称空间std;
//常规原型
int stringToNumber(常量字符串和文本);
int GCD(int,int);
int-LCM(int,int);
类分数{
公共访问说明符
整数分子;
int分母;//永远不能为0
//功能原型
分数();
void setNumDen();
void reduce();
无效添加();
};
//成员函数定义
分数::分数()
{
分子=0;
分母=0;
}
空隙率::setNumDen()
{
字符串numString;
字符串denString;
做{

这里没有完整的答案,但是add应该有两个
常量分数&
参数,并返回一个临时
分数
对象。您可以将其重命名为
操作符+
。许多库添加了一个
+=
操作符,不需要创建临时对象。C++11允许您减少这些临时对象的开销具有移动构造函数的对象


至于实现,这里有一个提示:1/6+1/9=(9+6)/54=5/18。我注意到您已经有了一个reduce函数。

我建议从更改add原型开始:
void add(const-fraction&toAdd)
将要添加到该分数的分数传递给
该分数。感谢您的输入,我这样做了,并且仍在努力解决它!注意,由于您有一个reduce方法,您不需要首先找到最小公分母(正如您计划的那样)。只需找到一个公倍数(例如,两个分母的乘积)完成后调用reduce。基本上同意。如果
add
要取两个分数并返回一个新分数,则它应该是静态方法或自由函数。
reduce
更改对象,并且
add
的行为应该相同。感谢大家的帮助!我正在接近解决这个问题!