Класс Стек Заголовочный файл
X.Print();
Рациональные числа
Заголовочный файл Rat.h
Пример 1. Класс Рациональные числа.
#include "iostream.h"
// Рациональные числа
class Rat{
public:
//Конструкторы
Rat(); //конструктор по умолчанию
Rat(int, int); // инициирующий конструктор
Rat(int); // конструктор-преобразователь
Rat(const Rat&); // копирующий конструктор
//Операции
// Сложение
Rat operator + (const Rat&) const;
Rat operator + (const int&) const;
// дружественная функция перегружает операцию +
friend Rat operator + (const int&, const Rat&);
virtual ~Rat(); // деструктор
// Сравнение <
bool operator < (const Rat&) const;
bool operator < (const int&) const;
// дружественная функция перегружает операцию <
friend bool operator < (const int&, const Rat&);
// Оператор присвоения
Rat operator = (const Rat&);
//Функции доступа к членам
int Numerator() const {return Num;};
int Denominator() const {return Den;};
void Print(){cout << Num << "/"<< Den << endl;}
private:
// Числитель и знаменатель
int Num;
int Den;
int GCD(int, int);
};
// File Rat.cpp: implementation of the Rat class.
#include "stdafx.h"
#include "Rat.h"
#include "math.h"
/ Конструкторы /
Rat::Rat(){} // конструктор по умолчанию
Rat::Rat(int a, int b) // инициирующий конструктор
{
if(b < 0){
b = - b;
a = - a;
}
int D = GCD(abs(a), b);
Num = a/D;
Den = b/D;
}
Rat::Rat(int a) // конструктор-преобразователь
{
Num = a;
Den = 1;
}
Rat::Rat(const Rat& r) // копирующий конструктор
{
Num = r.Num;
Den = r.Den;
}
//------------------------------------------------
int Rat::GCD(int a, int b)
{
while (a!= b)
if (a>b) a -= - b;
else b -= - a;
// cout << a + b << endl;
return a;
}
//-----------------------------------------------------------------
Rat Rat::operator + (const Rat& b) const
{
Rat c(Num*b.Den + b.Num*Den, b.Den*Den);
return c;
}
Rat Rat::operator + (const int& b) const
{
Rat c(Num + b*Den, Den);
return c;
}
// Дружественная функция
Rat operator + (const int& a, const Rat& b)
{
Rat c(a*b.Den +b.Num, b.Den);
return c;
}
//-----------------------------------------
bool Rat::operator < (const Rat& b) const // a/b < c/d
{
return Num*b.Den < Den*b.Num;
}
bool Rat::operator < (const int& b) const // a/b < c
{
return Num < Den*b;
}
bool operator < (const int& a, const Rat& b) // a < c/d
{
return a*b.Den < b.Num;
}
Rat Rat::operator = (const Rat& b)
{
// Поскольку левый операнд имеет тип Rat
// необходимо реализовать a = b;
Num = b.Num;
Den = b.Den;
return *this;
}
// Деструктор
Rat::~Rat(){} // деструктор по умолчанию
//-------------- end of Rat.cpp ------
Приложения
#include "stdafx.h"
#include "stdio.h"
#include "Rat.h"
int main()
{
printf("----------------------------\n");
Rat x(3, -9);
Rat y(2);
y.Print();
Rat a(4, 3);
Rat b(2, 3);
printf("-----------------------------\n");
(a + b).Print();
(a + 1).Print();
(1 + b).Print();
printf("-----------------------------\n");
if (a < b) a.Print(); else b.Print();
if (1 < b) b.Print(); else printf("%d\n", 1);
if (a < 1) a.Print(); else printf("%d\n", 1);
printf("------------------------------\n");
a.Print();
a = b;
a.Print();
Rat c = a + b;
printf(" c = ");
c.Print();
return 0;
}
#include "iostream.h"
{
int Data;
Item* Next;
Item(int a){Data = a; cout << Data << endl; }
};