Created
December 4, 2022 22:58
-
-
Save Morkrom/cc94cedfc618281f78b4287ba514d3e3 to your computer and use it in GitHub Desktop.
Convert integer to roman numeral in C
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <string.h> | |
#include <stdbool.h> | |
#include <stdlib.h> | |
static int oneThousand = 1000; | |
static int fiveHundred = 500; | |
static int oneHundred = 100; | |
static int fifty = 50; | |
static int ten = 10; | |
static int five = 5; | |
static char numFour[] = "IV"; | |
char *singlesChars(int tens) { | |
switch (tens) { | |
case 1: | |
return "I"; | |
case 2: | |
return "II"; | |
case 3: | |
return "III"; | |
case 4: | |
return "IV"; | |
case 5: | |
return "V"; | |
case 6: | |
return "VI"; | |
case 7: | |
return "VII"; | |
case 8: | |
return "VIII"; | |
case 9: | |
return "IX"; | |
case 10: | |
return "";//"X"; | |
} | |
return ""; | |
} | |
char *tensChars(int tens) { | |
//X, XX, XXX, XL, L, LX, LXX, LXXX, XC, C | |
switch (tens) { | |
case 1: | |
return "X"; | |
case 2: | |
return "XX"; | |
case 3: | |
return "XXX"; | |
case 4: | |
return "XL"; | |
case 5: | |
return "L"; | |
case 6: | |
return "LX"; | |
case 7: | |
return "LXX"; | |
case 8: | |
return "LXXX"; | |
case 9: | |
return "XC"; | |
case 10: | |
return "";//"C"; | |
} | |
return ""; | |
} | |
char *hundredsChars(int hundreds) { | |
//C, CC, CCC, CD, D, DC, DCC, DCCC, CM, M | |
switch (hundreds) { | |
case 1: | |
return "C"; | |
case 2: | |
return "CC"; | |
case 3: | |
return "CCD"; | |
case 4: | |
return "CD"; | |
case 5: | |
return "D"; | |
case 6: | |
return "DC"; | |
case 7: | |
return "DCC"; | |
case 8: | |
return "DCCC"; | |
case 9: | |
return "CM"; | |
case 10: | |
return ""; //"M"; | |
} | |
return ""; | |
} | |
char *thousandsChars(int thousandars, int parentar) { | |
int difference = parentar - thousandars*1000; | |
int memSize = 0; | |
int listCount = thousandars; | |
char thousands[thousandars]; | |
int startingIndex = 0; | |
if (difference >= 100) { | |
thousands[0] = *"C"; | |
startingIndex = 1; | |
} | |
for (int i = startingIndex; i < listCount; i++) { | |
thousands[i] = *"M"; | |
} | |
return thousands; | |
} | |
void convertIntToNumerals(int number) { | |
int thousands = 0; | |
int hundreds = 0; | |
int tens = 0; | |
int remainder = 0; | |
int subtractnumber = number; | |
if (number >= 1000) { | |
thousands = number / 1000; | |
subtractnumber -= thousands*1000; | |
} | |
if (subtractnumber >= 100) { | |
hundreds = subtractnumber / 100; | |
subtractnumber -= hundreds*100; | |
} | |
if (subtractnumber >= 10) { | |
tens = subtractnumber/10; | |
subtractnumber -= tens*10; | |
} | |
if (subtractnumber > 0) { | |
remainder = subtractnumber; | |
} | |
int difference = number - thousands*1000; | |
int memSize = 0; | |
int listCount = thousands; | |
int thousandsUnits = difference >= 100 ? thousands + 1: thousands; | |
char thousandsChars[thousandsUnits]; | |
int startingIndex = 0; | |
if (difference == 900) { | |
thousandsChars[0] = *"C"; | |
startingIndex = 1; | |
listCount += 1; | |
} | |
for (int i = startingIndex; i < listCount; i++) { | |
thousandsChars[i] = *"M"; | |
} | |
char *hundredsCharacters = hundredsChars(hundreds); | |
char *tensCharacters = tensChars(tens); | |
char *singlesCharacters = singlesChars(remainder); | |
int hundredsLength = strlen(hundredsCharacters); | |
int tensLength = strlen(tensCharacters); | |
int singlesLength = strlen(singlesCharacters); | |
int numberChars = hundredsLength + tensLength + singlesLength + listCount; | |
char newString[numberChars]; | |
int count = 0; | |
for (int i = 0; i < strlen(thousandsChars); i++) { | |
newString[count] = thousandsChars[i]; | |
count += 1; | |
} | |
for (int i = 0; i < hundredsLength; i++) { | |
newString[count] = hundredsCharacters[i]; | |
count += 1; | |
} | |
for (int i = 0; i < tensLength; i++) { | |
newString[count] = tensCharacters[i]; | |
count += 1; | |
} | |
for (int i = 0; i < singlesLength; i++) { | |
newString[count] = singlesCharacters[i]; | |
} | |
printf("%s\n", newString); | |
} | |
int main(int argc, char *argv[]) { | |
convertIntToNumerals(atoi(argv[1])); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment