From 9422f7d7b88558264291a5b8e2f9ba1a230fbbd8 Mon Sep 17 00:00:00 2001 From: Peter Date: Thu, 12 Aug 2021 10:39:52 +0800 Subject: [PATCH] roman numerals --- Week 2/README.md | 4 ++- Week 3/README.md | 5 +-- Week 3/ackermann/ackermann.c | 1 - Week 3/autogram/README.md | 5 +++ Week 3/roman/numerals.c | 24 ++++++++++++++ Week 3/roman/roman.c | 62 ++++++++++++++++++++++++++++++++++++ 6 files changed, 97 insertions(+), 4 deletions(-) create mode 100644 Week 3/autogram/README.md create mode 100644 Week 3/roman/numerals.c create mode 100644 Week 3/roman/roman.c diff --git a/Week 2/README.md b/Week 2/README.md index 45ec3d8..67cdd92 100644 --- a/Week 2/README.md +++ b/Week 2/README.md @@ -3,4 +3,6 @@ Lab sheet: http://web.archive.org/web/20210731041540/http://teaching.csse.uwa.edu.au/units/CITS2002/labsheets/labsheet1.php \ `rotate.c` -http://web.archive.org/web/20200221172641/http://teaching.csse.uwa.edu.au/units/CITS2002/labsheets/rotate.c \ No newline at end of file +http://web.archive.org/web/20200221172641/http://teaching.csse.uwa.edu.au/units/CITS2002/labsheets/rotate.c + +Solutions: https://web.archive.org/web/20210810152315/https://teaching.csse.uwa.edu.au/units/CITS2002/labsheets/labsheet1-solns.php \ No newline at end of file diff --git a/Week 3/README.md b/Week 3/README.md index 7f5edd1..839728f 100644 --- a/Week 3/README.md +++ b/Week 3/README.md @@ -1,4 +1,5 @@ # Week 3 -Lab sheet: https://teaching.csse.uwa.edu.au/units/CITS2002/labsheets/labsheet2.php\ -Sample `square.c`: https://teaching.csse.uwa.edu.au/units/CITS2002/labsheets/labsheet2.php +Lab sheet: https://web.archive.org/web/20210810152346/https://teaching.csse.uwa.edu.au/units/CITS2002/labsheets/labsheet2.php + +Sample `square.c`: http://web.archive.org/web/20210810152436/https://teaching.csse.uwa.edu.au/units/CITS2002/labsheets/square.c diff --git a/Week 3/ackermann/ackermann.c b/Week 3/ackermann/ackermann.c index df1bc2b..21f6f9b 100644 --- a/Week 3/ackermann/ackermann.c +++ b/Week 3/ackermann/ackermann.c @@ -39,7 +39,6 @@ void ackermann(int m, int n, struct ack_state *state) { ackermann(m-1, state->value, state); } state->current_depth--; - // return state; } int main(int argc, char const *argv[]) diff --git a/Week 3/autogram/README.md b/Week 3/autogram/README.md new file mode 100644 index 0000000..5b6902a --- /dev/null +++ b/Week 3/autogram/README.md @@ -0,0 +1,5 @@ +# Not doing that, sorry. Maybe later + +https://yawn.io/2019/08/28/pangrammatic-autograms.html + +https://gist.github.com/bjackman/4b0ccb3f91a3b9c214872ed518c9ced9 \ No newline at end of file diff --git a/Week 3/roman/numerals.c b/Week 3/roman/numerals.c new file mode 100644 index 0000000..95ca4b6 --- /dev/null +++ b/Week 3/roman/numerals.c @@ -0,0 +1,24 @@ + +struct numeral { + char *numeral; + int value; + int count; +}; + +#define NUMERAL_AMOUNT 13 + +static const struct numeral NUMERALS[] = { + {.count = 0, .numeral = "M", .value = 1000}, + {.count = 0, .numeral = "CM", .value = 900}, + {.count = 0, .numeral = "D", .value = 500}, + {.count = 0, .numeral = "CD", .value = 400}, + {.count = 0, .numeral = "C", .value = 100}, + {.count = 0, .numeral = "XC", .value = 90}, + {.count = 0, .numeral = "L", .value = 50}, + {.count = 0, .numeral = "XL", .value = 40}, + {.count = 0, .numeral = "X", .value = 10}, + {.count = 0, .numeral = "IX", .value = 9}, + {.count = 0, .numeral = "V", .value = 5}, + {.count = 0, .numeral = "IV", .value = 4}, + {.count = 0, .numeral = "I", .value = 1} +}; diff --git a/Week 3/roman/roman.c b/Week 3/roman/roman.c new file mode 100644 index 0000000..ad1bda5 --- /dev/null +++ b/Week 3/roman/roman.c @@ -0,0 +1,62 @@ +/* +* 🌶 🌶 Write a program that prints out all "roman numeral" equivalents of the + numbers between 1 and the single argument supplied to the program. Use the + rules for writing Roman numerals, and the online calculator, from: + Calculator Soup (just for the numerals I, V, X, L, C, D, and M). +*/ + +#include +#include +#include +#include "numerals.c" + +void arabic_to_roman(int n, struct numeral numerals[]) { + int idx = 0; + while (n > 0 && idx < NUMERAL_AMOUNT) + { + int value = numerals[idx].value; + if ( (n-value) < 0 ) + { + idx++; + } else { + n -= value; + numerals[idx].count++; + } + } +} + +void print_roman(struct numeral numerals[]) { + for (size_t i = 0; i < NUMERAL_AMOUNT; i++) + { + char *numeral = numerals[i].numeral; + for (size_t j = 0; j < numerals[i].count; j++) + { + printf("%s",numeral); + } + } + printf("\n"); +} + +int main(int argc, char const *argv[]) +{ + if (argc != 2) + { + printf("input one integer number\n"); + exit(EXIT_FAILURE); + } + + int n = atoi(argv[1]); + struct numeral numerals[NUMERAL_AMOUNT]; + memcpy(&numerals, &NUMERALS, sizeof NUMERALS); + arabic_to_roman(n, numerals); + if (n > 3999) + { + printf("[!] Outside of standard roman numerals.\n"); + } + + printf("%s = ",argv[1]); + print_roman(numerals); + + exit(EXIT_SUCCESS); + return 0; +}