סדנא תרגיל 3 שאלה 3

bennyסדנא ב++C, תרגילים, תרגילים בסדנא ב++CLeave a Comment

תרגול בנושא מחלקות, בתרגיל זה יש שתי מחלקות, מחלקה לייצוג נקודה (X,Y) ומחלקה ליצוג מצולע כלשהו המורכב ע"י נקודות ..

,תכנית ראשית

/******************************


targil: 3/3
lecturer: david Cohen
discription: calende the Scope of Polygonal is

*******************************/
#include "Point.h"
#include "Polygonal.h"
#include <iostream>
using namespace std;

int main()
{
int x,y;
Point A;               //creayte Point
int n;                 // the nym of the kodkodim
cin>> n;  
Polygonal R(n);      // creayte the Polygonal (metsula) with the num of kodkodim (n)

for (int i = 0; i < n; i++)
{
	cin>> x>>y;                // get point from user
	A.set_x(x); A.set_y(y);    // enter point cordinates to class point
	R.set_kodkod(A);            // insert the points to the kodkodim array
}

int sum=0;

for (int i = 0; i < R.get_kodkod_nam(); i++)
{
	if (i==R.get_kodkod_nam()-1)                             // the distance between the last piont to the first point ....
	{
		sum+=R.get_kodkod(0).distance(R.get_kodkod(i));      // ... R.get_kodkod(i) is the last point and R.get_kodkod(0) is the first point
	}
	else
	{
		sum+=R.get_kodkod(i+1).distance(R.get_kodkod(i));    //   add the distance between all points to the sum
	}
	
}

cout<<sum<<endl;    // print the result

//system ("pause");
return 0;
}

/*******OUTPUT************

4
2 1 5 8 6 4 2 5
19

**************************/

מימוש המחלקה Polygonal.

#include "Polygonal.h"
#include <iostream>
using namespace std;

Polygonal::Polygonal(int n)  // n is the number of kodkodim that user wont
{
	this->kodkod_nam=0;      
	//cout<<"c=tor on\n";
	this->kodkod=new Point[n];   //creayte array for the kodkodim
}



Polygonal::Polygonal(const Polygonal&Temp)    //copy c-tor    
{
	this-> kodkod_nam=Temp.kodkod_nam;

	//cout<<"copy c=tor on\n";
	for (int i = 0; i<kodkod_nam; i++)
	{
		kodkod[i]=Temp.kodkod[i];
	}
}



void Polygonal ::set_kodkod_nam(int n)  
{
	this->kodkod_nam=n;
}


int Polygonal ::get_kodkod_nam()   // important to know the num of kodkodim to clculate the Scope of Polygonal
{

	return kodkod_nam;
}


void Polygonal ::set_kodkod(Point A)    // to enter point to the kodkodim array
{
	kodkod[kodkod_nam]=A;
	kodkod_nam++;                     // update the num of kodkodim
}

Point Polygonal ::get_kodkod(int i)    // get kodkodim for clculate the distance between them
{
	return kodkod[i];

}

//void Polygonal::print_kodkodim()   
//{
//	for (int i = 0; i < kodkod_nam; i++)
//	{
	//	kodkod[i].printPoint();
//	}

//}


Polygonal::~Polygonal(void)
{
}

המחלקה Polygonal

#pragma once
#include "Point.h"

class Polygonal
{
	int kodkod_nam;
	Point* kodkod;
	
public:
	void set_kodkod(Point);

	//void print_kodkodim();

	void set_kodkod_nam(int);
	Point get_kodkod(int);
	int get_kodkod_nam();
	Polygonal(int);
	Polygonal(const Polygonal&Temp);
	~Polygonal(void);
};


מימוש המחלקה Point

#include "Point.h"
#include <cmath>
#include <iostream>
using namespace std;



Point::Point(void)
{
	
}

void Point::set_x(int x)
{
	this->x=x;
}
int Point::get_x()
{
	return x;
}

void Point::set_y(int y)
{
	this->y=y;
}
int Point::get_y()
{
	return y;
}
// function to return distance between two points
double Point::distance( Point p)
{

	return sqrt(pow(x-p.x,2.0)+pow(y-p.y,2.0));
}
void Point::printPoint()   // to print the point
{
	cout<<"("<<x<<","<<y<<")"<<" ";

}
Point::~Point(void)   // distructor
{
}

המחלקה Point

#pragma once
class Point
{
	int x;     // x cordinate of the point
	int y;     // y cordinate of the point
public:
	void set_x(int);
	int get_x();

	void set_y(int);
	int get_y();
	void printPoint();
	double distance(Point);
	Point(void);
	~Point(void);
};


כתיבת תגובה

האימייל לא יוצג באתר. שדות החובה מסומנים *