Alfodr
Published © GPL3+

Roman Numeral Converter for Arduino

An easy software solution that can be implemented into bigger projects.

BeginnerFull instructions provided12 minutes3,448
Roman Numeral Converter for Arduino

Things used in this project

Story

Read more

Code

Roman numerals converter

C/C++
/*
    Roman numerals converter

    This sketch is converting Roman numerals to Arabic numbers and back.
    It is made by following the logic of Java example I found online.

    Created 15 August 2018
    By Dusan Lesan
*/

String input = "1884";
//String input = "MDCCCLXXXIV";

void setup() {
    Serial.begin(9600);
    Serial.print(input);
    if (isValidNumber(input)) {
        Serial.print(" converted to Roman numerals is: ");
        Serial.println(toRoman(input.toInt()));
    } else {
        Serial.print(" converted to Arabic numbers is: ");
        Serial.println(toArabic(input));
    }
}

void loop() {}

boolean isValidNumber(String str) {
   if (str.length() == 0) return false;
   for (byte i = 0; i < str.length(); i++)
   {
       if (!isDigit(str.charAt(i))) return false;
   }
   return true;
}

const String numberMatrix[14][2] = {
    {"1000", "M"},
    {"900", "CM"},
    {"500", "D"},
    {"400", "CD"},
    {"100", "C"},
    {"90", "XC"},
    {"50", "L"},
    {"40", "XL"},
    {"10", "X"},
    {"9", "IX"},
    {"5", "V"},
    {"4", "IV"},
    {"1", "I"},
    {"0", ""}
};

String toRoman(int number) {
    int index = 0;
    String output = "";
    while (floor(number) < 13) {
        index = floor(number);
        output += numberMatrix[index][1];
        number -= numberMatrix[index][0].toInt();
    }
    return output;
}

int floor(int number) {
    int index = 0;
    for (index; index < 14; index++) {
        if (numberMatrix[index][0].toInt() <= number) {
            return index;
        }
    }
    return index;
}

const String reversedNumberMatrix[14][2] = {
    {"900", "CM"},
    {"1000", "M"},
    {"400", "CD"},
    {"500", "D"},
    {"90", "XC"},
    {"100", "C"},
    {"40", "XL"},
    {"50", "L"},
    {"9", "IX"},
    {"10", "X"},
    {"4", "IV"},
    {"5", "V"},
    {"1", "I"},
    {"0", ""}
};

int toArabic(String romanNumber) {
    int index = 0;
    int output = 0;
    while (floor(romanNumber) < 13) {
        index = floor(romanNumber);
        output += reversedNumberMatrix[index][0].toInt();
        romanNumber.remove(romanNumber.indexOf(reversedNumberMatrix[index][1]), reversedNumberMatrix[index][1].length());
    }
    return output;
}
int floor(String number) {
    int index = 0;
    for (index; index < 14; index++) {
        if (number.indexOf(reversedNumberMatrix[index][1]) != -1) {
            return index;
        }
    }
    return index;
}

Java code example

Java
This is a code that helped me with the logic of my code
import java.util.TreeMap;

public class RomanNumeralsConverter {

	private final static TreeMap<Integer, String> map = new TreeMap<Integer, String>();

	static {
		map.put(1000, "M");
		map.put(900, "CM");
		map.put(500, "D");
		map.put(400, "CD");
		map.put(100, "C");
		map.put(90, "XC");
		map.put(50, "L");
		map.put(40, "XL");
		map.put(10, "X");
		map.put(9, "IX");
		map.put(5, "V");
		map.put(4, "IV");
		map.put(1, "I");
	}

	public static void main(String[] args) {
		System.out.println(toRoman(5884));
	}

	public final static String toRoman(int number) {

		int l =  map.floorKey(number);
		if ( number == l ) {
			return map.get(number);
		}
		return map.get(l) + toRoman(number-l);
	}
}

Credits

Alfodr

Alfodr

2 projects • 3 followers

Comments