-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvector.cpp
More file actions
153 lines (148 loc) · 3.23 KB
/
vector.cpp
File metadata and controls
153 lines (148 loc) · 3.23 KB
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#include<iostream>
#include<cmath>
using namespace std;
class Vector3d
{
public:
double i,j,k;
Vector3d();
Vector3d(double, double, double);
void seti(double);
void setj(double);
void setk(double);
double geti();
double getj();
double getk();
void disp()
{
cout<<"Entered vectors are : "<<i<<"i + "<<j<<"j + "<<k<<"k"<<endl;
}
double dotProduct(Vector3d, Vector3d);
Vector3d crossProduct(Vector3d, Vector3d);
Vector3d add(Vector3d, Vector3d);
Vector3d sub(Vector3d, Vector3d);
};
Vector3d::Vector3d()
{
i=j=k=0;
}
Vector3d::Vector3d(double a, double b, double c)
{
i = a;
j = b;
k = c;
}
void Vector3d::seti(double a)
{
i = a;
}
void Vector3d::setj(double b)
{
j = b;
}
void Vector3d::setk(double c)
{
k = c;
}
double Vector3d::geti()
{
return i;
}
double Vector3d::getj()
{
return j;
}
double Vector3d::getk()
{
return k;
}
double Vector3d::dotProduct(Vector3d a, Vector3d b)
{
double dp = (a.i*b.i)+(a.j*b.j)+(a.k*b.k);
return dp;
}
Vector3d Vector3d :: crossProduct(Vector3d a, Vector3d b)
{
Vector3d res;
double ires = ((a.j*b.k)-(b.j*a.k));
double jres = -((a.i*b.k)-(b.i*a.k));
double kres = ((a.i*b.j)-(b.i*a.j));
res.seti(ires);
res.setj(jres);
res.setk(kres);
return res;
}
Vector3d Vector3d :: add(Vector3d a, Vector3d b)
{
Vector3d sum;
double ires = (a.i + b.i);
double jres = (a.j + b.j);
double kres = (a.k + b.k);
sum.seti(ires);
sum.setj(jres);
sum.setk(kres);
return sum;
}
Vector3d Vector3d :: sub(Vector3d a, Vector3d b)
{
Vector3d diff;
double ires = (a.i + b.i);
double jres = (a.j + b.j);
double kres = (a.k + b.k);
diff.seti(ires);
diff.setj(jres);
diff.setk(kres);
return diff;
}
int main()
{
int ch; double x,y,z,a,b,c;
cout<<"Enter first vector"<<endl;
cin>>x>>y>>z;
cout<<"Enter second vector"<<endl;
cin>>a>>b>>c;
Vector3d v1(x,y,z);
Vector3d v2(a,b,c);
a:
cout<<"Enter operation \n1.)Dot Product \n2.)Cross Product \n3.)Add \n4.)Subtract\n";
cin>>ch;
switch(ch)
{
case 1:
{
v1.disp();
v2.disp();
double dotp = v1.dotProduct(v1,v2);
cout<<"Dot Product of vectors is = ("<<dotp<<")"<<endl;
}break;
case 2:
{
v1.disp();
v2.disp();
Vector3d re;
re = re.crossProduct(v1, v2);
cout<<"Resultant vector is = ("<<re.geti()<<"i) + ("<<re.getj()<<"j) + ("<<re.getk()<<"k)"<<endl;
}break;
case 3:
{
v1.disp();
v2.disp();
Vector3d re;
re = re.add(v1,v2);
cout<<"Resultant vector is = ("<<re.geti()<<"i) + ("<<re.getj()<<"j) + ("<<re.getk()<<"k)"<<endl;
}break;
case 4:
{
v1.disp();
v2.disp();
Vector3d re;
re = re.sub(v1,v2);
cout<<"Resultant vector is = ("<<re.geti()<<"i) + ("<<re.getj()<<"j) + ("<<re.getk()<<"k)"<<endl;
}break;
default:
{
cout<<"Wrong input, please try again"<<endl;
goto a;
}break;
}return 0;
}