/home/toolbox/public_html/solutions/117/11715/a.c
1 #include <stdio.h>
2 #include <string.h>
3 #include <sys/types.h>
4 #include <sys/stat.h>
5 #include <fcntl.h>
6 #include <stdint.h>
7 #include <math.h>
8 #include <stdlib.h>
9 #include <ctype.h>
10
11 #define TRUE (1 == 1)
12 #define FALSE (1 != 1)
13
14 #define DEBUG if (FALSE)
15
16 /*
17 * Author: Isaac Traxler
18 * Date: 2022-10-26
19 * Purpose: fun
20 * Problem: 11715 - cars
21 */
22
23 /*
24 * This template reads data until a terminating value is reached.
25 */
26
27 int cnt;
28 int cse;
29
30 void init()
31 {
32 /* FUNCTION init */
33 cnt = 1;
34 } /* FUNCTION init */
35
36 void dump()
37 {
38 /* FUNCTION dump */
39 } /* FUNCTION dump */
40
41 int getInput()
42 {
43 /* FUNCTION getInput */
44 int dataReadFlag;
45
46 scanf(" %d ", &cse);
47 dataReadFlag = 0 < cse;
48 return (dataReadFlag);
49 } /* FUNCTION getInput */
50
51 void process()
52 {
53 /* FUNCTION process */
54 double a; /* acceleration */
55 double s; /* distance traversed */
56 double t; /* time */
57 double u; /* initial speed */
58 double v; /* speed after time t */
59
60 switch (cse)
61 {
62 /* switch */
63 /* given: u v t - calculate a, s */
64 case 1:
65 scanf(" %lf %lf %lf ", &u, &v, &t);
66 a = (v - u) / t;
67 s = t * (v + u) / 2;
68 printf("Case %d: %.3lf %.3lf\n", cnt, s, a);
69 break;
70 /* given: u v a - calculate t, s */
71 case 2:
72 scanf(" %lf %lf %lf ", &u, &v, &a);
73 t = (v - u) / a;
74 s = t * (v + u) / 2;
75 printf("Case %d: %.3lf %.3lf\n", cnt, s, t);
76 break;
77 /* given: u a s - calculate v, t */
78 case 3:
79 scanf(" %lf %lf %lf ", &u, &a, &s);
80 /* algebra to figure v out
81 *
82 * s = t * (v + u) / 2 (from case 1)
83 * 2 * s = t * (v + u) (multiply both sides by 2)
84 * t * (v + u) = 2 * s (flip sides)
85 * t = 2 * s / (v + u) (divide by (v + u) )
86 *
87 * a = (v - u) / t (from case 1)
88 * (a * t) = v - u (multiply both sides by t)
89 * v - u = a * t (flip sides)
90 * v = u + ( a * t) (add u to both sides)
91 * v = u + (a * (2 * s / (v + u))) (substitute for t from 6 lines up)
92 * v - u = a * (2 * s ) /(v + u) (subtract u fron both sides)
93 * v - u = 2 * a * s / (v + u) (remove parens)
94 * (v + u) (v - u) = 2 * a * s (multiply both sides by (v + u) )
95 * v * v - u * u = 2 * a * s (do multiplication)
96 * v * v = (u * u) + (2 * a * s) (add u * u to both sides)
97 * v = sqrt((u * u + 2 * a * s) (sqrt each side)
98 */
99 v = sqrt((u * u + 2 * a * s));
100 t = (v - u) / a;
101 printf("Case %d: %.3lf %.3lf\n", cnt, v, t);
102 break;
103 /* given: v a s - calculate u, t */
104 case 4:
105 scanf(" %lf %lf %lf ", &v, &a, &s);
106 /* taken from above (case 3)
107 * v * v = (u * u) + (2 * a * s)
108 * (v * v) - (2 * a * s) = (u * u) (subtract 2 * a * s from both sides)
109 * (u * u) = (v * v) - (2 * a * s) (flip sides)
110 * u = sqrt((v * v) - (2 * a * s)) (sqrt of both sides)
111 */
112 u = sqrt((v * v) - (2 * a * s));
113 t = (v - u) / a;
114 printf("Case %d: %.3lf %.3lf\n", cnt, u, t);
115 break;
116 } /* switch */
117 cnt++;
118 } /* FUNCTION process */
119
120 int main()
121 {
122 /* main */
123 int moreToDo;
124
125 init();
126 moreToDo = getInput();
127 while (moreToDo)
128 {
129 /* while */
130 process();
131 moreToDo = getInput();
132 } /* while */
133
134 return EXIT_SUCCESS;
135 } /* main */
136