use strict;
print "Please input a ROMA number:\n";
while(<STDIN>)
{
chomp;
my $roma=$_;
if($roma=~ m/^[IVXLCDM]+$/i)
{
my %hash=("I"=>0,"V"=>1,"X"=>2,"L"=>3,"C"=>4,"D"=>5,"M"=>6);
my @value=(1,5,10,50,100,500,1000);
$roma=uc($roma);
my @roma=split '',$roma;
my ($result,$i,$flag);
$result=$value[$hash{$roma[0]}] if($i==$#roma);
while($i<$#roma)
{
if($hash{$roma[$i]}<$hash{$roma[$i+1]})
{
my $id;
if($hash{$roma[$i]}%2 ==0)
{
$id=3;
}else
{
$id=2;
}
if(($hash{$roma[$i+1]}-$hash{$roma[$i]})<$id)
{
$result+=$value[$hash{$roma[$i+1]}]-$value[$hash{$roma[$i]}];
$i+=2;
}else
{
print "Invalid number, wrong oder or grammer.\n";
last;
}
}else
{
$result+=$value[$hash{$roma[$i]}];
if($roma[$i+1] eq $roma[$i])
{
$flag++;
}else
{
$flag=0;
}
if($flag>2)
{
print "Invalid number, wrong grammer.\n";
last;
}
$i++;
}
}
if($i==$#roma)
{
$result += $value[$hash{$roma[-1]}];
$i++;
}
if(($i==$#roma+1)||($#roma == 0))
{
print "$roma = $result\n";
}
}else
{
print "Please input the valid Roma number\n";
}
}
|