-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGlobal_functions.cpp
More file actions
172 lines (153 loc) · 5.01 KB
/
Copy pathGlobal_functions.cpp
File metadata and controls
172 lines (153 loc) · 5.01 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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#include <limits>
#include <iostream>
#include <cmath>
#include <string>
#include <vector>
#include <algorithm>
#include "Global_functions.h"
void check ( Material &a, arm_specs &ar, stress_calc &cal, string area_t)
{
cal.set_opt_stress_initial();
ar.set_mod_radius(ar.get_radius());
ar.set_mod_base(ar.get_base());
ar.set_mod_height(ar.get_height());
ar.mod_area_calc(area_t);
if (0.9*a.get_strength()< cal.get_opt_stress())
{
while (0.9*a.get_strength()< cal.get_opt_stress( ))
{
if (area_t == "rectangle")
{
ar.set_mod_base( 1.01*ar.get_mod_base()); // increasing base
ar.set_mod_height( 1.01*ar.get_mod_height()); // increasing height
ar.mod_area_calc( area_t );
cal.calc_opt_inertia( area_t );
cal.calc_opt_stress ( area_t );
}
else if (area_t == "circle")
{
ar.set_mod_radius( 1.01*ar.get_mod_radius()); // increasing radius
ar.mod_area_calc( area_t );
cal.calc_opt_inertia( area_t );
cal.calc_opt_stress ( area_t );
}
}
}
else if (0.9*a.get_strength()> cal.get_opt_stress( ))
{
while (0.9*a.get_strength()> cal.get_opt_stress( ))
{
if (area_t == "rectangle")
{
ar.set_mod_base( 0.99*ar.get_mod_base()); // base reduction
ar.set_mod_height( 0.99*ar.get_mod_height()); // height reduction
ar.mod_area_calc(area_t);
cal.get_mod_arm_mass();
cal.get_mod_bending_M();
cal.calc_opt_inertia( area_t );
cal.calc_opt_stress ( area_t );
}
else if (area_t == "circle")
{
ar.set_mod_radius( 0.99*ar.get_mod_radius()); // radius reduction
ar.mod_area_calc(area_t);
cal.get_mod_arm_mass();
cal.get_mod_bending_M();
cal.calc_opt_inertia( area_t );
cal.calc_opt_stress ( area_t );
}
}
}
}
void input_errors(double &input)
{
while(input <= 0 || cin.fail())
{
cin.clear();
//This clears any error state from cin (like failbit) after wrong input
cin.ignore(numeric_limits<streamsize>::max(), '\n');
//to clear all the buffer storage inputs by deleting all characters untill ('\n')
cout<<"Invalid input, please enter a positive value! \n";
cin>>input;
}
}
double String_error(string &r)
{
while (true)
{
vector <char> Vr(r.begin(),r.end());
vector <char> New;
int dot = 0;
string number_str;
double number;
for(int i=0; i<Vr.size(); i++)
{
if (
Vr[i] == '1' ||
Vr[i] == '2' ||
Vr[i] == '3' ||
Vr[i] == '4' ||
Vr[i] == '5' ||
Vr[i] == '6' ||
Vr[i] == '7' ||
Vr[i] == '8' ||
Vr[i] == '9' ||
Vr[i] == '0'
)
{
New.push_back(Vr[i]);
}
else if (Vr[i]== '.')
{
dot++;
if (dot == 1)
{
New.push_back(Vr[i]);
}
else
{
cout << "\ninvalid input, re-enter.\n";
cin >> r;
goto jump_here;
// when the compiler reads goto it jumps automatically to jump_here: statement ignoring all the statements in between
}
}
else
{
cout << "\ninvalid input, re-enter.\n";
cin >> r;
goto jump_here;
}
}
number_str= string(New.begin(), New.end());
number = stod(number_str);
return number;
jump_here:;
}
}
void input_data(double &l,double &mass, double &acc)
{
cout<<"\nPlease enter the arm length (m): ";
cin>>l;
input_errors(l);
cout<<"Please enter the payload mass (kg): ";
cin>>mass;
input_errors(mass);
cout<<"Please enter the max angular acc (rad/sec^2): ";
cin>>acc;
input_errors(acc);
}
void default_dim(arm_specs def)
{
cout<<"\nBase ="<<def.get_base()<<" m\n";
cout<<"Height ="<<def.get_height()<<" m\n";
cout<<"Arm length ="<<def.get_length()<<" m\n";
cout<<"Payload mass ="<<def.get_mass()<<" kg\n";
cout<<"Max angular acc ="<<def.get_acc()<<" rad/sec^2\n";
}
/*
this function is used to combine the classes ( Material / arm_specs / stress_calc ) together to optimize the dimensions of the arm
there are 2 cases
case 1: the max stress is higher than the material's strength therefore the stress of the arm must be reduced to less than the material's strength
case 2: the max stress is much lower than the material's strength therefore dimensions are reduced to reduce the cost
*/