roman numerals

This commit is contained in:
Peter 2021-08-12 10:39:52 +08:00
parent e33e588cc0
commit 9422f7d7b8
6 changed files with 97 additions and 4 deletions

View File

@ -4,3 +4,5 @@ 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
Solutions: https://web.archive.org/web/20210810152315/https://teaching.csse.uwa.edu.au/units/CITS2002/labsheets/labsheet1-solns.php

View File

@ -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

View File

@ -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[])

View File

@ -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

24
Week 3/roman/numerals.c Normal file
View File

@ -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}
};

62
Week 3/roman/roman.c Normal file
View File

@ -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 <stdio.h>
#include <stdlib.h>
#include <string.h>
#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;
}