Thread: Need help
View Single Post
 
Reply
Posted 2003-11-19, 11:41 AM in reply to Hades-Knight's post "Need help"
Here's working code. I checked for invalid input such as the following:

XIr - Invalid Character
XXIVI - Invalid Sequence
XXIVV - Invalid Sequence
XXXXI - Invalid Repitition

Here's the code:
------------------------------------------------------
#include<stdio.h>

int main(void)
{
int i, previous = 0, num, total = 0, flag = 0, key = 0, error = 0, number, repeat = 1;
char roman[50];

printf("Enter the Roman numeral: ");
scanf("%s",roman);

for (i = 0; i < 50; i++)
{

if (roman[i] == 'M')
num = 1000;
else if (roman[i] == 'D')
num = 500;
else if (roman[i] == 'C')
num = 100;
else if (roman[i] == 'L')
num = 50;
else if (roman[i] == 'X')
num = 10;
else if (roman[i] == 'V')
num = 5;
else if (roman[i] == 'I')
num = 1;
else if (roman[i] == '\0')
flag = 1;
else
error = 1;

if (num == previous)
repeat = repeat + 1;
else
repeat = 1;

if (repeat == 4 && num == 1)
{
error = 1;
flag = 1;
}
if (repeat == 2 && num == 5)
{
error = 1;
flag = 1;
}
if (repeat == 4 && num == 10)
{
error = 1;
flag = 1;
}
if (repeat == 2 && num == 50)
{
error = 1;
flag = 1;
}
if (repeat == 4 && num == 100)
{
error = 1;
flag = 1;
}
if (repeat == 2 && num == 500)
{
error = 1;
flag = 1;
}

if (flag == 0)
{
if (num == 0)
error = 1;

if (num > previous)
{
if (key == 1)
{
number = i;
i = 50;
}
total = (total - (2 * previous)) + num;
key = 1;
}
else
total += num;

previous = num;
num = 0;
}
else
{
number = i - 1;
i = 50;
}

}

if (roman[number + 1] != '\0')
error = 1;

if (error == 1)
printf("Error in input.");
else
printf("%d\n",total);

return(0);
}
D3V said:
This message is hidden because D3V is on your ignore list.
What is it they say about silence being golden?
Old
Profile PM WWW Search
Medieval Bob enjoys the static noises of ten television sets simultaneously tuned to 412.84 MHzMedieval Bob enjoys the static noises of ten television sets simultaneously tuned to 412.84 MHz
 
 
Medieval Bob