@@ -11,7 +11,7 @@ The numbers for the G.P. series:
11
11
The Sum of the G.P. series : 93.000000
12
12
13
13
+ 1. Write tests (run tests function + body of a test)
14
- 2. Input
14
+ + 2. Input
15
15
3. Invalid input
16
16
4. Finction to print out and find the sum
17
17
5. Check the tests
@@ -21,17 +21,73 @@ The Sum of the G.P. series : 93.000000
21
21
22
22
#include <stdio.h>
23
23
24
+ enum is_valid_boolean {
25
+ TRUE = 1 ,
26
+ FALSE = 0
27
+ };
28
+
24
29
void test (int first_number , int number_of_terms , int common_ratio , int expected_result , int test_number );
25
30
void run_tests ();
26
31
int find_gp_series (int first , int number_of_terms , int common_ratio );
32
+ int input_first_number (int * first_number );
33
+ int input_number_of_terms (int * number_of_terms );
34
+ int input_common_ratio (int * common_ratio );
35
+ void print_invalid_input ();
27
36
28
37
int main () {
29
- run_test ();
38
+ int first_number = 0 ;
39
+ if (input_first_number (& first_number )) {
40
+ int number_of_terms = 0 ;
41
+ if (input_number_of_terms (& number_of_terms )) {
42
+ int common_ratio = 0 ;
43
+ if (input_common_ratio (& common_ratio )) {
44
+ run_tests ();
45
+ } else {
46
+ print_invalid_input ();
47
+ }
48
+ } else {
49
+ print_invalid_input ();
50
+ }
51
+ } else {
52
+ print_invalid_input ();
53
+ }
30
54
return 0 ;
31
55
}
32
56
57
+ int input_common_ratio (int * common_ratio ) {
58
+ int is_valid = TRUE;
59
+ char endline = '\0' ;
60
+ if (!scanf ("%d%c" , common_ratio , & endline ) || endline != '\n' ) {
61
+ is_valid = FALSE;
62
+ }
63
+ return is_valid ;
64
+ }
65
+
66
+ int input_number_of_terms (int * number_of_terms ) {
67
+ int is_valid = TRUE;
68
+ char endline = '\0' ;
69
+ if (!scanf ("%d%c" , number_of_terms , & endline ) || endline != '\n' ) {
70
+ is_valid = FALSE;
71
+ }
72
+ return is_valid ;
73
+ }
74
+
75
+ int input_first_number (int * first_number ) {
76
+ int is_valid = TRUE;
77
+ char endline = '\0' ;
78
+ if (!scanf ("%d%c" , first_number , & endline ) || endline != '\n' ) {
79
+ is_valid = FALSE;
80
+ }
81
+ return is_valid ;
82
+ }
83
+
84
+ void print_invalid_input () {
85
+ printf ("n/a" );
86
+ }
87
+
33
88
int find_gp_series (int first , int number_of_terms , int common_ratio ) {
34
89
int sum = 0 ;
90
+ printf ("%d%d%d" ,first , number_of_terms , common_ratio );
35
91
return sum ;
36
92
}
37
93
0 commit comments