Jump to content

Decimal to Roman

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
5 replies to this topic

#1
TkTech

TkTech

    The Crazy One

  • Moderators
  • 1,396 posts
A quick, obscure decimal->roman.

Note: People using GCC need to compile with -lm since we are using math.h

#include <stdio.h>
#include <string.h>
#include <math.h>

int main(int argc, char *argv[])
{
    char        t[] = {'I','X','C','M','V','L','D'};
    char        r[37] = {0};
    int         pos = 0;
    
    if(argc == 2){
        argc = 0;
        
        while(argv[1][argc++] != '\0')
        {   
            while(((argv[1][argc-1])--) - 48 > 0)
            {
                if(argv[1][argc-1] - 48 >= 4 && strlen(argv[1])-argc != 3){
                    r[pos++] = t[strlen(argv[1])-argc+4];
                    argv[1][argc-1]-=4;
                }else
                    r[pos++] = t[strlen(argv[1])-argc];
            }
        }
        printf("%s\n",r);
    }
    return 0;    
}

Edited by TkTech, 21 September 2009 - 02:41 AM.
Typo oO


#2
Guest_Jordan_*

Guest_Jordan_*
  • Guests
Neat! Thanks for sharing! :)

#3
marwex89

marwex89

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 10,720 posts
Bet MathX can use this :D Wasn't he going to make something like this?
Hey! Check out my new Toyota keyboaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa

#4
TkTech

TkTech

    The Crazy One

  • Moderators
  • 1,396 posts
Yup, its why I made it.

#5
ZekeDragon

ZekeDragon

    Writes binary right handed and hex left handed

  • Moderators
  • 2,103 posts
Haven't ran it, but I don't think this'll work with numbers over 4000, and that was something I was going to address if I wrote an algorithm to make Roman numerals, should be able to go indefinitely high.

Otherwise, very nice, simple program. :)
Wow I changed my sig!

#6
TkTech

TkTech

    The Crazy One

  • Moderators
  • 1,396 posts
It'll work to 9999, which is more then most. I'll post an infinite one in a bit.

Edit: Er, bit more then a bit, wrong computer.