Essays.club - Получите бесплатные рефераты, курсовые работы и научные статьи
Поиск

Використання класів

Автор:   •  Октябрь 13, 2022  •  Лабораторная работа  •  1,085 Слов (5 Страниц)  •  116 Просмотры

Страница 1 из 5

Лабораторна робота №1

Використання класів.

 Теоретичні відомості: Розглянемо клас “раціональний дріб” Rational. Даний клас повинен відповідати наступним вимогам.

Стан класу: два поля типу “ціле”, з іменами num (від numerator ['нью: мерейте] чисельник) і den (від denominator [ді'номенейте] – знаменник). Обмежуємося діапазоном представлення в стандартних типах .

Додаткові вимоги: знаменник не повинен бути рівний нулю, ні за яких умов; знаменник завжди додатній, знак дробу визначається знаком чисельника; поля класу не повинні бути доступні ззовні класу непередбачуваними класом способами.

Методи класу: традиційні арифметичні операції (додавання, множення тощо), введення/виведення; крім того, будуть потрібні допоміжні операції для виконання арифметичних операцій - типу спрощення дробу і т.п.

[pic 1]

Код програми:

#include <iostream>

#include <iomanip>

#include <Windows.h>

using namespace std;

class Rational {

private:

        int num;

        int denom;

public:

        int* reducedForm(int num, int denom);

        Rational(int = 0, int = 0);//Конструктор з двома параметрами

        Rational(); //Порожній конструктор

        Rational(int num);//з одним параметром

        Rational add(const Rational&);// результат = ("this"+ arg)

        Rational sub(const Rational&); //результат = ("this" -  arg)

        Rational mult(const Rational&); // результат = ("this" *  arg)

        Rational divide(const Rational&); //результат  ("this" /  arg)

        void print();

        void printFloat();

};

int* Rational::reducedForm(int num, int denom) {

        // the GCD for a and b

        int a = num, b = denom, c;

        int* ans = new int[2];       //array created on heap

        while (a != 0) {

                c = a; a = b % a;  b = c;

        }

        //скорочення

        ans[0] = num / b;    //скорочення numerator

        ans[1] = denom / b;  //скорочення denominator

        return ans;

}

Rational::Rational(int anum, int adenom) {

        int* reduced = reducedForm(anum, adenom);

        num = reduced[0];

        denom = reduced[1];

        delete reduced;         //delete the array that was on the heap (garbage collect)

}

Rational Rational::add(const Rational& r2)// result = (this + arg)

{

        int numerator = (num * r2.denom) + (r2.num * denom);

        int denominator = denom * r2.denom;

        Rational sum(numerator, denominator);

        return sum;

}

Rational Rational::sub(const Rational& r2) //result = (this - arg)

{

        int numerator = (num * r2.denom) - (r2.num * denom);

        int denominator = denom * r2.denom;

        Rational sum(numerator, denominator);

        return sum;

}

Rational Rational::mult(const Rational& r2) //result = (this * arg)

...

Скачать:   txt (5.7 Kb)   pdf (160.7 Kb)   docx (77 Kb)  
Продолжить читать еще 4 страниц(ы) »
Доступно только на Essays.club