Created
November 16, 2016 06:24
-
-
Save ldante86/7816b0750b80df06527a9799a40b4b02 to your computer and use it in GitHub Desktop.
number to Roman Numeral
This file contains 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
#!/bin/bash - | |
# | |
# SCRIPT: romam | |
# AUTHOR: Luciano D. Cecere | |
# DATE: 2014 | |
######################################################################## | |
# | |
# roman - convert number (1-9999) to Roman numeral | |
# Copyright (C) 2014 Luciano D. Cecere <ldante86@aol.com> | |
# | |
# This program is free software: you can redistribute it and/or modify | |
# it under the terms of the GNU General Public License as published by | |
# the Free Software Foundation, either version 3 of the License, or | |
# (at your option) any later version. | |
# | |
# This program is distributed in the hope that it will be useful, | |
# but WITHOUT ANY WARRANTY; without even the implied warranty of | |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
# GNU General Public License for more details. | |
# | |
# You should have received a copy of the GNU General Public License | |
# along with this program. If not, see <http://www.gnu.org/licenses/>. | |
# | |
######################################################################### | |
base=(I II III IV V VI VII VIII IX) | |
tens=(X XX XXX XL L LX LXX LXXX XC) | |
hundreds=(C CC CCC CD D DC DCC DCCC CM) | |
thousands=(M MM MMM ^IV ^V ^VI ^VII ^VIII ^IX) | |
_roman() | |
{ | |
case "$1" in | |
[1-9]|[1-9][0-9]|[1-9][0-9][0-9][0-9]|[1-9][0-9][0-9]) | |
input="$1" ;; | |
*) | |
echo "Out of range [1-9999]" | |
return | |
esac | |
subst_0=${input:0:1} | |
subst_1=${input:1:1} | |
subst_2=${input:2:1} | |
subst_3=${input:3:1} | |
base_0=${base[subst_0-1]} | |
base_1=${base[subst_1-1]} | |
base_2=${base[subst_2-1]} | |
base_3=${base[subst_3-1]} | |
tens_0=${tens[subst_0-1]} | |
tens_1=${tens[subst_1-1]} | |
tens_2=${tens[subst_2-1]} | |
hundreds_0=${hundreds[subst_0-1]} | |
hundreds_1=${hundreds[subst_1-1]} | |
thousands_0=${thousands[subst_0-1]} | |
case ${#input} in | |
1) echo $base_0 | |
;; | |
2) [ $subst_1 -eq 0 ] && echo $tens_0 | |
[ $subst_1 -gt 0 ] && echo $tens_0$base_1 | |
;; | |
3) [ $subst_1 -eq 0 -a $subst_2 -eq 0 ] && echo $hundreds_0 | |
[ $subst_1 -gt 0 -a $subst_2 -eq 0 ] && echo $hundreds_0$tens_1 | |
[ $subst_1 -eq 0 -a $subst_2 -gt 0 ] && echo $hundreds_0$base_2 | |
[ $subst_1 -gt 0 -a $subst_2 -gt 0 ] && echo $hundreds_0$tens_1$base_2 | |
;; | |
4) [ $subst_1 -eq 0 -a $subst_2 -eq 0 -a $subst_3 -eq 0 ] && echo $thousands_0 | |
[ $subst_1 -eq 0 -a $subst_2 -eq 0 -a $subst_3 -gt 0 ] && echo $thousands_0$base_3 | |
[ $subst_1 -eq 0 -a $subst_2 -gt 0 -a $subst_3 -eq 0 ] && echo $thousands_0$tens_2 | |
[ $subst_1 -eq 0 -a $subst_2 -gt 0 -a $subst_3 -gt 0 ] && echo $thousands_0$tens_2$base_3 | |
[ $subst_1 -gt 0 -a $subst_2 -eq 0 -a $subst_3 -eq 0 ] && echo $thousands_0$hundreds_1 | |
[ $subst_1 -gt 0 -a $subst_2 -eq 0 -a $subst_3 -gt 0 ] && echo $thousands_0$hundreds_1$base_3 | |
[ $subst_1 -gt 0 -a $subst_2 -gt 0 -a $subst_3 -eq 0 ] && echo $thousands_0$hundreds_1$tens_2 | |
[ $subst_1 -gt 0 -a $subst_2 -gt 0 -a $subst_3 -gt 0 ] && echo $thousands_0$hundreds_1$tens_2$base_3 | |
esac | |
} | |
if [ $# -eq 0 ]; then | |
echo Usage: ${0##*/} 1-9999 | |
exit 1 | |
fi | |
while [ $# -gt 0 ]; do | |
_roman "$1" | |
shift | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment