|
| 1 | +// Task - output first 100 roman numbers |
| 2 | +// Convert decimal to roman in a loop |
| 3 | + |
| 4 | +#include <stdio.h> |
| 5 | +#include <stdlib.h> |
| 6 | + |
| 7 | +void iterate_over_range(const unsigned char start, const unsigned char end); |
| 8 | +void convert_to_roman(const unsigned char num); |
| 9 | +void roman_hundred(unsigned char *temp_num); |
| 10 | +void roman_fifty(unsigned char *temp_num); |
| 11 | +void roman_ten(unsigned char *temp_num); |
| 12 | +void roman_five(unsigned char *temp_num); |
| 13 | +void roman_one(unsigned char *temp_num); |
| 14 | +void print_newline(); |
| 15 | +void print_decimal(const unsigned char num); |
| 16 | +void print_header(); |
| 17 | + |
| 18 | +int main(void) { |
| 19 | + const unsigned char start = 1, end = 100; |
| 20 | + print_header(); |
| 21 | + iterate_over_range(start, end); |
| 22 | + return EXIT_SUCCESS; |
| 23 | +} |
| 24 | + |
| 25 | +void print_header() { |
| 26 | + printf("Decimal Roman\n" |
| 27 | + "numbers numerals\n" |
| 28 | + "-------------------\n"); |
| 29 | +} |
| 30 | + |
| 31 | +// I = 1, V = 5, X = 10, L = 50, C = 100 |
| 32 | +void convert_to_roman(const unsigned char num) { |
| 33 | + unsigned char temp_num = num; |
| 34 | + while (temp_num) { |
| 35 | + roman_hundred(&temp_num); |
| 36 | + roman_fifty(&temp_num); |
| 37 | + roman_ten(&temp_num); |
| 38 | + roman_five(&temp_num); |
| 39 | + roman_one(&temp_num); |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +void roman_one(unsigned char *temp_num) { |
| 44 | +while (*temp_num) { |
| 45 | + printf("I"); |
| 46 | + --*temp_num; |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +void roman_five(unsigned char *temp_num) { |
| 51 | + if (*temp_num >= 5) { |
| 52 | + printf("V"); |
| 53 | + *temp_num -= 5; |
| 54 | + } |
| 55 | + if (*temp_num == 4) { |
| 56 | + printf("IV"); |
| 57 | + *temp_num -= 4; |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +void roman_ten(unsigned char *temp_num) { |
| 62 | + while (*temp_num >= 10) { |
| 63 | + printf("X"); |
| 64 | + *temp_num -= 10; |
| 65 | + } |
| 66 | + if (*temp_num == 9) { |
| 67 | + printf("IX"); |
| 68 | + *temp_num -= 9; |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +void roman_fifty(unsigned char *temp_num) { |
| 73 | + if (*temp_num >= 50) { |
| 74 | + printf("L"); |
| 75 | + *temp_num -= 50; |
| 76 | + } |
| 77 | + if (*temp_num >= 40) { |
| 78 | + printf("XL"); |
| 79 | + *temp_num -= 40; |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +void roman_hundred(unsigned char *temp_num) { |
| 84 | + if (*temp_num >= 100) { |
| 85 | + printf("C"); |
| 86 | + *temp_num -= 100; |
| 87 | + } |
| 88 | + if (*temp_num >= 90) { |
| 89 | + printf("XC"); |
| 90 | + *temp_num -= 90; |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +void print_newline() { printf("\n"); } |
| 95 | + |
| 96 | +void print_decimal(const unsigned char num) { printf(" %-9d", num); } |
| 97 | + |
| 98 | +void iterate_over_range(const unsigned char start, const unsigned char end) { |
| 99 | + for (unsigned char index = start; index <= end; ++index) { |
| 100 | + print_decimal(index); |
| 101 | + convert_to_roman(index); |
| 102 | + print_newline(); |
| 103 | + } |
| 104 | +} |
0 commit comments