Question:
Switch/case problem in C language. Help please?
nandemonai
2007-11-08 02:10:38 UTC
Problem: Write a program that accepts an ordinary number and output its equivalent Roman numeral. The ordinary numbers and their equivalent Roman numerals are as follows: 1 corresponds to I ; 5 = V; 10 = X; 50 = L; 100 = C; 500 = D; 1000 = M.

Sample input/output on screen: Enter a number: 2968
The Roman numeral is: MMCMLXVIII

Note that maximum input number is 3,000.

I don't know how to do it, it can't possibly mean doing the syntax 3000 times! I think it may have to do with the place values, but I don't know how to work it out. Help please? Thanks.
Four answers:
Xelfman
2007-11-08 04:00:57 UTC
To convert an integer into Roman numerals, you'll need to have a "while loop" to repeatedly generate the numerals you want until the result reaches zero.



Inside the loop, you'll have a switch statement that goes something like:



Switch input:

case input > 1000

output "M"

input = input - 1000

break

case input > 900

output "XM"

input = input - 900

break

...

...

End Switch
brunt
2007-11-08 06:20:02 UTC
No need for a switch statement at all. The Cleanest that I can think of would be this:



void PrintRoman (int Number)

{

char Roman[100];

int Position = 0;



while (Number >= 1000)

{

Roman[Position++] = 'M';

Number -= 1000;

}



if (Number >= 900)

{

Roman[Position++] = 'C';

Roman[Position++] = 'M';

Number -= 900;

}



if (Number >= 500)

{

Roman[Position++] = 'D';

Number -= 500;

}



while (Number >= 100)

{

Roman[Position++] = 'C';

Number -= 100;

}



if (Number >= 90)

{

Roman[Position++] = 'X';

Roman[Position++] = 'C';

Number -= 90;

}



if (Number >= 50)

{

Roman[Position++] = 'L';

Number -= 50;

}



while (Number >= 10)

{

Roman[Position++] = 'X';

Number -= 10;

}



if (Number >= 9)

{

Roman[Position++] = 'I';

Roman[Position++] = 'X';

Number -= 9;

}



if (Number >= 5)

{

Roman[Position++] = 'V';

Number -= 5;

}



if (Number >= 4)

{

Roman[Position++] = 'I';

Roman[Position++] = 'V';

Number -= 4;

}



while (Number >= 1)

{

Roman[Position++] = 'I';

Number -= 1;

}



Roman[Position] = '\0';



printf ("Roman is %s\n", Roman)

}



There are more compact ways to write this using either parallel arrays or a structure, but for clarity, the above is best.
2007-11-08 03:44:55 UTC
From old memories.. look at the dragon book for compilers. I vaguely recall it having this case as an example for illustrating scanning or parsing.



read from left to right. for every letter that represents a value greater than the most recent one that you have read in, subtract the smaller one from the bigger one. At the same time, if you come across an X, add 10 to some variable. If you read IX, add 1 to a variable and remember 1 in a temp variable. if the temp is lesser than the next one, say in this case, X(=10), then subtract the temp from the grand total, and add present-temp to the grand total. This way, you should be able to work out.



Anyway, this came to me off handed and might need some fine tuning.
2007-11-08 07:14:03 UTC
Pseudo-code sample could be something like:



char input[];

char output[];

int number;

for(i=0;i
number = atoi(input[i]);

switch(number)

case 1:

if(number%1000) output[i]="M";

else(number%100) output[i]="C";

else(number%10) output[i]="X";

else output[i]="I";

break;

}



But you'll need to extend this and write it out properly, cos I couldn't be bothered ;-)


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...