1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
|
#include <iostream>
#include <vector>
#include <string>
#include <cmath>
using std::cout; using std::cin;
using std::endl;
class vectr {
public:
float x, y, z;
vectr() {};
vectr(float a, float b, float c) : x(a), y(b), z(c) {};
vectr(const vectr&);
float magnitude() {return sqrt(x * x + y * y + z* z);}
vectr operator+(vectr& other) {
vectr temp;
temp.x += other.x;
temp.y += other.y;
temp.z += other.z;
return temp;}
};
vectr::vectr(const vectr& v){
vectr v (v.x, v.y, v.z);
}
int main()
{
float x, y, z;
cout << "Enter <x, y, z> for vector A.\n";
cin >> x >> y >> z;
vectr A (x, y, z);
cout << "Enter <x, y, z> for vector B.\n";
cin >> x >> y >> z;
vectr B (x, y, z);
vectr C = A+B;
cout << "A + B = " << "<" << C.x << ", " << C.y << ", " << C.z << ">\n";
cout << "|A| = " << A.magnitude() << endl;
cout << "|B| = " << B.magnitude() << endl;
cout << "|A+B| = " << C.magnitude() << endl;
return 0;
}
|