-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdecel_dist.c
73 lines (59 loc) · 1.4 KB
/
decel_dist.c
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
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <unistd.h>
#include <stdbool.h>
#define ASTRONOMICAL_UNIT 149597870
void print_usage()
{
puts("Usage: decel_dist <current_velocity> <max_speed> - both units are kilometers");
}
void print_program_desc()
{
puts("Space Engine: Deceleration distance calculator");
}
double kmtoau(double distance_km)
{
return distance_km / ASTRONOMICAL_UNIT;
}
int main(int argc, char *argv[])
{
if (argc < 3) {
print_usage();
return 1;
}
int c;
bool to_au = false;
while ((c = getopt(argc, argv, "a")) != -1) {
switch (c) {
case 'a':
to_au = true;
break;
default:
print_usage();
return 1;
}
}
// check for the missing arguments to prevent seg fault
if (argc == 3 && optind == 2) {
print_program_desc();
print_usage();
return 1;
}
double current_velocity = atof(argv[optind]);
double max_speed = atof(argv[optind + 1]);
if (current_velocity == 0.0 || max_speed == 0.0) {
print_program_desc();
print_usage();
return 1;
}
double distance_to_decel =
(0 - pow(current_velocity, 2)) / (2 * max_speed);
distance_to_decel =
to_au == true ? kmtoau(distance_to_decel) : distance_to_decel;
char *unit = to_au == true ? "AU" : "kilometers";
print_program_desc();
printf("Distance needed to decelerate is %f %s\n",
fabs(distance_to_decel), unit);
return 0;
}