Roman Numeral Program :: Forum

Start Date Last Update Igniter Last Update By Status
7:58pm on Friday 12th July 2013 11 years ago

sinha2366

sinha2366




sinha2366

So here's what I needed help with - 
I need to create a code that converts numbers to Roman numerals. I found a source code online that does that but I don't know what happening. Could you tell me what's happening in this code in detail? I mean every line of it: How is it looping? What other things its using and why...

Instead of coping the code, I want to actually learn as to how it's working. 

Here it is: 

http://www.dreamincode.net/forums/topic/117017-arabicroman-numeral-converter/

While viewing a code, I simpy have too many questions and since there's no one to answer them, I get very confused. 
I hope you understand. 


Thank you very much for your help!

Aditya


#include
#include

std::string Roman(int arabic);
int NumOfDigits(int number);
int FirstDigitOf(int num);
int Power(int base, int exponent);

int main()
{
char answer = 'y';
while (answer == 'y')
{
int number;
std::cout << "Type in the number you want to convert into Roman numerals: ";
while (true)
{
std::cin >> number;
// if number < 1 or number > 3999 (biggest possible Roman numeral), ask again
if ((number > 0) && (number < 4000)) break;
std::cout << "Please give a number between 1 and 3999: ";
}

std::cout << Roman(number);

std::cout << "nDo you want to convert another number? (y/n) ";
std::cin >> answer;
std::cout << "______________nn";

} //end of while (answer == 'y')
} // END OF INT MAIN()

std::string Roman(int arabic)
{
// check if number < 1 or number > 3999 (biggest possible Roman numeral)
if ((arabic < 1) || (arabic > 3999)) return ("Please give a number between 1 and 3999.");

std::string roman;
while (arabic != 0)
{
int digit_num = NumOfDigits(arabic);
int next_digit = FirstDigitOf(arabic); // next digit to be converted; conversion goes left to right

// substract the first digit (the one that's now held by 'nect_digit') from the Arabic number
arabic -= next_digit * Power(10, (digit_num - 1)); // e.g. 3450 = 3450 - (3 * (10 ^ 3)) => 450

// use appropriate Roman digits
char one, five;
if (digit_num == 4) { one = 'M'; }
else if (digit_num == 3) { one = 'C'; five = 'D'; }
else if (digit_num == 2) { one = 'X'; five = 'L'; }
else if (digit_num == 1) { one = 'I'; five = 'V'; }

// convert to Roman numeral
if (next_digit <= 3)
{
while (next_digit != 0)
{
roman += one;
next_digit--;
}
}
else if (next_digit == 4)
{
roman = roman + one + five;
}
else if (next_digit == 9)
{
roman += one;
if (digit_num == 3) roman += 'M';
else if (digit_num == 2) roman += 'C';
else if (digit_num == 1) roman += 'X';
}
else if ((next_digit <= 8) && (next_digit > 4))
{
roman += five;
while (next_digit != 5)
{
roman += one;
next_digit--;
}
}

} //end of while (arabic != 0)

return roman;
}

int NumOfDigits(int number) {
int digits = 0;
while (number != 0)
{
digits++;
number /= 10;
}
return digits;
}

int FirstDigitOf(int number)
{
// returns the first digit from he LEFT of the number
return (number / Power(10, (NumOfDigits(number) - 1)));
}

int Power(int base, int exponent)
{
int result = 1;
while (exponent != 0)
{
result = result * base;
exponent--;
}
return result;
}




suzzett

I am currently looking into the code. I will update here after that.



Thank you for posting.





sinha2366

Thank you for your assistance!





sinha2366

Thank you for your assistance!





suzzett

I have prepared a video about this program. It is currently rendering I will be uploading it soon.




suzzett

The video is up with explanation. Let me know if that helps.

Basic C++ Program Example with Explanation. Arabic to Roman numeral converter




sinha2366

Thank you! That video helps a lot!

Two questions:

1) How could you manipulate the program so that if the user enters a Roman Numeral, the program converts it to a number and vice versa? In other words, the program would be smart enough to recognize the input and act accordingly.



2) How could you have different inputs for the loops? For ex, if the user types any word with a 'n' in front of it (usually, no, nah, nope) the program wouldn't loop. If there was any other reply, such as 'y', yes, yeah, yah etc, then the program WOULD loop.

Thanks a lot for your help!

Aditya

 




suzzett

What you are talking about is basically, a good question for something like an string manipulation.

I need to put some practice exercise so that everyone will be able to think about it.

In my recent video I have talked about character sequence and talked about manipulating something like a first or any character in given input. You can have a look into that.

A good strength in string manipulation in programming is a huge plus in your knowledge.

Also, have a look into cstring library in C++ and its functions and methods(I will talk about methods in videos probably this week).

Regarding your first question:

This program is solely designed to take in an integer and output an string. To make it do what you are asking has its own complexity level with bigger issues. After you know string manipulation as I have mentioned, you will then have some idea on how to approach this.




sinha2366

Alright, thank you.

I'll take a look into that.

Aditya


Please login to post your reply!
 

Login

Username:
Password:
Don't have an account?
Register here
Register

Subscribe Youtube



Network DigitalStage.org


Some interesting stuffs