Działania na wektorach

Program: przedstawiający działania na dwóch wektorach.

Wykonywane jest m.in.:

  • dodawanie,
  • odejmowanie,
  • przekształcenie.

Kompilator: Dev C++

Galeria:

Program w akcji.

Kod programu:

//Działania na wektorach - klasy, metody

#include <cstdlib>
#include <iostream>

using namespace std;

class wektor {
	private:
	float x,y;
	public:
	wektor();
	wektor(int , int );
	wektor operator+ (wektor &p);
	wektor operator- (wektor &p);
	void dodaj(wektor pp1, wektor pp2);
	void wypisz();
};

wektor wektor::operator+ (wektor &p)
{
	wektor tmp;
	tmp.x = x + p.x;
	tmp.y = y + p.y;
	return tmp;
}

wektor wektor::operator- (wektor &p)
{
	wektor tmp;
	tmp.x = x - p.x;
	tmp.y = y - p.y;
	return tmp;
}

wektor::wektor()
{
	x = 2;
	y = 2;
}
wektor::wektor(int x_kon2, int y_kon2)
{
	x = x_kon2;
	y = y_kon2;
}

void wektor::dodaj(wektor pp1, wektor pp2)
{
	x = pp1.x + pp2.x;
	y = pp1.y + pp2.y;	
}

void wektor::wypisz()
{
	cout<<"x = "<<x<<" | y = "<<y;
}

int main(int argc, char *argv[])
{
	int x,y;	
	cout<<"Podaj x: ";
	cin>>x;
	cout<<"Podaj y: ";
	cin>>y;
	{
	wektor wek1;
	wektor wek2(x,y);
	wektor dod;
		{
			dod = wek1 + wek2;
			dod.wypisz();
		}
	
	cout<<endl<<endl;
	wektor odj;
		{
			odj = wek1 - wek2;
			odj.wypisz();
		}
	}
	cout<<endl<<endl;

    system("PAUSE");
    return EXIT_SUCCESS;
}