?
Solved

c# -find roman numerals and convert to int

Posted on 2010-11-24
5
Medium Priority
?
1,803 Views
Last Modified: 2013-12-17
hello everybody.. im in a bit of problem now..

i need to find roman numerals in a string and convert it to int eg:

"XIV VARA CIVIL" = int 14
"VARA CIVIL XX" = int 20

does anyone know if there is any method for it in web? maybe a regex to find could work but i have no clue or time to make such conversor

thx for the help!

0
Comment
Question by:eguilherme
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 32

Expert Comment

by:Erick37
ID: 34208281
Will the numerals always be at either the beginning or end of the string?
Will "VARA CIVIL" always be in the string?
0
 
LVL 10

Author Comment

by:eguilherme
ID: 34208317
not really.. that was just a sample..

the roman numerals could be anywhere in the string (most of the time they are at either the beggining or at the end, but i cant be sure if that would be always the case)

but the text it will be different everytime.

is it possible to do such thing ?
0
 
LVL 6

Expert Comment

by:ajb2222
ID: 34208470
Since the roman numeral will always be a word, first break the string into it's component words.

         string y = "VARA CIVIL XX";
         string[] x = y.Split(' ');

this makes anarray of strings
     x[0] = "VERA"
     x[1] = "CIVIL"
     x[2] = "XX"

then try each value in the string array x to see if it is a valid roman numeral.

Here is a link to an example of how to convert a roman numeral to a number.
http://www.blackwasp.co.uk/RomanToNumber.aspx
0
 
LVL 35

Expert Comment

by:Terry Woods
ID: 34208800
You could do a regex match with pattern:
(?i)\b(?=[ivxlcdm]+)M{0,4}(?:CM|CD|D?C{0,3})(?:XC|XL|L?X{0,3})(?:IX|IV|V?I{0,3})\b

Based on, but slightly modified from, a pattern from: http://stackoverflow.com/questions/267399/how-do-you-match-only-valid-roman-numerals-with-a-regular-expression

That would capture only valid roman numerals, I believe - then you'd have to convert them to an integer.
0
 
LVL 12

Accepted Solution

by:
jmcmunn earned 2000 total points
ID: 34247659
Once you have just the valid roman numerals, here is some javascript that converts both to and from roman numerals.  It is trivial to switch this to C# or whatever you want.
<script language="javascript">

<!--

function Level(i, v, x)
        {
        this.i = i;
        this.v = v;
        this.x = x
        }

levels = new Array();

levels[0] = new Level('I', 'V', 'X');
levels[1] = new Level('X', 'L', 'C');
levels[2] = new Level('C', 'D', 'M');




function calcDigit(d, l)
        {
        if (l > 2)
                {
                str = '';
                for (var m = 1; m <= d * Math.pow(10, l - 3) ; m++)
                        str += 'M';
                return str
                }
                        
        else
                if (d == 1)
                        return levels[l].i
                else
                        if (d == 2)
                                return levels[l].i + levels[l].i
                        else
                                if (d == 3)
                                        return levels[l].i + levels[l].i + levels[l].i
                                else
                                        if (d == 4)
                                                return levels[l].i + levels[l].v
                                        else
                                                if (d == 5)
                                                        return levels[l].v
                                                else
                                                        if (d == 6)
                                                                return levels[l].v + levels[l].i
                                                        else
                                                                if (d == 7)
                                                                        return levels[l].v + levels[l].i + levels[l].i
                                                                else
                                                                        if (d == 8)
                                                                                return levels[l].v + levels[l].i + levels[l].i + levels[l].i
                                                                        else
                                                                                if (d == 9)
                                                                                        return levels[l].i + levels[l].x
                                                                                else
                                                                                        return ''
        }

function toRoman(n)
        {
        var r = ''
        for (var c = 0; c < n.length; c++)
                r += calcDigit(eval(n.charAt(c)), n.length - c - 1);
        return r        
        }
        
function fromRoman(n)
        {
        var r = 0
        
        for (var c = 0; c < n.length; c++)
                {
                var chr = n.charAt(c).toLowerCase();
                if (c < n.length - 1)
                        var next = n.charAt(c + 1).toLowerCase()
                else
                        var next = '';
                        
                if (c > 0)
                        var prev = n.charAt(c - 1).toLowerCase()
                else
                        var prev = '';
                
                if (chr == 'i')
                        {
                        if (next == 'v')
                                r += 4
                        else
                                if (next == 'x')
                                        r += 9
                                else
                                        r += 1;
                        continue
                        }
                        
                if (chr == 'v') 
                        {
                        if (prev != 'i')
                                r += 5;
                        continue
                        }
                        
                if (chr == 'x')
                        {
                        if (prev != 'i')
                                if (next == 'l')
                                        r += 40
                                else
                                        if (next == 'c')
                                                r += 90
                                        else
                                                r += 10;
                        continue
                        }
                        
                if (chr == 'l')
                        {
                        if (prev != 'x')
                                r += 50;
                        continue
                        }
                        
                if (chr == 'c')
                        {
                        if (prev != 'x')
                                if (next == 'd')
                                        r += 400
                                else
                                        if (next == 'm')
                                                r += 900
                                        else
                                                r += 100;
                        continue
                        }

                if (chr == 'd')
                        {
                        if (prev != 'c')
                                r += 500;
                        continue
                        }
                
                if (chr == 'm')
                        {
                        if (prev != 'c')
                                r += 1000;
                        continue
                                
                        }
                }
        
        return r
        
        }


function isNuneric(str)
        {
        for (var c = 0; c < str.length; c++)
                {
                var chr = str.charAt(c);
                if (chr != '0' & chr != '1' & chr != '2' & chr != '3' & chr != '4' & chr != '5' & chr != '6' & chr != '7' & chr != '8' & chr != '9')
                        return false
                }
        return true
        }

function get(f)
        {
        
        var n = f.elements[0].value
        
        if (!isNuneric(n))
                {
                for (var c = 0; c < n.length; c++)
                        {
                        var chr = n.charAt(c).toLowerCase();
                        if (chr != 'i' & chr != 'v' & chr != 'x' & chr != 'l' & chr != 'c' & chr != 'd' & chr != 'm')
                                {
                                alert ('Only the letters IVXLCDM, please');
                                return false
                                }
                        }
                mode = 'fromRoman'
                }
        else
                mode = 'toRoman';

        if (mode == 'toRoman')
                f.elements[0].value = toRoman(f.elements[0].value)
        else
                f.elements[0].value = fromRoman(f.elements[0].value);
        
        return false
        }
  
function go(site)
        {
        if (site.selectedIndex>0)
                top.location=site.options[site.selectedIndex].value
        }


function foc()
        {
        if (navigator.userAgent.indexOf('Mozilla/3') != -1 | navigator.userAgent.indexOf('Mozilla/4') != -1)
                document.forms[0].elements[0].focus()   
        }

//-->

</script>

Open in new window

0

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Suggested Courses

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question