Saturday, October 5, 2013

Decimal/Roman Conversion

Roman numerals consist of the letters I, V, X, L, C, D, M which represent the values 1, 5, 10, 50, 100, 500, 1000. These letters are combined in an order-value system. Basically the rule is, if a letter is followed by an equal or lower valued one it counts as positive. If it is followed by a higher valued one it counts negative, i.e., in the number IX, I is followed by X, which is higher valued, hence I serves as a negative influence. This means that the value of I is subtracted from the value of X to yield NINE (9).

There are some refinements to the basic rule:
  1. There are not more than three equal letters in a number.
  2. Only I,X,C are used as negative values. For example, it is not MLD for 1450 but MCDL.
  3. The negating influence is only applied up to the next power of ten. I is only subtracted from V and X but not from L and C and X is only subtracted from L and C. So 9 is IX but 99 is XCIX not IC.
Given these rules you have to write a program which given a Roman number converts it into its equivalent Decimal number and vice-versa.




Notes:
  1. The number 0 will not be present in the input or output.
  2. The subtraction rule should be applied only to the letter following immediately and should not be propagated i.e. numbers like IXC will not be valid.
Input Specification:
• First line has an integer N which specifies how many numbers follow
• There are N subsequent lines, each of which has either a valid decimal or roman number.

Output Specification:
N lines are output, each containing the corresponding converted number.

Sample Input and Output:

Input:
1
999
Output:
CMXCIX
Input:
3
2000
MCDL
MCM
Output:
MM
1450
1900

Solution:

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

//Function for converting decimal number to roman number
void to_roman(char input[]);

//Function for converting roman number to decimal number  
void to_decimal(char input[]);

void main()
{
    char value[20][20];
    int i,j,k,n;

    scanf("%d",&n);

    for(i=0;i<n;i++)
    {
        scanf("%s",value[i]);
    }

    for(i=0;i<n;i++)
    {
        if(value[i][0]<='9' && value[i][0]>='0')
        to_roman(value[i]);
        else
        to_decimal(value[i]);
    } 
}

void to_roman(char input[])    //Changing decimal value to roman
{
    //printf("\nRoman");
    int x,i,j,c,temp,number;
    char rom[]="IVXLCDM";
    x=strlen(input);
    for(i=0;i<x;i++)
    {
        temp=1;
        number=input[i]-48;
        if(number==0)
        continue;
        for(j=0;j<(x-i-1);j++)
        {temp = temp * 10;}
 
        switch(temp)
        {
            case 1:
                c=0;
                break;
            
            case 5:
                c=1;
                break;
            
            case 10:
                c=2;
                break;
            
            case 50:
                c=3;
                break;
            
            case 100:
                c=4;
                break;
            
            case 500:
                c=5;
                break;
            
            case 1000:
                c=6;
                break;
        }//end switch

        //Printing roman number
        if( number<4 ) //1~3
        {
            for(j=0;j<number;j++)
            {printf("%c",rom[c]);}
        }
        else if( number==4 )//4
        printf("%c%c",rom[c],rom[c+1]);
        else if( number<9 ) //5~8
        {
            printf("%c",rom[c+1]);
            for(j=0;j<(number-5);j++)
            {printf("%c",rom[c]);}
        }
        else    //9
        printf("%c%c",rom[c],rom[c+2]);
    }//end main for loop, printing new line.
    printf("\n");
}

void to_decimal(char input[])
{
    //printf("Decimal\n");
    int x,i=0,j,temp,number;
    int c,current;
    //char rom[]="IVXLCDM";
 
    x=strlen(input);
    number=0;
    for(i=0;i<x;i++)
    {
        for(j=0;j<2;j++)//Checking current and next character        
        {
            switch(input[i+j])
            {
                case 'I':
                c=1;
                break;

                case 'V':
                c=5;
                break;

                case 'X':
                c=10;
                break;

                case 'L':
                c=50;
                break;

                case 'C':
                c=100;
                break;

                case 'D':
                c=500;
                break;

                case 'M':
                c=1000;
                break;
                
                default:
                c=0;
                break;
            }
            if(j==0)
            current=c;
        } //End Checking current and next character
        if(current<c) //Subtract
        number=number-current;
        else //Add
        number=number+current;
    }//End main for loop
    printf("%d\n",number);
}

No comments:

Post a Comment