Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
proga.docx
Скачиваний:
0
Добавлен:
01.05.2025
Размер:
72.94 Кб
Скачать

If(!temp.LowerRightConer.InBounds())

{

temp = Windows();

cout<<"\nError: Resulted Window is out of consol\'s bound."<<endl;

cout<<"The Operation * is canceled.\n\n";

}

return temp;

}

Windows Windows::operator*= (const double& K)

{

if(K < 0)

{

cout<<"\nError: The negative values of stretch coefficient is not possible."<<endl;

cout<<"The Operation * is canceled.\n\n";

return *this;

}

int L = this->Length * K;

int W = this->Width * K;

Point P2 = this->LowerRightConer + Point(L, W);

If(!p2.InBounds())

{

cout<<"\nError: Resulted Window is out of consol\'s bound."<<endl;

cout<<"The Operation * is canceled.\n\n";

}

else

{

this->Length = L;

this->Width = W;

this->LowerRightConer = P2;

}

return *this;

}

Windows operator* (const double& K, const Windows& W)

{

Windows temp = W;

if(K < 0)

{

temp = Windows();

cout<<"\nError: The negative values of stretch coefficient is not possible."<<endl;

cout<<"The Operation * is canceled.\n\n";

return temp;

}

temp.Length *= K;

temp.Width *= K;

temp.LowerRightConer = temp.UperLeftCorner + Point(temp.Length, temp.Width);

If(!temp.LowerRightConer.InBounds())

{

temp = Windows();

cout<<"\nError: Resulted Window is out of consol\'s bound."<<endl;

cout<<"The Operation * is canceled.\n\n";

}

return temp;

}

ostream& operator<< (ostream& s, const Windows& W)

{

for(int i = 0; i < 80; i++) { cout<<'-';}

cout<<endl;

s<<W.Title<<endl;

int storоna=pow((W.Length*W.Length+W.Width*W.Width),0.5);

int simb=storоna*2+1;

for(int j=0;j < simb;j++)

{

for(int i=0;i < simb;i++)

if(abs(i-storоna) == (j>storоna ? simb-j-1 : j))

cout << "*";

else

cout << " ";

cout << "\n";

}

cout<<endl;

return s;

}

istream& operator>> (istream& s, Windows& W)

{

int x, y;

while(true)

{

cout<<"\n\nCoordinates of upper left corner: ";

cout << "\nx = "; cin >> x;

cout << "y = "; cin >> y;

if(x < 1 || x > 79 || y < 1 || y > 25)

cout<<"\nIncorrect coordinates. Try again."<<endl;

else

break;

}

Point P1(x, y);

while(true)

{

cout<<"\nCoordinates of lower right corner: ";

cout << "\nx = "; cin >> x;

cout << "y = "; cin >> y;

if(x < 1 || x > 79 || x < P1.GetX() || y < 1 || y > 24 || y < P1.GetY())

cout<<"\nIncorrect coordinates. Try again."<<endl;

else

break;

}

Point P2(x, y);

int ch;

while(true)

{

cout<<"\n\nChoose colors from the list:";

cout<<"\n1 - Red";

cout<<"\n2 - Orange";

cout<<"\n3 - Yellow";

cout<<"\n4 - Green";

cout<<"\n5 - Blue";

cout<<"\n6 - DarkBlue";

cout<<"\n7 - Purple";

cout<<"\nYou choice: "; cin>>ch;

if(ch < 1 || ch > 7)

cout<<"\nIncorrect colors. Try again."<<endl;

else

break;

}

Windows::Colors color;

switch(ch)

{

case 1:

color = Windows::Red;

break;

case 2:

color = Windows::Orange;

break;

case 3:

color = Windows::Yellow;

break;

case 4:

color = Windows::Green;

break;

case 5:

color = Windows::Blue;

break;

case 6:

color = Windows::DarkBlue;

break;

case 7:

color = Windows::Purple;

break;

}

W.UperLeftCorner = P1;

W.LowerRightConer = P2;

W.Length = P2.GetX() - P1.GetX();

W.Width = P2.GetY() - P1.GetY();

W.SetColor(color);

return s;

вариант 6.

7.1.1

Вариант 6.

Класс «Правильный многоугольник». Создать 2 объекта класса, вписанных в окружность радиуса R. Вычислить периметр объекта-окружности и объекта-многоуголь-ника. Сторона правильного многоугольника an = 2* R* sin (π/n).

7.1.1

Class.cpp

#include "stdafx.h"

unsigned Hex::number=0;

Hex::Hex()

{

point.x=0.0;

point.y=0.0;

n=6;

r=1.0;

number++;

}

Hex::Hex(unsigned _n, double _r, double _x, double _y)

{

n=_n;

r=_r;

point.x=_x;

point.y=_y;

number++;

}

Hex::Hex(const Hex & hex)

{

n=hex.n;

r=hex.r;

point.x=hex.point.x;

point.y=hex.point.y;

number++;

}

Hex::~Hex()

{

number--;

}

//----------------------------------------------------------------

void Hex::show()

{

cout << "Drawing " << n << "-side figure, radius=" << r << "; P: " << perimeter()<<"; S:"<< square() <<"; Size of side:"<< side()<<"; Coordinates of centre x & y:"<< point.x <<" & "<< point.y << "." << endl;

}

void Hex::shift(double dx, double dy)

{

point.x+=dx;

point.y+=dy;

}

void Hex::size(double newradius)

{

r=newradius;

}

void Hex::sides(unsigned new_number_of_sides)

{

n=new_number_of_sides;

}

//----------------------------------------------------------------

double Hex::side()

{

return 2*r*sin(3.1415926535897932384626433832795/n);

}

double Hex::perimeter()

{

return n*side();

}

double Hex::square()

{

double s=(2*r*sin(3.1415926535897932384626433832795/n));

return s/2*sqrtl(r*r-s*s/4);

}

Class.h

class Hex

{

static unsigned number;

unsigned n;

double r;

struct

{

double x,y;

} point;

public:

Hex();

Hex(unsigned N, double R, double X, double Y);

Hex(const Hex &);

~Hex();

void show();

void shift(double, double);

void size(double);

void sides(unsigned);

double side();

double perimeter();

double square();

};

Main.cpp

#include "stdafx.h"

void main()

{

Hex u;

Hex z(5,3.5,2.0, 2.0);

Hex s(u);

s.sides(3);

u.size(5);

u.show();

cout<<endl;

s.show();

cout<<endl;

z.show();

_getch();

}

Stdfx.h

#include <iostream>

#include <conio.h>

using namespace std;

#include "class.h"

Вариант 4

7.1.2

Создать класс Angle для работы с углами на плоскости, задаваемыми величиной в градусах и минутах. Обязательно должны быть реализованы: перевод в радианы, приведение к диапазону 0 – т360, увеличение и уменьшение угла на заданную величину, получение синуса, сравнение углов.

7.1.2.сpp

// 7.2.cpp: определяет точку входа для консольного приложения.

//

#include "stdafx.h"

int main()

{ int n;

Angle X,K;

Angle Y(30,30);

cin >> X;

cout << X;

cout<<"\nX in radians:\t"<<X.getRadians();

cout<<"\n\nX beyond range <0 - 360>\n";

cout << X << endl;

X.intoRange();

cout<<"\nX within range:"<< X <<"\n";

cout<< "vvedite n:";

cin >> n;

cout<<"\nMinus values from X\n";

X-=n;

cout<<"X: Degrees: "<< X <<"\n";

X+=n;

cout<<"X: Degrees: "<< X <<"\n";

cout<<"\nSinus for angle X\n";

cout<<"X:\t"<<X.sinA()<<"\n";

if (X>Y)

cout << "X>Y\n";

else cout << "Y>X\n";

K=X-Y;

cout<< K << endl;

K=X+Y;

cout<< K << endl;

_getch();

return 0;

}

Angle.cpp

#include "StdAfx.h"

#include "Angle.h"

Angle::Angle():gr(0),min(0)

{}

Angle::Angle(const int _gr, const int _min):gr(_gr),min(_min)

{}

Angle::Angle(const Angle &P):gr(P.gr),min(P.min)

{}

Angle::~Angle()

{}

ostream& operator<< ( ostream& os,Angle& P)

{

os<< P.gr << "." << P.min << endl;

return os;

}

istream& operator>> ( istream& is,Angle& P)

{

is>> P.gr >> P.min;

return is;

}

double Angle::getRadians()

{

return (gr+min/60.)*M_PI/180.;

}

void Angle::intoRange()

{

if(min<0)

{ gr--;

min += 60;

}

if(gr>0)gr = gr%360;

else gr = gr%360+360;

}

void Angle::removeRoughs()

{

min += gr*60;

gr = min/60;

min = min%60;

}

void Angle::operator+=(const int n)

{

gr +=n;

min +=n;

this->removeRoughs();

}

void Angle::operator-=(const int n)

{

gr -=n;

min -=n;

this->removeRoughs();

}

double Angle::sinA()

{

return sin(this->getRadians());

}

bool Angle::operator>(Angle & d1)

{

int t=(gr*60)+min;

int f=(d1.gr*60)+d1.min;

return (t>f) ? true:false;

}

Angle Angle::operator-(Angle& d1)

{

int t=gr-d1.gr;

int f=min-d1.min;

f+=t*60;

t=f/60;

f=f%60;

return Angle(t,f);

}

Angle Angle::operator+(Angle& d1)

{

int t=gr+d1.gr;

int f=min+d1.min;

f+=t*60;

t=f/60;

f=f%60;

return Angle(t,f);

}

Angle.h

#pragma once

#include <iostream>

using namespace std;

class Angle

{

private:

int gr;

int min;

public:

Angle();

Angle(const int _gr, const int _min);

Angle(const Angle&_P);

~Angle();

friend ostream& operator<< ( ostream& os,Angle& P);

friend istream& operator>> ( istream& is,Angle& P);

bool operator>(Angle &);

double getRadians();

void intoRange();

void removeRoughs();

void operator+=(const int n);

void operator-=(const int n);

Angle Angle::operator-(Angle& d1);

Angle Angle::operator+(Angle& d1);

double sinA();

};

Stdafx.h

// stdafx.h: включаемый файл для стандартных системных включаемых файлов

// или включаемых файлов для конкретного проекта, которые часто используются, но

// не часто изменяются

//

#pragma once

#include "targetver.h"

#include <stdio.h>

#include <tchar.h>

#include <iostream>

#include <stdio.h>

#include <tchar.h>

#include <fstream>

#include <iomanip>

#include <conio.h>

#include <ctime>

#include <istream>

#include <cmath>

#include "Angle.h"

using namespace std;

#define M_PI 3.14159265358979323846

Stdafx.cpp

// stdafx.cpp: исходный файл, содержащий только стандартные включаемые модули

// 7.2.pch будет предкомпилированным заголовком

// stdafx.obj будет содержать предварительно откомпилированные сведения о типе

#include "stdafx.h"

// TODO: Установите ссылки на любые требующиеся дополнительные заголовки в файле STDAFX.H

// , а не в данном файле

Вариант 6

7.1.2

Создать класс Time для работы со временем в формате «час:минута:секунда». Класс должен включать в себя не менее четырех функций инициализации: числами, строкой (например, «23:59:59»), секундами и временем. Обязательными операциями являются: вычисление разницы между двумя моментами времени в секундах, сложение времени и заданного количества секунд, вычитание из времени заданного количества секунд, сравнение моментов времени, перевод в секунды, перевод в минуты (с округлением до целой минуты).

7.1.2

Time.cpp

#include "stdafx.h"

Time::Time()

{

h=0;

m=0;

s=0;

}

Time::Time(int _h, int _m, int _s)

{

h=_h;

m=_m;

s=_s;

}

Time::Time(const Time & t)

{

h=t.h;

m=t.m;

s=t.s;

}

Time::~Time()

{

}

void Time::proverka()

{

if (s>59)

{

m+=s/60;

s-=60*(s/60);

}

if (m>59)

{

h+=m/60;

m-=60*(m/60);

}

if (h>23)

h-=24*(h/24);

}

//--------------------------------------------

void Time::show() const

{

cout<<"Hours: "<< h <<"; Minutes: "<< m <<"; Seconds: "<< s <<endl;

}

void Time::numbers()

{

cout<<"Input hours, minutes and seconds:"<<endl;

cin>>h>>m>>s;

proverka();

}

void Time::clock()

{

char buf[3];

cout<<"Input time:"<<endl;

cin.getline(buf, 3, ':');

h=(buf[0]-48)*10 + buf[1]-48;

cin.getline(buf, 3, ':');

m=(buf[0]-48)*10 + buf[1]-48;

cin.getline(buf, 3, ':');

s=(buf[0]-48)*10 + buf[1]-48;

cin.clear();

proverka();

}

void Time::secundus()

{

int sec;

cout<<"Input seconds:"<<endl;

cin>>sec;

h=sec/3600;

m=sec/60%60;

s=sec%60;

proverka();

}

void Time::perevods()

{

int so;

so=s+m*60+h*3600;

}

void Time::perevodm()

{

int mo;

if (s>=30)

mo=m+h*60+1;

if (s<=30)

mo=m+h*60;

}

//--------------------------------------------

Time Time::operator + (const Time & t) const

{

Time temp;

temp.s = t.s + s;

temp.m = t.m + m;

temp.h = t.h + h;

temp.proverka();

return temp;

}

Time Time::operator - (const Time & t) const

{

Time temp;

temp.s = s - t.s;

temp.m = m - t.m;

temp.h = h - t.h;

temp.proverka();

return temp;

}

Time Time::operator += (const Time & t)

{

s += t.s;

m += t.m;

h += t.h;

proverka();

return *this;

}

Time Time::operator -= (const Time & t)

{

s -= t.s;

m -= t.m;

h -= t.h;

proverka();

return *this;

}

Time Time::operator = (const Time & t)

{

s=t.s;

m=t.m;

h=t.h;

proverka();

return *this;

}

//------------------------------------------------

bool Time::operator == (const Time & t) const

{

if (s==t.s && m==t.m && h==t.h)

return 1;

else

return 0;

}

bool Time::operator > (const Time & t) const

{

if ((h>t.h) || (h==t.h && m>t.m) || (h==t.h && m==t.m && s>t.s))

return 1;

else

return 0;

}

bool Time::operator < (const Time & t) const

{

if ((h<t.h) || (h==t.h && m<t.m) || (h==t.h && m==t.m && s<t.s))

return 1;

else

return 0;

}

Time.h

lass Time

{

int h, m, s;

void proverka();

public:

Time();

Time(int H, int M, int S);

Time(const Time &);

~Time();

void clock();

void numbers();

void secundus();

void perevods();

void perevodm();

void show() const;

int getsec() const;

int getmin() const;

Time operator + (const Time &) const;

Time operator - (const Time &) const;

Time operator += (const Time &);

Time operator -= (const Time &);

Time operator = (const Time &);

bool operator == (const Time &) const;

bool operator > (const Time &) const;

bool operator < (const Time &) const;

};

Main

include "stdafx.h"

void main()

{

Time x;

Time y;

Time z;

x.clock();

y.numbers();

z.secundus();

x.show();

cout<<endl;

y.show();

cout<<endl;

z.show();

cout<<endl;

z=x-y;

z.show();

cout<<endl;

cout<<(x==y);

cout<<endl;

system("PAUSE");

}

Stdfx.h

#include <iostream>

#include <conio.h>

using namespace std;

#include "Time.h"

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]