Форум программистов
 

Восстановите пароль или Зарегистрируйтесь на форуме, о проблемах и с заказом рекламы пишите сюда - alarforum@yandex.ru, проверяйте папку спам!

Вернуться   Форум программистов > IT форум > Помощь студентам
Регистрация

Восстановить пароль
Повторная активизация e-mail

Купить рекламу на форуме - 42 тыс руб за месяц

Ответ
 
Опции темы Поиск в этой теме
Старый 22.12.2011, 20:43   #1
PinkPink
Пользователь
 
Регистрация: 09.10.2011
Сообщений: 98
По умолчанию Fraction

Нужно реализовать класс Fraction. Нашла вот такой код.
class Fraction
{
int cel;//
double drob; //
public:

Fraction(double d)//
{
double temp; //
if (d>0)
temp=floor(d);
else
temp=ceil(d);
cel = (int)temp;//целая часть
drob = fabs(d-temp);//дробная часть
}

double toDouble()const
{
double d(0);
unsigned long temp=1;
double r=1;
for (int i=0;i<32;i++)
{
//if (second & temp)
d+=r;
temp<<=1;
r*=2;
}
temp=1;
for (int i=0;i<31;i++)
{
//if (first & temp)
d+=r;
temp<<=1;
r*=2;
}
/*if (first & temp)d*=-1;*/
return d;
}

double FtoDouble()const
{
double d;
d=toDouble();
d+=drob;
return d;
}

string operator!()
{
stringstream os;
os<<this->FtoDouble();
return os.str();
}
Fraction operator++()
{
cel++;
return *this;
}
Fraction operator++(int)
{
Fraction x(*this);
cel++;
return x;
}
Fraction operator--()
{
cel--;
return *this;
}
Fraction operator--(int)
{
Fraction x(*this);
cel--;
return x;
}
friend Fraction operator+ (const Fraction &x,const Fraction &y);
.....
friend istream& operator>>(istream &t, Fraction &x);

};
Fraction operator+ (const Fraction &x,const Fraction &y)// сложение
{
double dx,dy,dz;
dx=x.FtoDouble();
dy=y.FtoDouble();
dz=dx+dy;
Fraction z(dz);
return z;
}
Fraction operator- (const Fraction &x,const Fraction &y)//вычитание
{
double dx,dy,dz;
dx=x.FtoDouble();
dy=y.FtoDouble();
dz=dx-dy;
Fraction z(dz);
return z;
}
Fraction operator* (const Fraction &x,const Fraction &y)//умножение
{
double dx,dy,dz;
dx=x.FtoDouble();
dy=y.FtoDouble();
dz=dx*dy;
Fraction z(dz);
return z;
}
Fraction operator/ (const Fraction &x,const Fraction &y)//деление
{
double dx,dy,dz;
dx=x.FtoDouble();
dy=y.FtoDouble();
dz=dx/dy;
Fraction z(dz);
return z;
}
bool operator== (const Fraction &x,const Fraction &y)
{
return (x.cel==y.cel) && (x.drob==y.drob);
}
bool operator!= (const Fraction &x,const Fraction &y)
{
return !(x==y);
}
bool operator> (const Fraction &x,const Fraction &y)
{
if (x.cel>y.cel)return 1;
if (x.cel==y.cel)
{
if (cel.toDouble(x)>=0)return x.drob>y.drob;
return y.drob>x.drob;
}
return 0;
}
bool operator< (const Fraction &x,const Fraction &y)
{
return !(x>y) && !(x==y);
}
bool operator>= (const Fraction &x,const Fraction &y)
{
return (x>y) || (x==y);
}
bool operator<= (const Fraction &x,const Fraction &y)
{
return (x<y) || (x==y);
}
bool operator== (const Fraction &x,const double &y)
{
return (x.FtoDouble()==y);
}
bool operator!= (const Fraction &x,const double &y)
{
return (x.FtoDouble()!=y);
}
bool operator> (const Fraction &x,const double &y)
{
return (x.FtoDouble()>y);
}
bool operator< (const Fraction &x,const double &y)
{
return (x.FtoDouble()<y);
}
bool operator>= (const Fraction &x,const double &y)
{
return (x.FtoDouble()>=y);
}
bool operator<= (const Fraction &x,const double &y)
{
return (x.FtoDouble()<=y);
}
bool operator== (const double &x,const Fraction &y)
{
return (x==y.FtoDouble());
}
bool operator!= (const double &x,const Fraction &y)
{
return (x!=y.FtoDouble());
}
bool operator> (const double &x,const Fraction &y)
{
return (x>y.FtoDouble());
}
bool operator< (const double &x,const Fraction &y)
{
return (x<y.FtoDouble());
}
bool operator>= (const double &x,const Fraction &y)
{
return (x>=y.FtoDouble());
}
bool operator<= (const double &x,const Fraction &y)
{
return (x<=y.FtoDouble());
}
ostream& operator<<(ostream &t,const Fraction &x)
{
t<<x.FtoDouble();
return t;
}
istream& operator>>(istream &t, Fraction &x)
{
double d;
t>>d;
Fraction y(d);
x=y;
return t;
}
Но не понятно, что здесь для чего. Помогите разобраться!
Код был реализован через другой какой-то класс. Как сделать его без левых классов?
PinkPink вне форума Ответить с цитированием
Старый 22.12.2011, 20:48   #2
PinkPink
Пользователь
 
Регистрация: 09.10.2011
Сообщений: 98
По умолчанию

По поводу того, что конкретно непонятно.
Зачем нужна и что делает функции double toDouble()const, double FtoDouble()const.
И не совсем понятно почему при объявлении функции после типа имени и скобочек стоит const???
Что означает эта запись? temp<<=1;
PinkPink вне форума Ответить с цитированием
Ответ


Купить рекламу на форуме - 42 тыс руб за месяц



Похожие темы
Тема Автор Раздел Ответов Последнее сообщение
C++, Fraction vendetta1403 Помощь студентам 1 23.08.2010 16:19