Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:

ООП / 3 и 6 лабы / sem4 / sem4 / laba 4

.cpp
Скачиваний:
20
Добавлен:
18.02.2017
Размер:
4.84 Кб
Скачать
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <conio.h>
#include <iostream>
#include <cmath>
#pragma once
using namespace std;

class Shape // класс форма 
{
protected:
	double S; //площадь
	int x[100], y[100]; //координаты
	double P; //периметр
	double p;//полупериметр
	double summa;
	double t[6]; //длина стороны
public:
	Shape ();
	int pow(int a, int b)// возведение в степень
{
int res=1;
while (b)
{
if (b%2==0)
{
b/=2;
a*=a;
}
else
{
b--;
res*=a;
}
}
return res;
}
	virtual void Draw()
	{
		
		cout<<  "(" << x[0] << "," << y[0] << ")" << endl;
	}
	virtual void Move(int NEWx,int NEWy)
	{
		x[0] = x[0]+NEWx;
		y[0] = y[0]+NEWy;
	}
	virtual void properties(){
		cout<<"Properties:"<<endl;
		cout<<"dlina1: "<< t[0]<<endl;
		cout<<"dlina2: "<< t[1]<<endl;
		cout<<"dlina3: "<< t[2]<<endl;
		cout<<"dlina4: "<< t[3]<<endl;
		cout<<"dlina5: "<< t[4]<<endl;
		cout<<"perimetr: "<< P<<endl;
		cout<<"square: "<<S<<endl;
		}
	Shape(int ax0, int ay0)
	{
		x[0] = ax0;
		y[0] = ay0;
	}
	int Intersection(Shape* pp)
	{
		return (this->S) - (pp->S);
	}
	/*Shape operator + (int i){
	 Shape tmp;
	 tmp.x[0]=this->x[0]+i;
	 tmp.y[0]=this->y[0]+i;
	 return tmp;}*/



};
class Triangle: public Shape
{   
public:
	
	Triangle(int ax0, int ay0, int ax1, int ay1, int ax2, int ay2):Shape(ax0, ay0)
	{
		x[1] = ax1;
		y[1] = ay1;
		x[2] = ax2;
		y[2] = ay2;
		t[0]= pow((x[0]-x[1]),2)+ pow((y[0]-y[1]),2);t[0]=sqrt(t[0]);
		t[1]= pow((x[1]-x[2]),2)+ pow((y[1]-y[2]),2);t[1]=sqrt(t[1]);
		t[2]= pow((x[0]-x[2]),2)+ pow((y[0]-y[2]),2);t[2]=sqrt(t[2]);
		t[3]=0; t[4]=0;
		P=t[0]+t[1]+t[2];
		p=P/2;
		S=p*(p-t[0])*(p-t[1])*(p-t[2]);S=sqrt(S);
	}
	Triangle();
	void Draw()
	{   
		cout<<  "Coordinates of the triangle:" <<  endl;
		Shape::Draw(); 
		for(int i=1; i<3; i++)
		{
			cout<< "(" << x[i] << "," << y[i] << ")"<< endl;
		}
	}
	void Move(int NEWx,int NEWy)
	{
		
		Shape::Move(NEWx,NEWy);
		for(int i=1; i<3; i++)
		{
			x[i] = x[i]+NEWx;
			y[i] = y[i]+NEWy;
		}
	}
};

class Square: public Shape 
{
public:
	
	Square(int ax0, int ay0, int ax1, int ay1, int ax2, int ay2, int ax3, int ay3):Shape(ax0, ay0)
	{
		x[1] = ax1;
		y[1] = ay1;
		x[2] = ax2;
		y[2] = ay2;
		x[3] = ax3;
		y[3] = ay3;
		t[0]= pow((x[0]-x[1]),2)+ pow((y[0]-y[1]),2);t[0]=sqrt(t[0]);
		t[1]= pow((x[1]-x[2]),2)+ pow((y[1]-y[2]),2);t[1]=sqrt(t[1]);
		t[2]= pow((x[2]-x[3]),2)+ pow((y[2]-y[3]),2);t[2]=sqrt(t[2]);
		t[3]= pow((x[0]-x[3]),2)+ pow((y[0]-y[3]),2);t[3]=sqrt(t[3]);
		t[4]=0;
		P=t[0]+t[1]+t[2]+t[3];
		S=t[0]*t[1];
	}
	Square();
	void Draw()
	{   
		cout<<  "Coordinates of the Square:" <<  endl;
		Shape::Draw(); 
		for(int i=1; i<4; i++)
		{
			cout<< "(" << x[i] << "," << y[i] << ")"<< endl;
		}
	}
	void Move(int NEWx,int NEWy)
	{
		
		Shape::Move(NEWx,NEWy);
		for(int i=1; i<4; i++)
		{
			x[i] = x[i]+NEWx;
			y[i] = y[i]+NEWy;
		}
	}
};
class Pentagon: public Shape
{
public:
	Pentagon(int ax0, int ay0, int ax1, int ay1, int ax2, int ay2, int ax3, int ay3,int ax4, int ay4):Shape(ax0, ay0)
	{
		x[1] = ax1;
		y[1] = ay1;
		x[2] = ax2;
		y[2] = ay2;
		x[3] = ax3;
		y[3] = ay3;
		x[4] = ax4;
		y[4] = ay4;
		t[0]= pow((x[0]-x[1]),2)+ pow((y[0]-y[1]),2);t[0]=sqrt(t[0]);
		t[1]= pow((x[1]-x[2]),2)+ pow((y[1]-y[2]),2);t[1]=sqrt(t[1]);
		t[2]= pow((x[2]-x[3]),2)+ pow((y[2]-y[3]),2);t[2]=sqrt(t[2]);
		t[3]= pow((x[4]-x[3]),2)+ pow((y[4]-y[3]),2);t[3]=sqrt(t[3]);
		t[4]= pow((x[0]-x[4]),2)+ pow((y[0]-y[4]),2);t[4]=sqrt(t[3]);

		P=t[0]+t[1]+t[2]+t[3]+t[4];
		S=(pow(t[0],2)/4)*sqrt(25+10*2.2);
	}
	Pentagon();
	void Draw()
	{   
		cout<<  "Coordinates of the  Pentagon:" <<  endl;
		Shape::Draw(); 
		for(int i=1; i<5; i++)
		{
			cout<< "(" << x[i] << "," << y[i] << ")"<< endl;
		}
	}
	void Move(int NEWx,int NEWy)
	{
		
		Shape::Move(NEWx,NEWy);
		for(int i=1; i<5; i++)
		{
			x[i] = x[i]+NEWx;
			y[i] = y[i]+NEWy;
		}
	}
};

int main(void)
{
	//Shape s; //объект s
	Shape* p[100];// массив указателей на базовый класс
	int pcnt=0;
	p[0]=new Triangle(15,5,10,17,9,10);
	p[1]=new Square(10,15,10,20,15,20,15,15);
	p[2]=new Pentagon (10,20,20,28,30,20,22,8,18,8);
	p[0]->Draw();
	p[0]->Move(1,1); cout<<"Move"<<endl;
	p[0]->Draw();
	p[0]->properties();
	p[1]->Draw();
	p[1]->Move(1,1);cout<<"Move"<<endl;
	p[1]->Draw();
	p[1]->properties();
	p[2]->Draw();
	p[2]->Move(1,1);cout<<"Move"<<endl;
	p[2]->Draw();
	p[2]->properties();
	cout <<"Pentagon->Triangle: "<< p[2]->Intersection(p[0]) << endl;
	cout <<"Pentagon->Square: "<< p[2]->Intersection(p[1]) << endl;
	cout <<"Square->Triangle: "<< p[1]->Intersection(p[0]) << endl;
	p[1]=p[0]+2;
//	p[0]->Draw();	
	getch();
	return 0;
}
Соседние файлы в папке sem4
  • #
    18.02.20174.84 Кб20laba 4.cpp
  • #
    18.02.20173.92 Кб20sem4.vcxproj
  • #
    18.02.2017955 б20sem4.vcxproj.filters
  • #
    18.02.2017143 б20sem4.vcxproj.user