![]() |
| | #1 |
| Registered User Join Date: Nov 2009
Posts: 18
| I need help on how to change user entered numerical number into Roman character. For example , 1 = i , 2 = ii , 5 = v , 10 = x , 13 = xiii etc... I have done the programming half way and do not know how to continue from 11 onward. Please advice !!! --------------------------------------------------------------------- Code:
#include<stdio.h>
int romanize(int q, int base, char letter)
{
int counter, counter2, temp, test;
if(base == 1)
{
if(q < 5)
{
printf("Romanize character is ");
for(counter = 1; counter <= q; counter++)
{
printf("%c",letter);
}
}
if(q >=5 && q <=9)
{
printf("Romanize character is v");
temp = q - 5;
for(counter = 1; counter <= temp; counter++)
{
printf("%c",letter);
}
}
}
//-------------------------------------------------------------------------------------------
if(base == 2)
{
test = q;
counter = 0;
counter2 = 0;
if(test <= 10 && test <= 49)
{
while( test >= 9 )
{
test = test - 10;
counter2++;
}
printf("inside loop is : %d\n",counter2);
printf("Romanize character is ");
for(counter = 1; counter <= counter2; counter++)
{
printf("%c",letter);
}
if(test < 5)
{
for(counter = 1; counter <= test; counter++)
{
printf("i");
}
}
if(test >=5 && test <=9)
{
printf("v");
temp = test - 5;
for(counter = 1; counter <= temp; counter++)
{
printf("%i");
}
}
}
}
}
main()
{
int i, n, ch, number, count, intnumber[10];
number = 0, count=0, i=0;
printf("input ?: ");
ch = getchar();
while(ch!='\n')
{
if('0' <= ch && ch <= '9')
{
//This is to count the number of digit the user has enter//
n = 0;
n = n * 10;
n = n + (ch - '0');
intnumber[i] = n;//Store each entered digit into array "intnumber[]"//
count++;
i++;
//--------------------------------------------------------//
number = number * 10;
number = number + (ch - '0');
}
ch = getchar();
}
printf("count is %d\n",count);
printf("number is %d\n",number);
if( count <= 1 ) // number is less than 10
{
romanize(number,count,'i');
}
else if( count > 1 ) // number is (10 <= number <=99)
{
romanize(number,count,'x');
}
}
|
| spurs01 is offline | |
| | #2 |
| Registered User Join Date: Sep 2006
Posts: 2,510
| Welcome to the C forum. ![]() Think of a funnel - big at the top, but quickly starts catching the bigger things as they are poured in. Then the medium sized things are caught as the funnel continues to narrow down. Finally, the smallest stuff is caught. And it's all done in descending order (big to small). You can do roman numerals this same way, using a while or for loop, with some logic to make it continually handle smaller and smaller numbers, and transfer them to roman numerals, until there are no numbers left. (or until we see we want another block of code to handle some part of the funnel that we're building. Some pseudo code: Code:
char roman[40];
int amount, i;
Get amount from user.
i = 0;
do {
switch(amount) {
case >= 1000:
roman[i] = M; ++i; amount -= 1000; break;
case >= 900;
roman[i] = C; ++i; roman[i] = M; ++i; amount -= 900; break;
case >= 500;
//etc.
/*
continue above for all cases of roman numerals. Double or triple letters,
like ii and iii, get an assignment of ONE char per letter, into the roman array. With
every char assigned, you increment i one more time.
*/
default: printf("\n Error: Amount is %d", amount);
}
}while(amount > 0);
This will shorten your whole code up, tremendously. |
| Adak is offline | |
| | #3 |
| Registered User Join Date: Nov 2009
Posts: 18
| Hi thanks for your guidance. Because our teacher did not teach us "switch" function yet, we r not suppose to use any advance functions etc. I have wrote the source code for this program. A little bit long but still workable. Now i like to add a little bit. User can enter in hexadecimal form and i still be able to display the correct roman character. I think i know how to do it but got a little problem with the very first part that ask user whether they want to enter in Hexadecimal or decimal form. A simple program but my while loop does not break away even the correct 'y' or 'n' is entered. why why why? really don't understand Code: #include<stdio.h>
int romanize(int q[], int base)
{
int counter, temp;
printf("\nThe Romanize number is ");
if(base == 4)
{
// 4 digit
for(counter = 1; counter <= q[0]; counter++)
{
printf("m");
}
// 3 digit more than 5
if(q[1] >= 5)
{
temp = q[1] - 5;
printf("d");
for(counter = 1; counter <= temp; counter++)
{
printf("c");
}
}
// 3 digit less than 5
if(q[1] < 5)
{
for(counter = 1; counter <= q[1]; counter++)
{
printf("c");
}
}
// 2 digit more than 5
if(q[2] >= 5)
{
temp = q[2] - 5;
printf("l");
for(counter = 1; counter <= temp; counter++)
{
printf("x");
}
}
// 2 digit less than 5
if(q[2] < 5)
{
for(counter = 1; counter <= q[2]; counter++)
{
printf("x");
}
}
// 1 digit more than 5
if(q[3] >= 5)
{
temp = q[3] - 5;
printf("v");
for(counter = 1; counter <= temp; counter++)
{
printf("i");
}
}
// 1 digit less than 5
if(q[3] < 5)
{
for(counter = 1; counter <= q[3]; counter++)
{
printf("i");
}
}
}
if(base == 3)
{
// 3 digit more than 5
if(q[0] >= 5)
{
temp = q[0] - 5;
printf("d");
for(counter = 1; counter <= temp; counter++)
{
printf("c");
}
}
// 3 digit less than 5
if(q[0] < 5)
{
for(counter = 1; counter <= q[0]; counter++)
{
printf("c");
}
}
// 2 digit more than 5
if(q[1] >= 5)
{
temp = q[1] - 5;
printf("l");
for(counter = 1; counter <= temp; counter++)
{
printf("x");
}
}
// 2 digit less than 5
if(q[1] < 5)
{
for(counter = 1; counter <= q[1]; counter++)
{
printf("x");
}
}
// 1 digit more than 5
if(q[2] >= 5)
{
temp = q[2] - 5;
printf("v");
for(counter = 1; counter <= temp; counter++)
{
printf("i");
}
}
// 1 digit less than 5
if(q[2] < 5)
{
for(counter = 1; counter <= q[2]; counter++)
{
printf("i");
}
}
}
if(base == 2)
{
// 2 digit more than 5
if(q[0] >= 5)
{
temp = q[0] - 5;
printf("l\n");
for(counter = 1; counter <= temp; counter++)
{
printf("x");
}
}
// 2 digit less than 5
if(q[0] < 5)
{
for(counter = 1; counter <= q[0]; counter++)
{
printf("x");
}
}
// 1 digit more than 5
if(q[1] >= 5)
{
temp = q[1] - 5;
printf("v");
for(counter = 1; counter <= temp; counter++)
{
printf("i");
}
}
// 1 digit less than 5
if(q[1] < 5)
{
for(counter = 1; counter <= q[1]; counter++)
{
printf("i");
}
}
}
if(base == 1)
{
// 1 digit more than 5
if(q[0] >= 5)
{
temp = q[0] - 5;
printf("v");
for(counter = 1; counter <= temp; counter++)
{
printf("i");
}
}
// 1 digit less than 5
if(q[0] < 5)
{
for(counter = 1; counter <= q[0]; counter++)
{
printf("i");
}
}
}
}
main()
{
int i, n, ch, count, hexadecimal, decimal, number[10];
int hex;
count=0, i=0;
//Ask user whether to input in hexadecimal form or not
printf("Do you want to input in Hexdecimal? Y/N : ");
hex = getchar();
while( hex != 'y' || hex != 'n'|| hex != 'Y' || hex != 'N')
{
printf("Please enter Y or N only!\n");
hex = getchar();
}
//--------------------------------------------------------
//if user input 'Y' or 'y' then proceed with the following
if(hex == 'Y' || hex == 'y')
{
printf("Please enter in hexadecimal form : \n");
hexadecimal = getchar();
while(hexadecimal != '\n')
{
if('0' <= hexadecimal && hexadecimal <= '9')
{
decimal = decimal * 16;
decimal = decimal + (hexadecimal - '0');
}
else if('A' <= hexadecimal && hexadecimal <= 'F')
{
decimal = decimal * 16;
decimal = decimal + (hexadecimal - 'A')+10;
}
else if('a' <= hexadecimal && hexadecimal <= 'f')
{
decimal = decimal * 16;
decimal = decimal + (hexadecimal - 'a')+10;
}
else
{
break;
}
count++;
i++;
hexadecimal = getchar();
}
printf("The decimal for hexadecimal is : %d\n",decimal);
}
else
{
while(ch!='\n')
{
if('0' <= ch && ch <= '9')
{
//This is to count the number of digit the user has enter//
n = 0;
n = n * 10;
n = n + (ch - '0');
number[i] = n; //Store each entered digit into array "intnumber[]"//
count++;
i++;
}
ch = getchar();
}
if(count < 5 ) // Handle number up to 9999 only
{
romanize(number, count);
}
else
printf("\nPlease enter number up to 9999 only.\n");
}
}
|
| spurs01 is offline | |
![]() |
| Tags |
| character, displaying, roman |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| sequential file program | needhelpbad | C Programming | 80 | 06-08-2008 01:04 PM |
| Interpreter.c | moussa | C Programming | 4 | 05-28-2008 05:59 PM |
| get wide character and multibyte character value | George2 | C++ Programming | 27 | 01-27-2008 05:10 AM |
| Convert to Roman Numerals - Program not working, suggest a workaround! | duffmckagan | C Programming | 3 | 08-21-2006 06:42 AM |
| Using loops for check a roman number input. | eryell | C++ Programming | 9 | 04-12-2006 11:04 AM |