-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmemfunct.cpp
executable file
·73 lines (62 loc) · 1.55 KB
/
memfunct.cpp
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
//A Simple Motion simulator
//Libraries
#include<cmath>
#include<bits/stdc++.h>
using namespace std;
//Object with member functions
struct V3{
double x,y,z;
struct V3 add(struct V3 const &b){
V3 v;
v.x=x+b.x;
v.y=y+b.y;
v.z=z+b.z;
return v;
}
double length(){
return(sqrt(x*x+y*y+z*z));
};
struct V3 scale(double const factor){
V3 v;
v.x=x*factor;
v.y=y*factor;
v.z=z*factor;
return v;
}
void print(){
cout<<"x="<<x<<" y="<<y<<" z="<<z<<endl;
}
};
//Accessing member function
signed main(){
V3 acc,vel,pos;//The initial accelerataion, velocity and position
V3 curr_disp,curr_pos;//The curret displacement and position
double delta_t,total_t,t=0.0;
//User input values
cout<<"Give the initial position \n";
cin>>pos.x>>pos.y>>pos.z;
cout<<"Give the initial components of velocity\n";
cin>>vel.x>>vel.y>>vel.z;
cout<<"Give the initial components of acceleration\n";
cin>>acc.x>>acc.y>>acc.z;
cout<<"Give the total time of simulation\n";
cin>>total_t;
cout<<"Give the simulation time granularity\n";
cin>>delta_t;
if((total_t<0)||(delta_t<0)){
cout<<"Invalid input\n";
return -1;
}
else{
//Program for motion simulator
//Calculating the current displacement using member functions
while(t<=total_t){
curr_disp = (vel.scale(t)).add(acc.scale(0.5*t*t));
curr_pos=curr_disp.add(pos);
cout<<"Time "<<t<<" ";
curr_disp.print();
t+=delta_t;
}
}
return 0;
}