0
#ifndef RATIONALNUMBER_H
#define RATIONALNUMBER_H

#include<iostream>
using namespace std;


class RationalNumber
{
    friend ostream &operator<<(ostream &out,RationalNumber &r);
    friend istream &operator>>(istream &in,RationalNumber &r);

    private:
        int numerator;
        int denominator;
    public:
        RationalNumber(int n,int d);
        RationalNumber();
        operator void*(){return (numerator== 0 ? NULL :this);}

};
#endif


#include"RationalNumber.h"
#include<stdexcept>
#include<cmath>

RationalNumber::RationalNumber():numerator(1),denominator(1)
{
}
RationalNumber::RationalNumber(int n,int d):numerator(n),denominator(d)
{
    if(denominator==0)
        throw runtime_error("cant divide by zero");
}
ostream &operator<<(ostream &out,RationalNumber &r)
{
    if(r.denominator == 1 || r.denominator == -1 && r.numerator !=0)

    {
    if(r.denominator<0 && r. numerator <0)
            out<<abs(r.numerator);
        else if(r.denominator<0 || r.numerator<0)
            out<<"-"<<abs(r.numerator);
        else
            out<<r.numerator;
    }
    else if(abs(r.denominator) == abs(r.numerator))
    {
        if(r.denominator<0 && r. numerator <0)
            out<<"1";
        else if(r.denominator<0 || r.numerator<0)
            out<<"-"<<"1";
        else if(r.denominator == 0 && r.numerator ==0)
            out<<"Nothing";
        else 
            out<<"1 ";
    }
    else if(r.numerator<0 && r.denominator >0 ||r.numerator>0 && r.denominator<0)
        out<<"-"<<abs(r.numerator)<<"/"<<abs(r.denominator);
    else if(r.numerator<0 && r.denominator<0)
        out<<abs(r.numerator)<<"/"<<abs(r.denominator);
    else if(r.numerator ==0)
        out<<r.numerator;
    else if(r.numerator>0 && r.denominator>0)
        out<<r.numerator<<"/"<<r.denominator;
    else if(r.numerator ==NULL && r.denominator == NULL)
        out<<" Nothing "<<endl;

    return out;
}
istream &operator>>(istream &in,RationalNumber &r)
{
    int top;
    int bottom;
    char divide;

    r.numerator = 0;
    r.denominator = 0;

    in>>top;

    if(in)
    {
        r.denominator = 1;

        while (isspace(divide = in.peek())&& divide != '\n')
            in.get();

        if(divide !='\n')
        {
            if(divide =='/')
            {
                in>>divide;

                while (isspace(bottom = in.peek()))
                    in.get();

                in>>bottom;

                if(!in || bottom==0)
                {
                    r.numerator=top;
                    in.clear(ios::failbit);
                }
                else
                {
                    r.numerator=top;
                    r.denominator=bottom;
                }
            }
            else
            {
                r.numerator=top;
                in.clear(ios::failbit);
            }
        }
        else
        {
            r.numerator = top;
        }

    }
    else
        in.clear(ios::failbit);

    return in;
}

/////////////////////////////////////////////// /////////////////////////////////////////////// /

12/34       */this is the file im reading in from. the class is supposed to
12             be a rational number class but i would like for a 23 to 
23             read correctly without causing an error.It
34             is only giving me problems with the last number in the file. Im not sure
23             whats going on*/

/////////////////////////////////////////////// /////////////////////////////////////////////// /

4

0 に答える 0