
- •Void print();
- •Include.H
- •Void Point::set_Point_x( double _x)
- •Void Point::set_Point_y( double _y)
- •Void print ();
- •Void Cube::Draw()
- •Void Cube::square(double r)
- •Int main()
- •Include "targetver.H"
- •If(!p1.InBounds() || !p2.InBounds())
- •If(!temp.UperLeftCorner.InBounds() || !temp.LowerRightConer.InBounds())
- •If(!temp.UperLeftCorner.InBounds() || !temp.LowerRightConer.InBounds())
- •If(!temp.LowerRightConer.InBounds())
- •If(!p2.InBounds())
- •If(!temp.LowerRightConer.InBounds())
Вариант 10
7.1.1. Класс «Трапеция». Создать 2 объекта класса с боковыми сторонами b и основанием a. Угол м5жду основанием и боковой стороной равен 30 0. Левый нижний угол трапеции расположен в начале координат. Основание расположено на оси Ox. Вычислить площадь и периметр исходного объекта-трапеции. Создать новый объект класса, сметив нижние вершины исходного объекта на значение dy вверх. Параллельность основания оси абсцисс и высота сохраняются. Sтрапеции = h * (a + b) / 2.
Trapez.h
#ifndef TRAPEZ_H
#define TRAPEZ_H 1
#include "stdafx.h"
class trapez
{
private:
Point O;
double a,b, ugol; // описание угла
char *name; //наименование объекта: object 1, object 2
char *objID;
public:
static int count;
trapez (); //конструктор по умолчанию
trapez (double _x, double _y, double _ugol, double _a, double _b); //конструктор с параметрами
trapez (Point _O, double _ugol, double _a, double _b);
trapez (const trapez &_R); //конструктор копирования
trapez (const char*); //конструктор
~trapez (); //деструктор
void name_object(const char *ident); //деструктор
double get_h ();
double get_c ();
double get_plosh_s ();
double get_perim_p ();
void print ();
};
#endif
Trapez.cpp
#include "stdafx.h"
trapez::trapez(): O (), ugol (0.5), a (2), b (4)
{
}
int trapez::count=0;
trapez::trapez( double _x, double _y, double _ugol, double _a, double _b): O (_x,_y), ugol (_ugol), a (_a), b (_b)
{
name_object("Trapez");
cout << "constructor_par Point is working "<< endl;
}
trapez::trapez( Point _O, double _ugol, double _a, double _b): O (_O), ugol (_ugol), a (_a), b (_b)
{
name_object("Trapez");
cout << "constructor_par Point is working "<< endl;
}
trapez::trapez(const trapez &R): O (R.O), ugol ( R.ugol), a (R.a), b (R.b)
{
name_object("Trapez");
cout << "constructor_copy Point is working " << endl;
}
trapez::~trapez()
{
cout << "destructor trapez is working " << endl;
}
double trapez::get_h ()
{
double h;
h = b*0.5;
return h;
}
double trapez::get_c ()
{
double m,c;
m = sqrt( b*b - (get_h()* get_h() ));
c = a-2*m;
return c;
}
double trapez::get_plosh_s ()
{
double s;
s = ( ( a+get_c())*get_h())/2;
return s;
}
double trapez::get_perim_p ()
{
double p;
p = a+b*b+get_c();
return p;
}
void trapez:: print ()
{
cout << a << " "<< b <<" " << ugol <<" "<< "(" << O. get_Point_x () << "," << O. get_Point_y () << ")" ;
}
void trapez::name_object(const char *ident)
{
objID = new char[strlen(ident)+1]; //выделение памяти под objID
strcpy(objID, ident); //заполнение строки objID
count++; //увеличение значения счетчика объектов
char buf[20]; //вспомогательный буфер для заполнения поля name
sprintf(buf, "object %d", count); //сохранение в buf строки “object N”
name = new char[strlen(buf)+1]; //выделение памяти под name
strcpy (name, buf);
cout << "Constructor for: " << objID << " " << name << endl;
}
Stdafx.h
#pragma once
#include "targetver.h"
#include <iostream>
#include <stdio.h>
#include <tchar.h>
#include <fstream>
#include <iomanip>
#include <conio.h>
#include <ctime>
#include <istream>
#include <cmath>
#include "point.h"
#include "trapez.h"
#define M_PI
using namespace std;
stdafx.cpp
// stdafx.cpp: исходный файл, содержащий только стандартные включаемые модули
// 7,,1.pch будет предкомпилированным заголовком
// stdafx.obj будет содержать предварительно откомпилированные сведения о типе
#include "stdafx.h"
// TODO: Установите ссылки на любые требующиеся дополнительные заголовки в файле STDAFX.H
// , а не в данном файле
Point.h
#ifndef POINT_H
#define POINT_H 1
class Point
{
private:
double x;
double y;
public:
Point(); //конструктор по умолчанию
Point(const double _x, const double _y); //конструктор с параметрами
Point(const Point& _P); //конструктор копирования
~Point();
void set_Point_x(const double _x); // вводит координату х
void set_Point_y(const double _y); // вводит координату у
double get_Point_x() const; // выводит координату х
double get_Point_y() const;
};
#endif
Point.cpp
#include "stdafx.h"
Point::Point()
{
x=1.0*rand()/RAND_MAX;
y=1.0*rand()/RAND_MAX;
cout << "constructor_um Point is working " << x << " " << y << endl;
}
Point::Point(const double _x, const double _y)
{
x=_x;
y=_y;
cout << "constructor_par Point is working " << x << " " << y << endl;
}
Point::Point(const Point& P)
{
x=P.x;
y=P.y;
cout << "constructor_copy Point is working " << x << " " << y << endl;
}
Point::~Point()
{
cout << "destructor Point is working " << endl;
}
void Point::set_Point_x(const double _x)
{
x=_x;
return;
}
void Point::set_Point_y(const double _y)
{
y=_y;
return;
}
double Point::get_Point_x() const
{
return x;
}
double Point::get_Point_y() const
{
return y;
}
Вариант 12
7.1.1
Класс «Четырехугольник». Одна из сторон исходного объекта класса лежит на оси абсцисс. Создать 3 новых объекта, каждый из которых является зеркальным отражением исходного объекта относительно оси ординат. Вычислить периметр этого объекта-четырехуголь-ника.
Ugolnik.h
#include "point.h"
#ifndef ugolnik_H
#define ugolnik_H 1
class ugolnik
{
private:
Point x1,x2,x3,x4;
char *name;
char *objID;
public:
static int count;
ugolnik();
ugolnik(const double x_1_x,const double x_1_y,const double x_2_x,const double x_2_y,const double x_3_x,const double x_3_y,const double x_4_x,const double x_4_y,char *S);
ugolnik(const Point &P1,const Point &P2,const Point &P3,const Point &P4,char *S);
~ugolnik(void);
void zerkalo(const ugolnik &_P);
void schet();
void name_object(const char *ident);
void starline();
Void print();
void ugolnik::zerkalo2(ugolnik &D);
};
#endif
7.1.1.cpp
#include "stdafx.h"
#include "point.h"
int main()
{
{ ugolnik X;
double t1,t2, t3 ,t4 ,t5 ,t6, t7 ,t8;
cout<<"vvedite tochki"<<endl;
cin>>t1>>t2>>t3>>t4>>t5>>t6>>t7>>t8;
ugolnik Y(t1 ,t2 ,t3 ,t4 ,t5 ,t6 ,t7 ,t8,"ugolnik A");
Point P1(t1,t2);
Point P2(t3,t4);
Point P3(t5,t6);
Point P4(t7,t8);
ugolnik Z(P1,P2,P3,P4,"ugolnik B");
X.zerkalo(Y);
X.schet();
X.zerkalo2(Y);
X.print();
Y.print();
}
_getch();
return 0;
}
void ugolnik::zerkalo2(ugolnik &D)
{
D.x1.set_Point_x(-x1.get_Point_x());
D.x1.set_Point_y(-x1.get_Point_y());
D.x2.set_Point_x(-x2.get_Point_x());
D.x2.set_Point_y(-x2.get_Point_y());
D.x3.set_Point_x(-x3.get_Point_x());
D.x3.set_Point_y(-x3.get_Point_y());
D.x4.set_Point_x(-x4.get_Point_x());
D.x4.set_Point_y(-x4.get_Point_y());
}
Ugolnik.cpp
#include "StdAfx.h"
#include <iostream>
#include <conio.h>
int ugolnik::count=0;
ugolnik::ugolnik():x1(),x2(),x3(),x4()
{
objID = new char[21]; //выделение памяти под objID
strcpy(objID, "ugolnik"); //заполнение строки objID
count++; //увеличение значения счетчика объектов
char buf[20];
//вспомогательный буфер для заполнения поля name
sprintf(buf, "object %d", count); //сохранение в buf строки “object N”
name = new char[strlen(buf)+1]; //выделение памяти под name
strcpy (name, buf);
}
ugolnik::ugolnik(const double x_1_x,const double x_1_y,const double x_2_x,const double x_2_y,const double x_3_x,const double x_3_y,const double x_4_x,const double x_4_y,char *S):
x1(x_1_x,x_1_y),x2(x_2_x,x_2_y),x3(x_3_x,x_3_y),x4(x_4_x,x_4_y)
{
name_object(S);
}
ugolnik::ugolnik(const Point &P1,const Point &P2,const Point &P3,const Point &P4,char *S):x1(P1),x2(P2),x3(P3),x4(P4)
{
name_object(S);
}
ugolnik::~ugolnik(void)
{}
void ugolnik::name_object(const char *ident)
{
objID = new char[strlen(ident)+1]; //выделение памяти под objID
strcpy(objID, ident); //заполнение строки objID
count++; //увеличение значения счетчика объектов
char buf[20];
//вспомогательный буфер для заполнения поля name
sprintf(buf, "object %d", count); //сохранение в buf строки “object N”
name = new char[strlen(buf)+1]; //выделение памяти под name
strcpy (name, buf);
}
void ugolnik::zerkalo(const ugolnik &P) // zerkalo(X,Y) :void zerkalo(const ugolnik &D,const ugolnik &P)
{ x1.x=-P.x1.x;
x1.y=-P.x1.y;// D.set_x1(P.get_x1)
x2.x=-P.x2.x;
x2.y=-P.x2.y;
x3.x=-P.x3.x;
x3.y=-P.x3.y;
x4.x=-P.x4.x;
x4.y=-P.x4.y;
}
void ugolnik::schet()
{double a,b,c,d,Per;
a=abs(x1.x-x2.x);
b=sqrt((x1.x-x2.x)*(x1.x-x2.x)+(x2.y*x2.y));
c=sqrt((x2.x-x3.x)*(x2.x-x3.x)+(x3.y-x2.y)*(x3.y-x2.y));
d=sqrt((x3.y-x4.y)*(x3.y-x4.y)+(x4.x-x3.x)*(x4.x-x3.x));
Per=a+b+c+d;
cout<<Per<<endl;
}
void ugolnik::print()
{
cout << x1.get_Point_x()<< x1.get_Point_y() << " " << x2.get_Point_x() << x2.get_Point_y() << " " << x3.get_Point_x() << x3.get_Point_y() << " " << x4.get_Point_x()<< x4.get_Point_y() << endl;
}
Stdafx.h
/ stdafx.h
#pragma once
#include "targetver.h"
#include <stdio.h>
#include <tchar.h>
#include <iostream>
#include <time.h>
#include <conio.h>
#include <iomanip>
#include <math.h>
#include "point.h"
#include "ugolnik.h"
using namespace std;
// TODO
Point.h
#ifndef POINT_H
#define POINT_H 1
class Point
{
private:
double x;
double y;
public:
Point(); //конструктор по умолчанию
Point(const double nx,const double ny);
Point(const Point &_P); //инициализация координат случайными значениями от 0 до 1
~Point();
void set_Point_x(const double _x);
void set_Point_y(const double _y);
double get_Point_x() const;
double get_Point_y() const;
friend class ugolnik;
void show_Point() const; //метод закрыт для доступа извне
};
#endif
Point.cpp
#include "stdafx.h"
Point::Point():x(0),y(0)
{}
Point::Point(const double nx,const double ny):x(nx),y(ny)
{}
Point::Point(const Point &_P):x(_P.x),y(_P.y)
{}
Point::~Point()
{
cout << "destructor Point is working " << endl;
}
void Point::set_Point_x(const double _x)
{
x=_x;
return;
}
void Point::set_Point_y(const double _y)
{
y=_y;
return;
}
double Point::get_Point_x() const
{
return x;
}
double Point::get_Point_y() const
{
return y;
}
void Point::show_Point() const
{
cout <<"("<< x << " , " << y <<")"<< endl;
}
Вариант 3 или 9
7.1.2. Создать класс Money для работы с денежными суммами. Число должно быть представлено двумя полями: типа long для рублей и типа unsigned char – для копеек. Дробная часть (копейки) при выводе на экран должна быть отделена от целой части запятой. Реализовать сложение, вычитание, деление сумм, деление суммы на дробное число, умножение на дробное число и операции сравнения.
7.1.2.сpp
// 7.1.2.cpp: определяет точку входа для консольного приложения.
//
#include "stdafx.h"
int main()
{
Money X,K;
cout << "Enter kov-vo banknot(5000,1000,500,100):";
cin >> X;
cout << X << endl;
Money Y(5,7,5,7,5,7,1,2,3,4);
Money Z(Y);
cout << Y << endl;
K=Y+X;
cout << K << endl;
X=Y;
cout << X << endl;
if (X>K) cout<< "X>K" << endl;
else cout << "X<K"<< endl;
K=X=Y;
cout << K << X << Y << endl;
X=K/2.5;
cout << X << endl;
X=X*2.5;
cout << X << endl;
_getch();
return 0;
}
Money.cpp
#include "stdafx.h"
Money::Money():ft(0),t(0),fs(0),s(0),fd(0),d(0),kop(0),fkop(0),dkop(0),fdkop(0)
{}
Money::Money(const int na,const int nb,const int nc,const int nd,const int n,const int k,const int nx,const int ny,const int nz,const int nk)
{
ft=na;fs=nc;kop=nk;fkop=nz;dkop=ny;fdkop=nx;
t=nb;s=nd;fd=n;d=k;
}
Money::Money(const Money &P)
{
ft=P.ft;fs=P.fs;fdkop=P.fdkop;dkop=P.dkop;fkop=P.fkop;kop=P.kop;
t=P.t;s=P.s;fd=P.fd;d=P.d;
}
Money::~Money()
{
}
Money Money::operator+(Money& d1) const
{
int r=s+d1.s;// суммируем поля данных
int e=fs+d1.fs;
int w=t+d1.t;
int q=ft+d1.ft;
int z=fdkop+d1.fdkop;
int x=dkop+d1.dkop;
int y=fkop+d1.fkop;
int k=kop+d1.kop;
int ny=fd+d1.fd;
int nk=d+d1.d;
return Money (q,w,e,r,ny,nk,z,x,y,k);// возвращаем конструктор
}
Money Money::operator-(Money& d1) const
{
int r=s-d1.s;
int e=fs-d1.fs;
int w=t-d1.t;
int q=ft-d1.ft;
int z=fdkop-d1.fdkop;
int x=dkop-d1.dkop;
int y=fkop-d1.fkop;
int k=kop-d1.kop;
int ny=fd-d1.fd;
int nk=d-d1.d;
return Money (q,w,e,r,ny,nk,z,x,y,k);
}
Money Money::operator/(double d1)
{
double s=schet()/d1;
return razlo(s);
}
Money Money::operator*(double d1)
{
double s=schet()*d1;
return razlo(s);
}
bool Money::operator>(Money &d1)
{
return (schet()>d1.schet()) ? true : false;
}
Money& Money::operator=(Money& a) //перегрузка присваивания
{
ft=a.ft;
t=a.t;
fs=a.fs;
s=a.s;
fdkop=a.fdkop;
fkop=a.fkop;
dkop=a.dkop;
kop=a.kop;
fd=a.fd;
d=a.d;
return *this; //возвращает объект
}
Money Money::razlo(double sk)
{
int b=sk;
ft=0;t=0;fs=0;s=0;fd=0;d=0;fdkop=0;dkop=0;fkop=0;kop=0;
int a;
double dr=sk-b;
int ost=b%5000;
ft=(b-ost)/5000;
if (ost>=1000)
{
a=ost%1000;
t=(ost-a)/1000;
ost=a;
}
if (ost>=500)
{
a=ost%500;
fs=(ost-a)/500;
ost=a;
}
if(ost>=100)
{
a=ost%100;
s=(ost-a)/100;
ost=a;
}
if (ost>=50)
{
a=ost%50;
fd=(ost-a)/50;
ost=a;
}
if(ost>=10)
{
a=ost%10;
d=(ost-a)/10;
ost=a;
}
dr*=100;
int dd=dr;
if(dd>=50)
{
a=dd%50;
fdkop=(dd-a)/50;
dd=a;
}
if(dd>=10)
{
a=dd%10;
dkop=(dd-a)/10;
dd=a;
}
if(dd>=5)
{
a=d%5;
fkop=(dd-a)/5;
dd=a;
}
if(dd>=1)
{
a=d%1;
kop=(dd-a)/1;
dd=a;
}
return Money(ft,t,fs,s,fd,d,fdkop,dkop,fkop,kop);
}
double Money::schet() const
{
double S=5000*ft+1000*t+500*fs+100*s+50*fd+10*d+0.5*fdkop+0.1*dkop+0.05*fkop+0.01*kop;
return S;
}
ostream& operator<< (ostream& os,Money& p)
{
os << p.ft << " " << p.t <<" " << p.fs << " "<< p.s <<" "<< p.fd <<" "<< p.d <<" "<<p.fdkop <<" "<< p.dkop << " "<< p.fkop << " "<< p.kop << " S=" << p.schet()<< endl;
return os;
}
istream& operator>> (istream& is, Money& p)
{
is >>p.ft >> p.t >> p.fs >> p.s >> p.fd >>p.d >> p.fdkop >> p.dkop >> p.fkop >> p.kop;
return is;
}
Money.h
ifndef MONEY_H
#define MONEY_H 1
#include <iostream>
using namespace std;
class Money
{
private:
int ft,t,fs,s,fd,d,kop,fkop,dkop,fdkop;
public:
Money();
Money(const int na,const int nb,const int nc,const int nd,const int n,const int k,const int nx,const int ny,const int nz,const int nk);
Money(const Money&_P);
~Money();
Money operator+(Money& ) const;
Money operator-(Money& ) const;
Money operator/(double);
Money operator*(double);
bool operator>(Money & );
Money& operator=(Money& a);
double schet() const;
Money razlo(double);
friend ostream& operator<< (ostream& os,Money& p);
friend istream& operator>> (istream& is, Money& p);
};
#endif
Stdfax.h
#pragma once
#include "targetver.h"
#include <stdio.h>
#include <tchar.h>
#include <iostream>
#include <conio.h>
#include <stdio.h>
#include <tchar.h>
#include <fstream>
#include <iomanip>
#include <ctime>
using namespace std;
#include "Money.h"
// TODO: Установите здесь ссылки на дополнительные заголовки, требующиеся для программы
Вариант 9
7.1.1 Класс «Квадрат». Создать 2 объекта класса, стороны которого параллельны осям координат. Вычислить площадь и длину диагонали квадрата. Создать новый объект, переместив исходный объект-квадрат таким образом, чтобы одна из сторон совпала с осью OY. Вывести координаты вершин нового объекта.
Quadro.cpp
#include "quadro.h"
#include "point.h"
#include "include.h"
int quadro::count=0 ;
quadro::quadro():t1()
{
srand(time(NULL));
a=rand() % 9 + 1;
while (!prov())
{
t1.rePoint();
t2.rePoint();
}
name_object("Quadro");
}
quadro::quadro(const double r_b_x, const double r_b_y,const double l_b_x, const double l_b_y,const double _a):
t1(r_b_x, r_b_y),t2(l_b_x, l_b_y),a(_a)
{
while (!prov())
{
t1.rePoint();
t2.rePoint();
}
name_object("Quadro");
}
quadro::quadro(const quadro &P): t1(P.t1),t2(P.t2)
{
a=P.a;
while (!prov())
{
t1.rePoint();
t2.rePoint();
}
name_object("Quadro");
}
quadro::~quadro()
{
delete [] objID;
delete [] name;
count--;
cout << "destructor Point is working " << endl;
}
double quadro::set_S()
{
double Sk;
return Sk=a*a;
}
double quadro::set_P()
{
double P;
return P=a*4;
}
void quadro::move_quadro()
{
double n;
n=t1.get_Point_x();
t1.set_Point_x(0);
t2.move_Point(n);
cout << "Move quadro"<<endl;
cout << "verhina 1:";
t1.show_Point();
cout << "\nverhina 2:";
t2.show_Point();
starline();
}
void quadro::size_quadro(bool temp,double i)
{
if (temp)
{
a+=i;
t1.size_plus_point(i);
t2.size_plus_point(i);
}
else
{
a-=i;
t1.size_minus_point(i);
t2.size_minus_point(i);
}
cout << "Size quadro"<<endl;
name_object("Quadro");
}
void quadro::name_object(const char *ident)
{
objID = new char[strlen(ident)+1]; //выделение памяти под objID
strcpy(objID, ident); //заполнение строки objID
count++; //увеличение значения счетчика объектов
char buf[20]; //вспомогательный буфер для заполнения поля name
sprintf(buf, "object %d", count); //сохранение в buf строки “object N”
name = new char[strlen(buf)+1]; //выделение памяти под name
strcpy (name, buf);
cout << "Is working " <<"a="<< a << " " <<"Sk=" << set_S() << " P="<< set_P() << endl;
cout << "verhina 1:" ;
t1.show_Point();
cout << "\nverhina 2:";
t2.show_Point();
cout << objID <<" "<< name << endl;
starline();
}
void quadro::starline() const
{
for(int i=0;i<45;i++)
cout << "*";
cout << endl;
}
bool quadro::prov()
{
if ((t1.Ox-t2.Ox==fabs(a)) && (t1.Oy-t2.Oy==fabs(a)))
return true;
}