U E D R , A S I H C RSS

삼총사Cpp Study/숙제2/곽세환

~cpp 
#include <iostream>
using namespace std;
#include <cmath>

class CVector
{
private:
	double cx, cy, cz;
public:
	CVector();
	CVector(double x, double y, double z);
	void SetVector(double x, double y, double z);
	CVector operator+(CVector v);
	CVector operator-(CVector v);
	CVector operator*(double s);
	CVector operator*(CVector v);
	double operator^(CVector v);
	void Normalize();
	double Length();
	double GetX();
	double GetY();
	double GetZ();
	void ShowVector();
};

int main()
{
	CVector v1(10, 20, 30);
	CVector v2(50, 60, 70);
	CVector v3;
	
	cout << "v1 : ";
	v1.ShowVector();

	cout << "v2 : ";
	v2.ShowVector();

	v3 = v1 + v2;
	cout << "v1 + v2 : ";
	v3.ShowVector();

	v3 = v1 - v2;
	cout << "v1 - v2 : ";
	v3.ShowVector();

	v3 = v1 * 5;
	cout << "v1 * 5 : ";
	v3.ShowVector();

	v3 = v1 * v2;
	cout << "v1과 v2의 외적 : ";
	v3.ShowVector();

	cout << "v1과 v2의 내적 : " << (v1 ^ v2) << endl;

	cout << "v1의 길이 : " << v1.Length() << endl;

	v1.Normalize();
	cout << "v1의 초기화 : ";
	v1.ShowVector();

	return 0;
}

CVector::CVector()
{
	cx = cy = cz = 0;
}

CVector::CVector(double x, double y, double z)
{
	SetVector(x, y, z);
}

void CVector::SetVector(double x, double y, double z)
{
	cx = x;
	cy = y;
	cz = z;
}

CVector CVector::operator+(CVector v)
{
	CVector temp(cx + v.GetX(), cy + v.GetY(), cz + v.GetZ());
	return temp;
}	
	
CVector CVector::operator-(CVector v)
{
	CVector temp;
	temp.SetVector(cx - v.GetX(), cy - v.GetY(), cz - v.GetZ());
	return temp;
}

CVector CVector::operator*(double s)
{
	CVector temp(cx * s, cy * s, cz * s);
	return temp;
}

CVector CVector::operator*(CVector v)
{
	CVector temp(cy * v.GetZ() - cz * v.GetY(), cz * v.GetX() - cx * v.GetZ(), cx * v.GetY() - cy * v.GetX());
	return temp;
}

double CVector::operator^(CVector v)
{
	return cx * v.GetX() + cy * v.GetY() + cz * v.GetZ();
}

void CVector::Normalize()
{
	cx = cy = cz = 1;
}

double CVector::Length()
{
	return sqrt(cx * cx + cy * cy + cz * cz);
}

double CVector::GetX()
{
	return cx;
}

double CVector::GetY()
{
	return cy;
}

double CVector::GetZ()
{
	return cz;
}

void CVector::ShowVector()
{
	cout << "x = " << cx << ", y = " << cy << ", z = " << cz << endl;
}

진짜 잘하네요.. _ 단한개를 지적하자면 스칼라값을 곱할때 v3 = 5 * v1; 이 안된다는것 정도겠네요... 이런건 friend함수를 한개정도 더 만들어주면 됩니다.

수정판 2번째

~cpp 
#include <iostream>
using namespace std;
#include <cmath>

class CVector
{
private:
	double cx, cy, cz;
public:
	CVector();
	CVector(double x, double y, double z);
	void SetVector(double x, double y, double z);
	CVector operator+(CVector v);
	CVector operator-(CVector v);
	CVector operator*(double s);
	CVector operator*(CVector v);
	double operator^(CVector v);
	void Normalize();
	double Length();
	friend CVector operator*(double s, CVector v);
	friend ostream & operator<<(ostream & os, CVector v);
};

int main()
{
	CVector v1;
	v1.SetVector(10, 20, 30);
	CVector v2(50, 60, 70);
	CVector v3;
	
	cout << "v1 : " << v1 << endl;

	cout << "v2 : " << v2 << endl;
	
	v3 = v1 + v2;
	cout << "v1 + v2 : " << v3 << endl;
	
	cout << "v1 - v2 : " << v1 - v2 << endl;

	cout << "v1 * 5 : " << v1 * 5 << endl;

	cout << "5 * v1 : " << 5 * v1 << endl;
	
	cout << "v1과 v2의 외적 : " << v1 * v2 << endl;
	
	cout << "v1과 v2의 내적 : " << (v1 ^ v2) << endl;

	cout << "v1의 길이 : " << v1.Length() << endl;

	v1.Normalize();
	cout << "v1의 초기화 : " << v1 << endl;
	
	return 0;
}

CVector::CVector()
{
	cx = cy = cz = 0;
}

CVector::CVector(double x, double y, double z)
{
	SetVector(x, y, z);
}

void CVector::SetVector(double x, double y, double z)
{
	cx = x;
	cy = y;
	cz = z;
}

CVector CVector::operator+(CVector v)
{
	return CVector(cx + v.cx, cy + v.cy, cz + v.cz);
}	
	
CVector CVector::operator-(CVector v)
{
	return CVector(cx - v.cx, cy - v.cy, cz - v.cz);
}

CVector CVector::operator*(double s)
{
	return CVector(cx * s, cy * s, cz * s);
}

CVector CVector::operator*(CVector v)
{
	return CVector(cy * v.cz - cz * v.cy, cz * v.cx - cx * v.cz, cx * v.cy - cy * v.cx);
}

double CVector::operator^(CVector v)
{
	return cx * v.cx + cy * v.cy + cz * v.cz;
}

void CVector::Normalize()
{
	cx = cy = cz = 1;
}

double CVector::Length()
{
	return sqrt(cx * cx + cy * cy + cz * cz);
}

CVector operator*(double s, CVector v)
{
	return v * s;
}

ostream & operator<<(ostream & os, CVector v)
{
	os << "x = " << v.cx << ", y = " << v.cy << ", z = " << v.cz;
	return os;
}
Valid XHTML 1.0! Valid CSS! powered by MoniWiki
last modified 2021-02-07 05:29:39
Processing time 0.0147 sec