-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrational_test.c
74 lines (59 loc) · 1.84 KB
/
rational_test.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
74
/**
* @brief Little program using rational numbers.
*
* This file implements a little example using rational numbers.
*
* @file rational_test.c
* @author Thomas Irgang
* @date 17 Feb 2015
*/
#include <stdio.h>
#include <stdlib.h>
#include "rational.h"
/**
* @brief Little example using rational numbers.
*
* This function is a little example how to use rational numbers.
*/
int main(void)
{
struct Rational *r, *s, *t;
char **string;
r = rational_create(); /* Create a rational number with value 0. */
r->n = 12; /* Set value of rational number. */
r->d = 18;
rational_normalize(r); /* Normalize the rational number. (Result: 2/3) */
printf("r=");
rational_print(r); /* Print rational number to stdout. */
printf("\n");
s = rational_get(1, 5); /* Create a rational number with given value. */
string = rational_to_string(s); /* Convert number to string (pointer to char[] containg string representation) */
printf("s=%8s\n", *string);
free(*string); /* Do not forget to free also the array, not only the pointer. */
free(string);
printf("r * s = ");
t = rational_multiply(r, s); /* Mulitply rational numbers. */
rational_print(t);
printf("\n");
free(t);
printf("r / s = ");
t = rational_divide(r, s); /* Divide rational numbers. */
rational_print(t);
printf("\n");
free(t);
printf("r + s = ");
t = rational_add(r, s); /* Add rational numbers. */
rational_print(t);
printf("\n");
free(t);
printf("r - s = ");
t = rational_subtract(r, s); /* Subtract rational numbers. */
rational_print(t);
printf("\n");
free(t);
printf("r < s? %d\n", rational_is_a_smaller_than_b(r, s)); /* Compare rational numbers. */
printf("s < r? %d\n", rational_is_a_smaller_than_b(s, r));
free(r);
free(s);
return EXIT_SUCCESS;
}