Task 1 A3

You might also like

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 6

#include<iostream>

#include<cmath>
using namespace std;
class vector
{
double* x;
double* y;
double* z;
public:
vector()
{
x = new double;
y = new double;
z = new double;
*x = 0;
*y = 0;
*z = 0;
}
vector(double a, double b, double c)
{
x = new double;
y = new double;
z = new double;
*x = a;
*y = b;
*z = c;
}
vector(const vector& c)
{
x = new double;
y = new double;
z = new double;
*x = *c.x;
*y = *c.y;
*z = *c.z;
}
~vector()
{
//cout << "destructor called for " << *x << " " << *y << " " << *z <<
endl;
delete x;
delete y;
delete z;
}
vector operator=(const vector& c)
{
*x = *c.x;
*y = *c.y;
*z = *c.z;
return *this;
}
vector operator+(const vector& c)
{
vector t;
*t.x = (*x + (*c.x));
*t.y = (*y + (*c.y));
*t.z = (*z + (*c.z));
return t;
}
vector operator-(const vector& c)
{
vector t;
*t.x = (*x - (*c.x));
*t.y = (*y - (*c.y));
*t.z = (*z - (*c.z));
return t;
}
vector operator ++()
{
++(*x);
++(*y);
++(*z);
return *this;
}
vector operator ++(int)
{
vector temp;
*temp.x = (*x)++;
*temp.y = (*y)++;
*temp.z = (*z)++;
return temp;
}
vector operator --()
{
--(*x);
--(*y);
--(*z);
return *this;
}
vector operator --(int)
{
vector temp;
*temp.x = (*x)--;
*temp.y = (*y)--;
*temp.z = (*z)--;
return temp;
}
float len()
{
float t;
t = float(sqrt((*x * (*x)) + (*y * (*y)) + (*z * (*z))));
return t;
}
float Angle(vector& a)
{
float t;
float dot;
dot = (*this * a);
t = acos((dot) / ((this->len()) * a.len()));
return t;
}
float operator*(const vector& c)
{
float t;
t = (*x * (*c.x)) + (*y * (*c.y)) + (*z * (*c.z));
return t;
}
friend bool operator ==(const vector& c1, const vector& c2);
friend bool operator !=(const vector& c1, const vector& c2);
friend bool operator <(const vector& c1, const vector& c2);
friend bool operator >(const vector& c1, const vector& c2);
friend ostream& operator <<(ostream& c1, const vector& c2);
friend istream& operator >>(istream& c1, const vector& c2);
};
bool operator==(const vector& c1, const vector& c2)
{

if ((*c1.x == *c2.x) && (*c1.y == *c2.y) && (*c1.z == *c2.z))


{
return true;
}
else
{
return false;
}

}
bool operator!=(const vector& c1, const vector& c2)
{

if ((*c1.x != *c2.x) && (*c1.y != *c2.y) && (*c1.z != *c2.z))


{
return true;
}
else
{
return false;
}

}
bool operator<(vector& c1, vector& c2)
{
float len1, len2;
len1 = c1.len();
len2 = c2.len();
if (len1 < len2)
{
return true;
}
else
{
return false;
}
}
bool operator>(vector& c1, vector& c2)
{
float len1, len2;
len1 = c1.len();
len2 = c2.len();
if (len1 > len2)
{
return true;
}
else
{
return false;
}
}
ostream& operator<<(ostream& c1, const vector& c2)
{
cout << *c2.x << "i ";
if (*c2.y >= 0)
{
cout << " + " << *c2.y << "j ";
}
else
{
cout << *c2.y << "j ";
}
if (*c2.z >= 0)
{
cout << " + " << *c2.z << "k ";
}
else
{
cout << *c2.z << "k ";
}
cout << endl;
return c1;
}
istream& operator>>(istream& c1, const vector& c2)
{
cout << endl;
cout << " x-component : ";
cin >> *c2.x;
cout << " y-component : ";
cin >> *c2.y;
cout << " z-component : ";
cin >> *c2.z;
return c1;
}
int main()
{

double* x = new double;


double* y = new double;
double* z = new double;
*x = 2;*y = -3;*z = 5;
vector u;
vector v;
vector q;
int choice = 0;
cout << endl;
cout << "Enter the 1st vector : ";
cout << endl << "
____________________________________________________________ " << endl;
cin >> u;
cout << endl;;
cout << "Enter the 2nd vector : ";
cout << endl << "
____________________________________________________________ " << endl;
cin >> v;
cout << endl;
cout << endl;
while (choice != 12)
{
cout << endl << "
____________________________________________________________ " << endl;
cout << "Enter the operation you want to perform : " << endl;
cout << "1. Dot Product.\n2. Legth of the vector.\n3. Angle between
vectors.\n4. Add.\n5. Subtraction.\n6. Post-increment\n7. pre-increment\n8. Equal
to\n9. NOT Equal to\n10. Post-decrement\n11. pre-decrement \n12. Exit.";
cout << endl;
cout << "Enter your choice(1-12) : ";
cout << endl << "
____________________________________________________________ " << endl;
cin >> choice;
if (choice == 1)
{
int m;
m = u * v;
cout << endl << "Dot product of vectors : " << m << endl;
}
else if (choice == 2)
{
cout << endl;
float len;
len = u.len();
cout << "length of vector 1 : ";
cout << len << endl;
len = v.len();
cout << "length of vector 2 : ";
cout << len << endl;
}
else if (choice == 3)
{
cout << endl;
float angle;
angle = u.Angle(v);
cout << "Angle between the vector is " << angle << endl;
}
else if (choice == 4)
{
cout << endl;
u = q + v;
cout << "Addition of two vectors : " << u << endl;
}
else if (choice == 5)
{
cout << endl;
u = q - v;
cout << "Subtraction of two vectors : " << u << endl;
}
else if (choice == 6)
{
cout << endl;
cout << "Vector 1 with post increment : " << u++ << endl;
cout << "Vector 2 with post increment : " << v++ << endl;
}
else if (choice == 7)
{
cout << endl;
cout << "Vector 1 with pre increment : " << ++u << endl;
cout << "Vector 2 with pre increment : " << ++v << endl;
}
else if (choice == 8)
{
if (u == v)
{
cout << endl << "vectors are equal" << endl;
}
else
{
cout << endl << "vectors are not equal" << endl;
}
}
else if (choice == 9)
{
if (u != v)
{
cout << endl << "vectors are not equal" << endl;
}
else
{
cout << endl << "vectors are equal" << endl;
}
}
else if (choice == 10)
{
cout << endl;
cout << "Vector 1 with post increment : " << u-- << endl;
cout << "Vector 2 with post increment : " << v-- << endl;
}
else if (choice == 11)
{
cout << endl;
cout << "Vector 1 with pre increment : " << --u << endl;
cout << "Vector 2 with pre increment : " << --v << endl;
}
else if (choice == 12)
{
break;
}
cout << endl << endl << endl;
system("pause");
system("cls");
}
}

You might also like