|
1 | | -#include "myIrrational.h" |
2 | 1 |
|
3 | | -int main(){ |
| 2 | +#include <stdio.h> |
| 3 | +#include <stdint.h> |
| 4 | +#include <stdlib.h> |
| 5 | +#include <string.h> |
| 6 | +#include <limits.h> |
| 7 | +#include <stdbool.h> |
| 8 | +#include <math.h> |
| 9 | +#include <float.h> |
4 | 10 |
|
5 | | - uint16_t n = 0; |
6 | | - |
| 11 | +// constants definitions |
| 12 | +#define PI 3.14159265358979323846 |
| 13 | + |
| 14 | +// function to get number of user |
| 15 | +uint16_t getNum(); |
| 16 | + |
| 17 | +// function to get user action |
| 18 | +void await_input(); |
| 19 | +void menu( uint16_t n ); |
| 20 | + |
| 21 | +// functions to calculate the irrational constants |
| 22 | +double calculate_two( const uint16_t n ); |
| 23 | +void calculate_pi( const uint16_t n ); |
| 24 | +double calculate_euler( const uint16_t n ); |
| 25 | + |
| 26 | +// function to print lines |
| 27 | +void print_line(); |
| 28 | + |
| 29 | +int main() |
| 30 | +{ |
| 31 | + menu(0); |
| 32 | + return 0; |
| 33 | +} |
| 34 | + |
| 35 | +void menu( uint16_t n ) |
| 36 | +{ |
| 37 | + while(n == 0) |
| 38 | + { |
| 39 | + n = getNum(); |
| 40 | + if(n != 0) break; |
| 41 | + } |
| 42 | + |
| 43 | + int32_t operation = 0; |
| 44 | + static double two = 0, euler = 0; |
| 45 | + print_line(); |
| 46 | + printf("Which constant do you want to display?\n"); |
| 47 | + |
| 48 | + printf("1. The Square root of 2 by continuous fraction"); |
| 49 | + if(two != 0) printf(" (%.15lf)", two); |
| 50 | + |
| 51 | + printf("\n2. The Gregory-Leibniz and Nilakantha representation of pi"); |
| 52 | + |
| 53 | + printf("\n3. The infinite series representation of euler's constant"); |
| 54 | + if(euler != 0) printf(" (%.15lf)", euler); |
| 55 | + |
| 56 | + printf("\n4. Choose another number"); |
| 57 | + printf("\n5. Exit the program\n"); |
| 58 | + printf("Your choice: "); |
| 59 | + scanf("%d", &operation); |
| 60 | + switch (operation) |
| 61 | + { |
| 62 | + case 1: |
| 63 | + two = calculate_two(n); |
| 64 | + break; |
| 65 | + |
| 66 | + case 2: |
| 67 | + calculate_pi(n); |
| 68 | + break; |
| 69 | + |
| 70 | + case 3: |
| 71 | + euler = calculate_euler(n); |
| 72 | + break; |
| 73 | + |
| 74 | + case 4: |
| 75 | + uint16_t newNum = getNum(n); |
| 76 | + menu(newNum); |
| 77 | + break; |
| 78 | + |
| 79 | + case 5: |
| 80 | + printf("eiting...\n"); |
| 81 | + exit(EXIT_SUCCESS); |
| 82 | + break; |
| 83 | + |
| 84 | + default: |
| 85 | + printf("Invalid option!\n"); |
| 86 | + break; |
| 87 | + |
| 88 | + } |
7 | 89 | menu(n); |
8 | | - |
9 | | - return 0; |
| 90 | + return; |
| 91 | +} |
| 92 | + |
| 93 | +uint16_t getNum() |
| 94 | +{ |
| 95 | + uint16_t num = 0; |
| 96 | + printf("Please enter n (16-bits unsigned): "); |
| 97 | + scanf("%hu", &num); |
| 98 | + if( num < 1 ) |
| 99 | + { |
| 100 | + printf("\nError, invalid input\n"); |
| 101 | + return 0; |
| 102 | + } else return num; |
10 | 103 | } |
| 104 | + |
| 105 | +double calculate_two( const uint16_t n ) |
| 106 | +{ |
| 107 | + print_line(); |
| 108 | + double val = 1.0; |
| 109 | + double preVal = 0; |
| 110 | + for(uint16_t i = 1 ; i <= n ; i++) |
| 111 | + { |
| 112 | + printf("n = %hu: %.15lf (%.15lf)\n", i, val, val - sqrt(2.0)); |
| 113 | + preVal = val; |
| 114 | + val = 1.0 + (1.0 / (1.0 + val)); |
| 115 | + } |
| 116 | + |
| 117 | + await_input(); |
| 118 | + return preVal; |
| 119 | + |
| 120 | +} |
| 121 | + |
| 122 | +void calculate_pi( const uint16_t n ) |
| 123 | +{ |
| 124 | + print_line(); |
| 125 | + double val_gl = 4.0, valNil = 3.0, denominator = 3.0, denominatorNil = 2.0, sign = 1.0; |
| 126 | + |
| 127 | + for(uint16_t i = 1 ; i <= n ; i++) |
| 128 | + { |
| 129 | + printf("n = %hu:\n\tGregory-leibniz series: %.15lf (%.15lf)\n", i, val_gl, fabs(val_gl - PI)); |
| 130 | + printf("\tNilakantha series: %.15lf (%.15lf)\n", valNil, fabs(valNil - PI)); |
| 131 | + |
| 132 | + val_gl = val_gl - (sign * ( 4 / denominator)); |
| 133 | + valNil = valNil + (sign * (4 / ((denominatorNil) * (denominatorNil + 1) * (denominatorNil + 2)))); |
| 134 | + |
| 135 | + denominator += 2; |
| 136 | + denominatorNil += 2; |
| 137 | + |
| 138 | + sign *= -1; |
| 139 | + } |
| 140 | + |
| 141 | + await_input(); |
| 142 | + return; |
| 143 | + |
| 144 | +} |
| 145 | + |
| 146 | +double calculate_euler( const uint16_t n ) |
| 147 | +{ |
| 148 | + print_line(); |
| 149 | + double val = 1.0, factorial = 1.0, preVal = 0;; |
| 150 | + for( uint16_t i = 1 ; i <= n ; i++ ) |
| 151 | + { |
| 152 | + printf("n = %hu: %.15lf (%.15lf)\n", i, val, val - exp(1.0)); |
| 153 | + factorial *= i; |
| 154 | + preVal = val; |
| 155 | + val += (1 / factorial); |
| 156 | + } |
| 157 | + |
| 158 | + await_input(); |
| 159 | + return preVal; |
| 160 | + |
| 161 | +} |
| 162 | + |
| 163 | +void print_line() |
| 164 | +{ |
| 165 | + for( int32_t i = 0 ; i < 80 ; i++) printf("-"); |
| 166 | + printf("\n"); |
| 167 | + return; |
| 168 | +} |
| 169 | + |
| 170 | +void await_input() |
| 171 | +{ |
| 172 | + char temp[UCHAR_MAX]; |
| 173 | + getchar(); |
| 174 | + printf("Return to menu to continue? [Y/n] "); |
| 175 | + scanf("%s", temp); |
| 176 | + for( uint8_t i = 0 ; temp[i] != '\0' ; i++) |
| 177 | + { |
| 178 | + if(temp[i] == 'N' || temp[i] == 'n') |
| 179 | + { |
| 180 | + printf("exiting...\n"); |
| 181 | + exit(EXIT_SUCCESS); |
| 182 | + } |
| 183 | + else if(temp[i] == 'Y' || temp[i] == 'y') |
| 184 | + { |
| 185 | + return; |
| 186 | + } |
| 187 | + else continue; |
| 188 | + } |
| 189 | + await_input(); |
| 190 | +} |
| 191 | + |
0 commit comments