Предмет:
Тип роботи:
Звіт з практики
К-сть сторінок:
46
Мова:
Українська
<iostream>
class fraction
{
private:
int num;
int den;
fraction& simplify();
public:
fraction();
fraction(int a, unsigned b = 1);
fraction(const fraction& f);
~fraction();
void show(std::ostream& os) const;
fraction add(const fraction& f) const;
fraction operator+(const fraction& f) const;
fraction operator-(const fraction& f) const;
fraction operator/(const fraction& f) const;
fraction operator*(const fraction& f) const;
fraction operator=(const fraction& f) const;
friend bool operator==(const fraction& a, const fraction& b);
friend bool operator!=(const fraction& a, const fraction& b);
friend bool operator<=(const fraction& a, const fraction& b);
friend bool operator>=(const fraction& a, const fraction& b);
friend bool operator<(const fraction& a, const fraction& b);
friend bool operator>(const fraction& a, const fraction& b);
};
std::ostream& operator<<(std::ostream& os, const fraction& f);
#include "fraction.h"
#include <iostream>
fraction::fraction() : num(0), den(1)
{
num = 0; den = 1;
}
fraction::fraction(int a, unsigned b) : num(a)
{
if (b > 0) den = b;
else
{
den = 1;
std::cerr << "error: wrong denominator\n";
}
simplify();
}
fraction::fraction(const fraction& f)
{
num = f.num; den = f.den;
}
fraction::~fraction()
{
}
void fraction::show(std::ostream& os) const
{
os << num << '/' << den;
}
fraction fraction::add(const fraction& f) const
{
int a = this->num*f.den + this->den*f.num;
int b = this->den * f.den;
return fraction(a,b).simplify();
}
fraction fraction::operator+(const fraction& f) const
{
int a = this->num*f.den + this->den*f.num;
int b = this->den * f.den;
return fraction(a,b).simplify();
}
fraction fraction::operator=(const fraction& f) const
{
return fraction(f);
}
fraction fraction::operator/(const fraction& f) const
{
int a = this->num * f.den;
int b = this->den * f.num;
return fraction(a,b).simplify();
}
fraction fraction::operator-(const fraction& f) const
{
int a = this->num * f.den - this->den * f.num;
int b = this->den * f.den;
return fraction(a,b).simplify();
}
fraction fraction::operator*(const fraction& f) const
{
int a = this->num * f.num;
int b = this->den * f.den;
return fraction(a,b).simplify();
}
fraction& fraction::simplify()
{
int a = this->num;
int b = this->den;
while (a != b)
{
if (a > b) a -= b;
else b -= a;
}
if (a > 1)
{
num /= a;
den /= a;
}
return *this;
}
std::ostream& operator<<(std::ostream& os, const fraction& f)
{
f.show(os);
return os;
}
bool operator==(const fraction& a, const fraction& b)
{
if (a.num==b.num && a.den==b.den) return true;
else return false;
}
bool operator!=(const fraction& a, const fraction& b)
{
return !(a==b);
}
bool operator<(const fraction& a, const fraction& b)
{
float a1=float(a.num)/a.den, b1=float(b.num)/b.den;
return (a1<b1);
}
bool operator>(const fraction& a, const fraction& b)
{
float a1=float(a.num)/a.den, b1=float(b.num)/b.den;
return (a1>b1);
}
bool operator>=(const fraction& a, const fraction& b)
{
return !(a<b);
}
bool operator<=(const fraction& a, const fraction& b)
{
return !(a>b);
}
#include "fraction.h"
#include <iostream>
fraction makeDefaultFraction()
{
return fraction();
}
fraction retCopyFraction(fraction& t)
{
return t;
}
int main()
{
fraction b(8,24);
fraction c(1,2);
fraction d=c;
std::cout<<"b: "<<b<<"\n";
std::cout<<"d: "<<d<<"\n";
std::cout<<"d<b: "<<(d<b)<<"\n";
std::cout<<"d>b: "<<(d>b)<<"\n";
std::cout<<"d==b: "<<(d==b)<<"\n";
std::cout<<"d!=b: "<<(d!=b)<<"\n";
std::cout<<"d<=b: "<<(d<=b)<<"\n";
std::cout<<"d>=b: "<<(d>=b)<<"\n";
std::cout<<"d+b: "<<(d+b)<<"\n";
std::cout<<"d-b: "<<(d-b)<<"\n";
std::cout<<"d*b: "<<(d*b)<<"\n";
std::cout<<"d/b: "<<(d/b)<<"\n";
std::cin.get();
std::cin.get();
return 0;
}
13.Програма, яка за допомогою канви малює чотирикутник та визначає його тип, площу, периметр.
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, Menus, Math, ComCtrls ;
type
TForm1 = class(TForm)
MainMenu1: TMainMenu;
N1: TMenuItem;
StatusBar1: TStatusBar;
N2: TMenuItem;
N3: TMenuItem;
procedure FormCreate(Sender: TObject);
procedure FormMouseDown(Sender: TObject; Button: TMouseButton;