Ниже тебе будет представлен код, написанный на C++. Опиши работу каждой функции.…
- User
Ниже тебе будет представлен код, написанный на C++. Опиши работу каждой функции. #include
using namespace std;
class Fractions { private: int whole; // целая часть int fraction; // дробная часть
public: Fractions() { // конструктор по умолчанию whole = 0; fraction = 0; }
Fractions(int w, int f) { // конструктор с параметрами whole = w; fraction = f; } Fractions operator+(Fractions& f) { // оператор сложения Fractions result; result.whole = whole + f.whole; result.fraction = fraction + f.fraction; if (result.fraction >= 1000) { // проверка переноса из дробной части result.fraction -= 1000; result.whole++; } return result; } Fractions operator-(Fractions& f) { // оператор вычитания Fractions result; result.whole = whole - f.whole; result.fraction = fraction - f.fraction; if (result.fraction < 0) { // проверка переноса в дробную часть result.fraction += 1000; result.whole--; } return result; } Fractions operator*(Fractions& f) { // оператор умножения Fractions result; result.whole = whole * f.whole + whole * f.fraction / 1000 + f.whole * fraction / 1000; result.fraction = fraction * f.fraction % 1000; return result; } bool operator==(Fractions& f) { // оператор сравнения на равенство return (whole == f.whole && fraction == f.fraction); } bool operator>(Fractions& f) { // оператор сравнения на больше if (whole > f.whole) { return true; } else if (whole == f.whole) { return fraction > f.fraction; } else { return false; } } bool operator<(Fractions& f) { // оператор сравнения на меньше if (whole < f.whole) { return true; } else if (whole == f.whole) { return fraction < f.fraction; } else { return false; } } void print() { // функция вывода на экран cout << whole << "." << fraction << endl; }};
int main() { Fractions f1(1, 500); Fractions f2(2, 750); Fractions f3;
f3 = f1 + f2; f3.print(); f3 = f2 - f1; f3.print(); f3 = f1 * f2; f3.print(); if (f1 == f2) cout << "f1 == f2" << endl; if (f1 > f2) cout << "f1 > f2" << endl; if (f1 < f2) cout << "f1 < f2" << endl; return 0;}
- Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.