Closed. This question needs details or clarity。它当前不接受答案。
                            
                        
                    
                
                            
                                
                
                        
                            
                        
                    
                        
                            想改善这个问题吗?添加详细信息并通过editing this post阐明问题。
                        
                        5年前关闭。
                                                                                            
                
        
我需要编写一个程序来计算分数,这是我的头文件:

#ifndef FRACTION_H
#define FRACTION_H
#include <iostream>
#include <string>
using namespace std;

class Fraction {

    private:
        int *numer;
        int *denom;
        int gcd(int, int);
    public:
        void reduce();
        int getNum();
        int getDen();
        Fraction();
        Fraction(int numerator);
        Fraction(int num, int den);
        Fraction(string s);  // take string parameter of the form of "numerator/defnominator
        Fraction(Fraction &other);  // copy constructor
        Fraction & operator=(Fraction & rhs);
        ~Fraction();
        // overloading arithematic operation
        Fraction & operator+ (Fraction & rhs);
        Fraction & operator- (Fraction & rhs);
        Fraction & operator* (Fraction & rhs);
        Fraction & operator/ (Fraction & rhs);
        bool operator > (Fraction & rhs);
        bool operator >= (Fraction & rhs);
        bool operator == (Fraction & rhs);
        bool operator < (Fraction & rhs);
        bool operator <= (Fraction & rhs);
        bool operator!=(Fraction & rhs);
        Fraction & operator++();
        Fraction & operator++(int);
        Fraction & operator--();
        Fraction & operator--(int);

        Fraction & operator+=(Fraction & rhs);
        Fraction & operator-=(Fraction & rhs);
        Fraction & operator*=(Fraction & rhs);
        Fraction & operator/=(Fraction & rhs);

        // Exponentiation
        Fraction & operator^(int n);

        bool isZero();
        bool isProper();  // a fracton is proper if abs(numerator) < (denominator)
        bool isNegative();
        bool isPositive();

        operator string();
        operator double();

        string toString();
        string toProperString();   // properString format of 15/4   is  3 3/4

        friend ostream & operator <<  (ostream & out, Fraction & rhs);
        friend istream & operator >> (istream & in, Fraction &rhs);
};

#endif


对于Fraction.cpp文件:

我具有将派系格式转换为字符串的功能,这就是我所拥有的功能,但不确定是否正确。
这是我现在得到的:

#include <iostream>
#include<cmath>
#include <cassert>
#include <string>
#include<iomanip>
#include <stdlib.h>
#include "Fraction.h"
#include <sstream>

using namespace std;

Fraction::Fraction()
{
    numer = new int(0);
    denom = new int(1);
    //cout<<"construtor"<<endl;
}
int Fraction::getNum()
{
    return *numer;

}
int Fraction::getDen()
{
    return *denom;
}
int Fraction::gcd(int n, int d)      //moving sign to numerator
{
    //assert((n > 0 && d > 0));         //ensuring numerator and demominator have no common divisors
    //while (n != d)                    // return the greatest common divisor
    //{
    //  if (n < d)
    //      d = d - n;
    //  else
    //      n = n - d;
    //}
    //return n;
    if(n<0)
        n=-n;
    if(d<0)
        d=-d;
    if(n<d)
        return gcd(d,n);
    if(d==0)
        return n;
    return gcd(d,n%d);

}
Fraction::Fraction(int numerator)
{
    numer = new int(numerator);
    denom= new int (1);
    //printf("Fraction::Fraction(int numerator) \n");
}

Fraction::Fraction(int num, int den)
{
    assert (den != 0);

    numer = new int(num);
    denom = new int(den);
    reduce();

    //cout<<"reduce function"<<endl;
}
void Fraction::reduce()
{
    int sign = 1;
    if (*numer < 0)
    {
        sign = -1;
        *numer = -*numer;
    }
    if (*numer< 0)
    {
        sign = -1;
        *denom = -*denom;
    }
    assert(*denom != 0);
    int d = 1;
    if (*numer>0)
    {
        d= gcd(*numer, *denom);
        *numer = sign*(*numer / d);
        *denom = *denom / d;
    }
}

Fraction::Fraction(string s)
{
    string delimiter = "/";
    string n = s.substr(0, s.find(delimiter));
    string d=s.substr(s.find(delimiter)+1,s.length());

    numer = new int (atoi(n.c_str()));
    denom = new int(atoi(d.c_str()));   //every first time using pointer in constructor
    cout << n << d << endl;
    //constructor
}
Fraction::Fraction(Fraction &other)
{
    numer = new int( other.getNum());
    denom = new int(other.getDen());
    //cout<<"copy construtor"<<endl;
}

Fraction::~Fraction()
{
    delete numer;
    numer = 0;           //if (numer) if(denom)
    delete denom;
    denom = 0;
}

Fraction & Fraction::operator=(Fraction & rhs)
{
    if (this != &rhs)
    {                          //do i need delete pointer here?
        *numer = rhs.getNum();
        *denom = rhs.getDen();
    }
    else
        return *this;
}
Fraction & Fraction::operator+ (Fraction & rhs)
{
    Fraction *result = new Fraction(this->getNum()*rhs.getDen() + this->getDen()*rhs.getNum(), this->getDen()*rhs.getDen());
    result->reduce();
    return *result;
}

Fraction & Fraction::operator- (Fraction & rhs)
{
    Fraction *result=new Fraction((this->getNum()*rhs.getDen() - this->getDen()*rhs.getNum(), this->getDen()*rhs.getDen()));
    result->reduce();
    return *result;
}

Fraction & Fraction::operator*(Fraction & rhs)
{
    Fraction *result=new Fraction((this->getNum()*rhs.getNum(), this->getDen()*rhs.getDen()));
    result->reduce();
    return *result;

}

Fraction & Fraction::operator/(Fraction & rhs)
{
    Fraction *result=new Fraction((this->getNum()*rhs.getDen(), this->getDen()*rhs.getNum()));
    result->reduce();
    return *result;
}

bool Fraction::operator > (Fraction & rhs)
{
    if (((float)getNum() / getDen())>((float)rhs.getDen() / rhs.getNum()))
        return true;
    else
        return false;
}

bool Fraction::operator >= (Fraction & rhs)
{
    if (((float)getNum() / getDen()) >= ((float)rhs.getDen() / rhs.getNum()))
        return true;
    else
        return false;
}

bool Fraction::operator == (Fraction & rhs)
{
    if (getNum()*rhs.getDen() == getDen()*rhs.getNum())
        return true;
    else
        return false;

}
bool Fraction::operator < (Fraction & rhs)
{
    if (((float)getNum() / getDen())<((float)rhs.getDen() / rhs.getNum()))
        return true;
    else
        return false;
}
bool Fraction::operator <= (Fraction & rhs)
{
    if (((float)getNum() / getDen()) <= ((float)rhs.getDen() / rhs.getNum()))
        return true;
    else
        return false;
}

bool Fraction::operator!=(Fraction & rhs)
{
    if (getNum()*rhs.getDen() == getDen()*rhs.getNum())
        return false;
    else
        return true;

}

Fraction & Fraction::operator++()  //pre fix
{
    *numer += *denom;
    reduce();
    return *this;
}

Fraction & Fraction::operator++(int inInt)  //post fix
{

    Fraction *temp = new Fraction (*this);
    *numer += *denom;
    reduce();
    return *temp;

}

Fraction & Fraction::operator--()
{
    *numer -= *denom;
    reduce();
    return *this;
}

Fraction & Fraction::operator--(int)
{
    Fraction *temp = new Fraction(*this);
    *numer -= *denom;
    reduce();
    return *temp;
}

Fraction & Fraction::operator+=(Fraction & rhs)
{
    *numer = (*numer)*rhs.getDen() + (*denom)*rhs.getNum();
    *denom = (*denom)*rhs.getDen();
    reduce();
    return *this;

}
Fraction & Fraction::operator-=(Fraction & rhs)
{
    *numer = (*numer)*rhs.getDen() - (*denom)*rhs.getNum();
    *denom = (*denom)*rhs.getDen();
    reduce();
    return *this;
}
Fraction & Fraction::operator*=(Fraction & rhs)
{
    *numer=(*numer)*rhs.getNum();
    *denom=(*denom)*rhs.getDen();
    reduce();
    return *this;
}

Fraction & Fraction::operator/=(Fraction & rhs)
{
    *numer=(*numer)*rhs.getDen();
    *denom=(*denom)*rhs.getNum();
    reduce();
    return *this;

}

Fraction & Fraction::operator^(int n)
{
    *numer = (double)pow((float)*numer, n);

    *denom = (double)pow((float)*denom, n);
    reduce();
    return *this;
}

bool Fraction::isZero()
{
    return (*numer) == 0;
}

bool Fraction::isProper()
{
    if (abs(*numer)<abs(*denom))
        return true;
    else
        return false;
}

bool Fraction::isNegative()
{
    if (*numer<0)
        return true;
    else
        return false;
}

bool Fraction::isPositive()
{
    if (*numer>0)
        return true;
    else
        return false;
}

Fraction::operator string()
{
    return this->toString();
}
Fraction::operator double()
{
    return ((double)(*numer) / (*denom));
}

string Fraction::toString()
{
    char num[100], deom[100];
    char *s = new char[50];
    itoa(*numer, num, 10);
    itoa(*denom, deom, 10);

    char * delimiter = new char[2];
    delimiter[0] = '\/';
    delimiter[1] = '\0';   //stops copying delimiter

    strcpy(s, num);
    strcat(s, delimiter);
    //  strcat(s,'\0');
    strcat(s, deom);
    //  strcat(s,'\0');

    return s;

}
string Fraction::toProperString()
{
    int a = *(this->numer) / *(this->denom);
    int num = *(this->numer) % *(this->denom);
    ostringstream ostr;
    ostr <<a << " " << num << "/" << *(this->denom);
    return ostr.str();

}

ostream & operator <<  (ostream & out, Fraction & rhs)
{
    if(rhs.getDen()!=1)
    {
        out << rhs.getNum() << "/" << rhs.getDen();
    }
    else
        cout<<rhs.getNum();
    return out;
}

istream & operator >> (istream & in, Fraction &rhs)
{
    int n, d;
    in >> n;
    char c;
    in >> c;
    if (c == '/')
        in >> d;
    else
    {
        in.putback(c);
        d = 1;
    }
    rhs = Fraction(n, d);
    return in;
}


这是主程序

#include "Fraction.h"

void main()
{
    Fraction a1;
    Fraction a(1,2);
    Fraction b(4,5);
    Fraction c(6,8);
    Fraction d(b);
    Fraction g(-4,8);
    Fraction g1(4,-10);
    Fraction z(7,5);

    cout << a1 << endl;
    cout << a  << endl;
    cout << b  << endl;
    cout << c  << endl;
    cout << d  << endl;
    cout << g  << endl;
    cout << g1 << endl;

    string s  = "3/4";
    string s1 = "2/-3";
    Fraction b1(s);
    Fraction b2(s1);

    cout << b1 << endl;
    cout << b2 << endl;

    a1 = b + c; cout << a1 << endl;
    a1 = b-c  ; cout << a1 << endl;
    a1 = b*c  ; cout << a1 << endl;
    a1 = b / c; cout << a1 << endl;

    b += a; cout << b << endl;
    b -= a; cout << b << endl;
    b /= a; cout << b << endl;
    b++   ; cout << b << endl;
    ++b   ; cout << b << endl;
    b--   ; cout << b << endl;
    --b   ; cout << b << endl;
    b /= a; cout << b << endl;
    b *a  ; cout << b << endl;
    b^2   ; cout << b << endl;

    cout << a.toString()       << endl;
    cout << z.toProperString() << endl;

    Fraction f1(-4,5);
    Fraction f2(0,1);
    cout << "is f1 negative?"             << f1.isNegative() << endl;
    cout << "is f2 zero?"                 << f2.isZero()     << endl;
    cout << "is f1 a proper fraction?"    << f1.isProper()   << endl;
    cout << "is f1 a positive fraction? " << f1.isPositive() << endl;

    a = Fraction(9, 8);
    b = Fraction(7, 8);

    if (a < b)
        cout << "a is smaller than b"   << endl;
    else
        cout << " a is larger than b"   << endl;

    if(a==b)
        cout << "a is equal to b"       << endl;
    else
        cout << "a does not equal to b" << endl;
}


由于没有人愿意帮助我,所以我在这里为所有人张贴了所有代码。
我花了一段时间在助教的帮助下完成了这个项目。

最佳答案

您正在各个地方使用

Fraction *result=new Fraction((this->getNum()*rhs.getDen() - this->getDen()*rhs.getNum(), this->getDen()*rhs.getDen()));


这需要

Fraction *result=new Fraction(this->getNum()*rhs.getDen() - this->getDen()*rhs.getNum(), this->getDen()*rhs.getDen());


否则您实际上只会传递this->getDen()*rhs.getDen()How does the Comma Operator work)。另请参阅项目符号3。
Fraction & Fraction::operator=(Fraction & rhs)在所有情况下均无法返回值(删除else
这不是JAVA。不要执行new int(1)。只是:int(1)。节省了内存管理(What is The Rule of Three?)的麻烦
不要执行using namespace std;。当然不在头文件(Why is "using namespace std" considered bad practice?)中。另请参阅项目符号19。
优先使用C ++ 11标头(例如<cstdlib>)而不是C库标头
<stdlib.h>
您的toString方法使用非标准的itoa。另外,每次调用都会泄漏几个char缓冲区...请尝试c ++样式:

string Fraction::toString()
{
    return std::to_string(*numer) + '/' + std::to_string(*denom);
}


看到?一条线,没有更多的泄漏。不再有终止NUL的临时人员。没有更多的strcat废话了。只是业务逻辑。 (当然,仍然需要解决前面的项目符号中提到的其他问题)
哦,要尽量让成员成为const

string Fraction::toString() const

它必须是int main(),而不是void。至少对于符合标准/便携式的代码。
复制构造函数必须使用const&参数。

Fraction(Fraction const& other);  // copy constructor


复制分配操作符相同。

以下行不应在符合标准的编译器上进行编译:

rhs = Fraction(n, d);


原因是,临时(rhs)无法(合法地)绑定到非常量参考参数。再次注意,这很重要,您必须将getNum()getDen()标记为const,否则将无法在参数上调用它们
从约定返回具有const语义的操作符的引用,这违反了“最不惊奇原则”,因此被认为是有害的:


Fraction& Fraction::operator+(Fraction&)应该按值返回
Fraction& Fraction::operator-(Fraction&)应该按值返回
Fraction& Fraction::operator*(Fraction&)应该按值返回
Fraction& Fraction::operator/(Fraction&)应该按值返回
后缀Fraction& Fraction::operator++(int)应返回Fraction
后缀Fraction& Fraction::operator--(int)应返回Fraction


使用这些运算符的结果很容易得到意外的结果。给定const-正确性,问题可以得到减轻,但是运算符的实用性仍未达到应有的水平。
除非您知道自己在做什么,否则不要混用doublefloat(提示:您不会:Floating point inaccuracy examples


  时间流逝...好吧,我整理了一些。 new的所有使用都完全被误导了(对不起)。重用可以简化许多代码。

不要if (cond) return true; else return false;。而是return cond;
您的构造函数已经执行reduce()。因此,让您的操作员仅依靠它,而不是进行多余的减少:

 Fraction Fraction::operator*(Fraction const& rhs) const {
     return Fraction(numer*rhs.numer, denom*rhs.denom);
 }

 Fraction& Fraction::operator*=(Fraction const& rhs) {
     return *this = (*this * rhs);
 }

析构函数现在是多余的(不再有指针!)
构造函数已概括为三个:

 Fraction(int num = 0, int den = 1);
 Fraction(std::string const& s);  // take string parameter of the form of "numerator/defnominator
 Fraction(Fraction const& other);  // copy constructor

构造函数使用初始化列表:

 Fraction::Fraction(int numerator, int den) : numer(numerator), denom(den)
 {
     assert(denom != 0);
     reduce();
 }

gcd可以是静态的(并且不必是类成员。只需在实现文件中将其设置为静态)即可。
一些“测试用例”和输出是相当无用的,例如

     b *a  ; cout << b << endl;
     b^2u  ; cout << b << endl;


不打印与表达式相关的任何内容。我更新了它们以使其更有用:

     b *= a;     cout << "b *= a; // "     << b  << endl;
     b ^= 2u;    cout << "b ^= 2; // "     << b  << endl;

您不小心使用了cout而不是out中的operator<<
即使在operator>=<=中也不要执行浮点相等(有关上述floatdouble的信息,请参见上文,请参见项目符号11。)


事不宜迟,这是我的版本。免责声明我没有检查很多逻辑。我刚刚回顾了编码问题和风格。

输出:

a1: 0
a : 1/2
b : 4/5
c : 3/4
d : 4/5
g : -1/2
g1: 2/-5
b1: 3/4
b2: 2/-3
a1 = b + c; // 31/20
a1 = b-c  ; // 1/20
a1 = b*c  ; // 3/5
a1 = b / c; // 16/15
b += a; // 13/10
b -= a; // 4/5
b /= a; // 8/5
b++   ; // 13/5
++b   ; // 18/5
b--   ; // 13/5
--b   ; // 8/5
b /= a; // 16/5
b *= a; // 8/5
b ^= 2; // 64/25
1/2
1 2/5
is f1 negative? true
is f2 zero? true
is f1 a proper fraction? true
is f1 a positive fraction? false
a is not smaller than b
a is not equal to b


看到Live On Coliru

请注意,在启用所有警告的情况下,代码可以干净地编译:-Wall -pedantic -Wextra -Werror -Weffc++

代码清单

#ifndef FRACTION_H
#define FRACTION_H
#include <iostream>
#include <string>
#include <stdexcept>
#include <limits>

class Fraction {

    private:
        int numer;
        int denom;
        // static int gcd(int, int);
    public:
        void reduce();
        int getNum() const;
        int getDen() const;
        Fraction(int num = 0, int den = 1);
        Fraction(std::string const& s);  // take string parameter of the form of "numerator/denominator
        Fraction(Fraction const& other);  // copy constructor
        Fraction& operator=(Fraction const& rhs);
        // overloading arithematic operation
        Fraction  operator+  (Fraction const& rhs) const;
        Fraction  operator-  (Fraction const& rhs) const;
        Fraction  operator*  (Fraction const& rhs) const;
        Fraction  operator/  (Fraction const& rhs) const;
        bool      operator>  (Fraction const& rhs) const;
        bool      operator>= (Fraction const& rhs) const;
        bool      operator== (Fraction const& rhs) const;
        bool      operator<  (Fraction const& rhs) const;
        bool      operator<= (Fraction const& rhs) const;
        bool      operator!= (Fraction const& rhs) const;
        Fraction& operator++ ();
        Fraction& operator-- ();
        Fraction  operator++ (int);
        Fraction  operator-- (int);

        Fraction& operator+=(Fraction const& rhs);
        Fraction& operator-=(Fraction const& rhs);
        Fraction& operator*=(Fraction const& rhs);
        Fraction& operator/=(Fraction const& rhs);

        // Exponentiation
        Fraction operator^(unsigned n) const;
        Fraction& operator^=(unsigned n);

        bool isZero()     const;
        bool isProper()   const; // a fracton is proper if abs(numerator) < (denominator)
        bool isNegative() const;
        bool isPositive() const;

        operator std::string() const;
        operator double() const;

        std::string toString() const;
        std::string toProperString() const; // properString format of 15/4   is  3 3/4

        friend std::ostream& operator<< (std::ostream& out, Fraction const& rhs);
        friend std::istream& operator>> (std::istream& in, Fraction& rhs);
};

#endif

// for the Fraction.cpp file:

#include <iostream>
#include<cmath>
#include <cassert>
#include <string>
#include <iomanip>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <sstream>

int Fraction::getNum() const
{
    return numer;
}

int Fraction::getDen() const
{
    return denom;
}

static int gcd(int n, int d)      //moving sign to numerator
{
    if(n<0)
        n=-n;
    if(d<0)
        d=-d;
    if(n<d)
        return gcd(d,n);
    if(d==0)
        return n;
    return gcd(d,n%d);
}

Fraction::Fraction(int numerator, int den) : numer(numerator), denom(den)
{
    assert(denom != 0);
    reduce();
}

void Fraction::reduce()
{
    int sign = 1;
    if (numer < 0)
    {
        sign = -1;
        numer = -numer;
    }
    if (numer< 0)
    {
        sign = -1;
        denom = -denom;
    }
    assert(denom != 0);
    int d = 1;
    if (numer>0)
    {
        d     = gcd(numer, denom);
        numer = sign*(numer / d);
        denom = denom / d;
    }
}

Fraction::Fraction(std::string const& s)
    : Fraction()
{
    std::istringstream iss(s);

    char delimiter = 0;

    if (
            (iss >> numer)
         && (iss >> delimiter)
         && (delimiter == '/')
         && (iss >> denom))
    {
        assert(denom != 0);
        reduce();
    } else
    {
        throw std::runtime_error("invalid conversion to Fraction");
    }
}

Fraction::Fraction(Fraction const& other)
    : numer(other.numer), denom(other.denom)
{
}

Fraction& Fraction::operator=(Fraction const& rhs)
{
    if (this != &rhs)
    {
        numer = rhs.numer;
        denom = rhs.denom;
    }
    return *this;
}

Fraction Fraction::operator+ (Fraction const& rhs) const
{
    return Fraction(numer*rhs.denom + denom*rhs.numer, denom*rhs.denom);
}

Fraction Fraction::operator- (Fraction const& rhs) const
{
    return Fraction(numer*rhs.denom - denom*rhs.numer, denom*rhs.denom);
}

Fraction Fraction::operator*(Fraction const& rhs) const
{
    return Fraction(numer*rhs.numer, denom*rhs.denom);
}

Fraction Fraction::operator/(Fraction const& rhs) const
{
    return Fraction(numer*rhs.denom, denom*rhs.numer);
}

bool Fraction::operator > (Fraction const& rhs) const
{
    return static_cast<double>(*this) > rhs;
}

bool Fraction::operator >= (Fraction const& rhs) const
{
    return (*this == rhs) || static_cast<double>(*this) > rhs;
}

bool Fraction::operator == (Fraction const& rhs) const
{
    return (1l*numer*rhs.denom == 1l*denom*rhs.numer);
}

bool Fraction::operator < (Fraction const& rhs) const
{
    return static_cast<double>(*this) < rhs;
}

bool Fraction::operator <= (Fraction const& rhs) const
{
    return (*this == rhs) || static_cast<double>(*this) < rhs;
}

bool Fraction::operator!=(Fraction const& rhs) const
{
    return !(*this == rhs);
}

Fraction& Fraction::operator++()  //prefix
{
    numer += denom;
    reduce();
    return *this;
}

Fraction Fraction::operator++(int) //postfix
{
    Fraction temp = *this;
    numer += denom;
    reduce();
    return temp;
}

Fraction& Fraction::operator--()
{
    numer -= denom;
    reduce();
    return *this;
}

Fraction Fraction::operator--(int)
{
    Fraction temp = *this;
    numer -= denom;
    reduce();
    return temp;
}

Fraction& Fraction::operator+=(Fraction const& rhs)
{
    return *this = (*this + rhs);
}

Fraction& Fraction::operator-=(Fraction const& rhs)
{
    return *this = (*this - rhs);
}

Fraction& Fraction::operator*=(Fraction const& rhs)
{
    return *this = (*this * rhs);
}

Fraction& Fraction::operator/=(Fraction const& rhs)
{
    return *this = (*this / rhs);
}

// utility
template <typename T>
T int_pow(T x, unsigned exponent)
{
    static_assert(std::is_integral<T>(), "only supports integral types");

    intmax_t base = x, result = 1;

    while (exponent != 0)
    {
        // TODO protect against overflows
        if (exponent % 2)
            result *= base;

        exponent /= 2;
        base *= base;
    }

    assert(result <= std::numeric_limits<T>::max()); // value too large to be represented
    assert(result >= std::numeric_limits<T>::min()); // value too small to be represented
    return static_cast<T>(result);
}

Fraction Fraction::operator^(unsigned n) const
{
    return Fraction(int_pow(numer, n), int_pow(denom, n));
}

Fraction& Fraction::operator^=(unsigned n)
{
    return *this = (*this ^ n);
}

bool Fraction::isZero() const
{
    return numer == 0;
}

bool Fraction::isProper() const
{
    return abs(numer)<abs(denom);
}

bool Fraction::isNegative() const
{
    return numer<0;
}

bool Fraction::isPositive() const
{
    return numer>0;
}

Fraction::operator std::string() const
{
    return toString();
}

Fraction::operator double() const
{
    return (static_cast<double>(numer) / denom);
}

std::string Fraction::toString() const
{
    return std::to_string(numer) + '/' + std::to_string(denom);
}

std::string Fraction::toProperString() const
{
    int a   = numer / denom;
    int num = numer % denom;

    std::ostringstream ostr;
    ostr << a << " " << Fraction(num, denom);
    return ostr.str();
}

std::ostream& operator <<  (std::ostream& out, Fraction const& rhs)
{
    if(rhs.denom!=1)
        return out << rhs.getNum() << "/" << rhs.denom;
    else
        return out << rhs.getNum();
}

std::istream& operator >> (std::istream& in, Fraction& rhs)
{
    int n, d = 1;
    char c;

    if (in >> n >> c)
    {
        if (c == '/')
        {
            in >> d;
        }
        else
        {
            in.putback(c);
        }
        rhs = Fraction(n, d);
    }
    return in;
}

// And here's the main program

int main()
{
    Fraction a1;
    Fraction a(1,2);
    Fraction b(4,5);
    Fraction c(6,8);
    Fraction d(b);
    Fraction g(-4,8);
    Fraction g1(4,-10);
    Fraction z(7,5);

    std::cout << "a1: " << a1 << "\n";
    std::cout << "a : " << a  << "\n";
    std::cout << "b : " << b  << "\n";
    std::cout << "c : " << c  << "\n";
    std::cout << "d : " << d  << "\n";
    std::cout << "g : " << g  << "\n";
    std::cout << "g1: " << g1 << "\n";

    std::string s  = "3/4";
    std::string s1 = "2/-3";
    Fraction b1(s);
    Fraction b2(s1);

    std::cout << "b1: " << b1 << "\n";
    std::cout << "b2: " << b2 << "\n";

    a1 = b + c; std::cout << "a1 = b + c; // " << a1 << "\n";
    a1 = b-c  ; std::cout << "a1 = b-c  ; // " << a1 << "\n";
    a1 = b*c  ; std::cout << "a1 = b*c  ; // " << a1 << "\n";
    a1 = b / c; std::cout << "a1 = b / c; // " << a1 << "\n";

    b += a;     std::cout << "b += a; // "     << b  << "\n";
    b -= a;     std::cout << "b -= a; // "     << b  << "\n";
    b /= a;     std::cout << "b /= a; // "     << b  << "\n";
    b++   ;     std::cout << "b++   ; // "     << b  << "\n";
    ++b   ;     std::cout << "++b   ; // "     << b  << "\n";
    b--   ;     std::cout << "b--   ; // "     << b  << "\n";
    --b   ;     std::cout << "--b   ; // "     << b  << "\n";
    b /= a;     std::cout << "b /= a; // "     << b  << "\n";
    b *= a;     std::cout << "b *= a; // "     << b  << "\n";
    b ^= 2u;    std::cout << "b ^= 2; // "     << b  << "\n";

    std::cout << a.toString()       << "\n";
    std::cout << z.toProperString() << "\n";

    Fraction f1(-4,5);
    Fraction f2(0,1);
    std::cout << "is f1 negative? "            << std::boolalpha << f1.isNegative() << "\n";
    std::cout << "is f2 zero? "                << std::boolalpha << f2.isZero()     << "\n";
    std::cout << "is f1 a proper fraction? "   << std::boolalpha << f1.isProper()   << "\n";
    std::cout << "is f1 a positive fraction? " << std::boolalpha << f1.isPositive() << "\n";

    a = Fraction(9, 8);
    b = Fraction(7, 8);

    if (a < b)
        std::cout << "a is smaller than b"     << "\n";
    else
        std::cout << "a is not smaller than b" << "\n";

    if(a==b)
        std::cout << "a is equal to b"         << "\n";
    else
        std::cout << "a is not equal to b"     << "\n";
}

关于c++ - 分数运算符,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/22053803/

10-12 00:45