Хотите улучшить этот вопрос? Добавьте больше подробностей и уточните проблему, отредактировав это сообщение.
Закрыт 9 месяцев назад.
У меня перегрузка + и << работает, но для этого мне нужно писать все с указателями (*C=*A+*B; cout << *C). Что нужно изменить, чтобы работало без них?
#include "pch.h"
#include <iostream>
#include <cmath>
#include <ctime>
using namespace std;
#define N 5
class Fraction
{
private:
int chys,
zn;
public:
Fraction()
{
this->chys = 0;
this->zn = 0;
}
Fraction(int chys, int zn)
{
this->chys = chys;
this->zn = zn;
}
Fraction(const Fraction &obj)
{
this->chys = obj.chys;
this->zn = obj.zn;
}
int GetChys() { return chys; }
int GetZn() { return zn; }
void setFraction(int x, int y) { chys = x; zn = y; }
int CommonZn(Fraction *A, Fraction *B)
{
int q = 0;
int i = 0;
for (i = 1; q != 1; i++)
{
if ((i % A->zn == 0) && (i % B->zn == 0))
{
q = 1;
A->chys *= (i / A->zn);
B->chys *= (i / B->zn);
}
}
A->zn = i - 1;
B->zn = i - 1;
this->zn = i - 1;
return i - 1;
}
int skor(int *chys, int *zn)
{
int i = 0, b = 0;
if (*chys > *zn) { b = *chys; }
else (b = *zn);
for (i = b; i > 0; i--)
{
if (*chys%i == 0 && *zn%i == 0)
{
*chys = *chys / i;
*zn = *zn / i;
return 0;
}
}
}
void comperizon(Fraction *A, Fraction *B)
{
if (A->chys > B->chys) { cout << endl; cout << "Перший дріб більший за другий" << endl; }
else if (A->chys == B->chys) { cout << endl; cout << "Дроби однакові" << endl; }
else if (A->chys < B->chys) { cout << endl; cout << "Другий дріб більший за перший" << endl; }
}
void addition(Fraction *A, Fraction *B)
{
this->chys = A->chys + B->chys;
skor(&this->chys, &this->zn);
cout << "\nСума дробів = ";
cout << *this;
cout << endl;
}
void subtraction(Fraction *A, Fraction *B)
{
this->chys = A->chys - B->chys;
skor(&this->chys, &this->zn);
cout << "\nРізниця дробів = ";
cout << *this;
cout << endl;
}
void multiplication(Fraction *A, Fraction *B)
{
this->chys = (A->chys * B->chys);
this->zn = (A->zn * B->zn);
skor(&this->chys, &this->zn);
cout << "\nДобуток дробів = ";
cout << *this;
cout << endl;
}
void division(Fraction *A, Fraction *B)
{
this->chys = (A->chys * B->zn);
this->zn = (A->zn * B->chys);
skor(&this->chys, &this->zn);
cout << "\nЧастка дробів = ";
cout << *this;
cout << endl;
}
Fraction& operator=(Fraction &second)
{
cout << "operator =" << endl;
chys = second.chys; zn = second.zn;
return *this;
}
bool operator == (const Fraction &obj)
{
cout << "operator ==" << endl;
return (this->chys == obj.chys);
}
bool operator >= (const Fraction &obj)
{
cout << "operator >=" << endl;
return (this->chys >= obj.chys);
}
bool operator <= (const Fraction &obj)
{
cout << "operator <=" << endl;
return (this->chys <= obj.chys);
}
friend Fraction& operator +(const Fraction &first, const Fraction &second)
{
cout << "operator +" << endl;
return *new Fraction( first.chys + second.chys, first.zn );
}
friend ostream &operator << (ostream &out, const Fraction &fr);
~Fraction() { }
};
ostream &operator << (ostream &out, const Fraction &fr)
{
out << fr.chys << "/" << fr.zn;
return out;
}
int main()
{
system("chcp 1251");
system("color f0");
system("cls");
int chys1, zn1, chys2, zn2;
cout << "Введіть перший дріб = ";
cin >> chys1;
cin.ignore(1);
cin >> zn1;
cout << endl;
cout << "Введіть другий дріб = ";
cin >> chys2;
cin.ignore(1);
cin >> zn2;
cout << endl;
Fraction *A = new Fraction(chys1, zn1);
Fraction *B = new Fraction(chys2, zn2);
Fraction *C = new Fraction(*A);
C->CommonZn(A, B);
C->comperizon(A, B);
C->addition(A, B);
C->subtraction(A, B);
C->multiplication(A, B);
C->division(A, B);
cout << endl;
*C = *A + *B;
cout << *C;
cout << endl;
getchar();
getchar();
return 0;
}
out << fr.GetChys << "/" << fr.GetZn;
Вы забыли указать, что GetChys
и GetZn
- функции
out << fr.GetChys() << "/" << fr.GetZn();
Далее в вашем коде:
friend Fraction& operator +(const Fraction &first, const Fraction &second)
{
cout << "operator +" << endl;
return *new Fraction( first.chys + second.chys, first.zn );
}
Так же вы возвращаете и принимаете ссылки на объекты, если вы хотите получать и передавать их по значению, необходимо реализовать конструктор копирования.
Оборудование для ресторана: новинки профессиональной кухонной техники
Частный дом престарелых в Киеве: комфорт, забота и профессиональный уход