Roman ad Arabic numerus racio et vice versa

Conversion of numbers from Arabic to Roman number system and vice versa

Program to convert numbers from Roman to Arabic and vice versa number system was written as "Windows Form" application in Sharp Develop IDE, programming language C#, and for his work is essential .Net framework 2.0 or higher. Graphical user interface (GUI) is intuitive and consists of two text boxes for entering numbers to convert , buttons with possible digits of number systems, for selecting cipher when entering numbers , and delete buttons. Max value for conversion is 3999. If you want to download program click the picture of program screen shot.

Table of contents :

Introduction

Mathematics is always interesting scientific discipline with its own problems which are solved by computer. "Conversion of numbers from Arabic to Roman number system and vice versa" is one of the subjects that can be used for learning how to develop an algorithm and computer program for solving this and other similar problems.

Since I comply with a general method of solving problems with computers and this problem will be solved through specific steps.

Step one

Complete overview and understanding of the process for which you want to create a computer program, or write a Pseudo code.

Step two

Writing the Algorithm based on a pseudo code.

Step three

Coding on the basis of the algorithm, in some of the programming language that is most suitable for this type of program.

Step four

Testing and debugging program.

Step five

Writing documentation for the program, instruction, license agreement and patenting of software.

Solving problem

Step one

Research and a thorough understanding of the process of conversion of numbers from Roman to Arabic number system.

Searching all over the internet for information on how to convert number from Roman to Arabic number system and vice versa, I have found many interesting links and articles relating to the subject. What is most important , all of them explain process of conversion trough simple mathematical examples, understandable to anyone who have basic knowledge in mathematic. I have chosen only one article on wich I have based my research and development of Pseudo code. and that is :

“Roman Numerals” , that explains everything about Roman numbers (Symbols , History , etc.),

Analise this article thoroughly. Here is extracted the essence that is needed for problem solving.

Conversion from Roman number system to Arabic number system :

Today we use only seven symbols from Roman numeral system for representing Arabic numbers :

1 = I

2 = II

3 = III

5 = V

10 = X

50 = L

100 = C

500 = D

1000 = M

Symbols for larger numbers exists but this solution is limited to max value of 3999.

Symbols that reperesent Arabic numbers in Roman numeral system are :

Symbol for zero do not exists

1 = I

2 = II

3 = III

4 = IV

5 = V

6 = VI

7 = VII

8 = VIII

9 = IX

10 = X

So the conversion from Arabic to Roman is simple .

Break Arabic number into numbers of thousands, hundreds, tens , ones and then replace the values of digits with

it's representation in Roman numerals.

Follow these rules :

“I “, “X” , “C” and “M” can be repeated only three times sequentially.

“V”,”L”, and “D” can never be repeated sequentially

“I” can be subtracted only from “V“ and “X”.

“X” can be subtracted only from “L” and “C”.

“C” can be subtracted only from “D” and “M”.

“V” , “L” and “D” can never be subtracted.

Only One small value can be subtracted from large one.

Notice that because of these rules the max value for conversion in this progam is limited to 3999. So we have only 3999 possible Roman numerals to convert.

Example:

3999 = 3000 + 900 + 90 + 9 = MMM + CM + XC + IX = MMMCMXCIX

3000 = M + M + M = MMM

900 = M – C = CM

90 = C – X = XC

9 = X – I = IX

Since the symbol for zero do not exists you want need to convert it.

Example:

2005 = 2000 + 5 = MM + V = MMV

2000 = M + M = MM

5 = V = V

More examples :

Arabic Number............................................... Roman Number

2 = 1 + 1...............................................................I + I = II

4 = 5 – 1..............................................................V - I = IV

7 = 5 + 1 + 1........................................................ V + I + I = VII

9 = 10 – 1............................................................X - I = IX

57 = 50 + 7..........................................................L + VII = LVII

172 = 100 + 70 + 2 .............................................C + LXX + II = CLXXII

583 = 500 + 80 + 3 ............................................DLXXXIII

1258 = 1000 + 200 + 50 + 8...............................MCCLVII

2999 = MMCMXCIX

Pseudo code for program for converting numbers goes like this :

    1. Start program

    2. Enter number you want to convert ( by using buttons with number symbols )

    3. Find appropriate representation of number value in other number system

    4. Show result

    5. End program

Since the rules are too complex to be turned into algorithm and program, too many energy and time would be spend for coding "Parser" for such small number of Roman numerals that can be written with these rules, only 3999, and my rule is that “Simplicity is the Esence of Life”, it is easier to write into program it self all appropriate representation of number values that are needed for conversion and just make a query. That means that I shall manually input all possible representations of numbers for conversion into program.

Complexity of rules do not necessarily means that implementation throughout algorithm is not possible . I have simply decided that since the limitation of maximum value for conversion is 3999, I do not need that kind of problem solving. Also this way the functionality of GUI comes to first place , it prevents the user from making a mistake while entering a number for conversion and conversion is immediate after choosing number to convert. Look at the program code.

Step two

Writing the algorithm based on a pseudo code.

When you finish writing pseudo code for program it is easy to write Algorithm. Since the program shall be written as a Windows Form application with a graphical user interface that uses many different controls, and the algorithm is suited to this type of work environment.

In program we use :

Text Box 1 control (in program Roman ) for entering Roman number to convert and showing result for conversion from Arabic to Roman. Buttons (in program Roman Number One trough Thousand) that represent Roman numbers , for selecting Roman digits and Button for deleting Roman number.

Text Box 2 control ( in program Arabic ) for entering Arabic number to convert and showing result for conversion from Roman to Arabic. Buttons (in program Arabic Number Zero trough Nine ) that represent Arabic numbers , for selecting Arabic digitsand Button for deleting Arabic number.

There are many subroutines that shall be used in program and the main are :

Button Click subroutines that are related to, controlling entered number value representations, and deleting entered values Select Roman number, and Select Arabic number, for finding appropriate representation of number values to.

Other subroutines are not explained but they are simple and understandable simply by looking the program code.

With those controls and subroutines we create whole program based on the pseudo code.

Each of this controls have its own event handler that is used in program and its own algorithm accordingly.

Since this article relates to solving problem from fifth grade math., I shall not present algorithms here , they are too simple , and it is possible to write them simply by looking the program code.

Step three

Coding on the basis of the algorithm, in some of the programming language that is most suitable for this type of program.

Here you can see the part of program for conversion , that is explained trough pseudo code. Reading the program code that was written by some other programmer could be difficult, and usually hard for understanding , especially if it is only the fragments as shown below. So it is recommended to see complete code , download full solution for IDE Sharp Develop 4.1 with open sorce code written in C#. There you can see complete code for program.

/*

* Created in IDE SharpDevelop , C#

* User: Perić Željko

* Date: 02.03.2012

* Time: 18:49

*

* Program for conversion of Roman numbers to Arabic and vice versa

*/

using System;

using System.Collections.Generic;

using System.Drawing;

using System.Windows.Forms;

namespace Roman_ad_Arabic_numerus_ratio_et_vice_versa

{

/// <summary>

/// Description of MainForm.

/// </summary>

public partial class MainForm : Form

{

//

// Declaration of variables

//

public int RomanLenght = 0;

public int ArabicLenght = 0;

public bool IsSelected = false;

public MainForm()

{

//

// The InitializeComponent() call is required for Windows Forms designer support.

//

InitializeComponent();

}

void RomanNumberOne(object sender, EventArgs e)

{

Roman.Text = Roman.Text + "I";

CheckRomanLenght();

SelectArabicNumber();

}

void RomanNumberFive(object sender, EventArgs e)

{

Roman.Text = Roman.Text + "V";

CheckRomanLenght();

SelectArabicNumber();

}

void RomanNumberTen(object sender, EventArgs e)

{

Roman.Text = Roman.Text + "X";

CheckRomanLenght();

SelectArabicNumber();

}

void RomanNumberFifty(object sender, EventArgs e)

{

Roman.Text = Roman.Text + "L";

CheckRomanLenght();

SelectArabicNumber();

}

void RomanNumberHundred(object sender, EventArgs e)

{

Roman.Text = Roman.Text + "C";

CheckRomanLenght();

SelectArabicNumber();

}

void RomanNumberFiveHundred(object sender, EventArgs e)

{

Roman.Text = Roman.Text + "D";

CheckRomanLenght();

SelectArabicNumber();

}

void RomanNumberThousand(object sender, EventArgs e)

{

Roman.Text = Roman.Text + "M";

CheckRomanLenght();

SelectArabicNumber();

}

void DeleteRoman(object sender, EventArgs e)

{

RomanLenght = RomanLenght - 1;

if ( RomanLenght <= 0 )

{

RomanLenght = 0;

Roman.Text = Roman.Text.Substring(0,RomanLenght);

Roman.Refresh();

ArabicEnable();

Arabic.Text = "";

Arabic.Refresh();

}

else

{

Roman.Text = Roman.Text.Substring(0,RomanLenght);

Roman.Refresh();

SelectArabicNumber();

Arabic.Refresh();

}

}

void ArabicNumberZero(object sender, EventArgs e)

{

Arabic.Text = Arabic.Text + "0";

CheckArabicLenght();

SelectRomanNumber();

}

void ArabicNumberOne(object sender, EventArgs e)

{

Arabic.Text = Arabic.Text + "1";

CheckArabicLenght();

SelectRomanNumber();

}

void ArabicNumberTwo(object sender, EventArgs e)

{

Arabic.Text = Arabic.Text + "2";

CheckArabicLenght();

SelectRomanNumber();

}

void ArabicNumberThree(object sender, EventArgs e)

{

Arabic.Text = Arabic.Text + "3";

CheckArabicLenght();

SelectRomanNumber();

}

void ArabicNumberFour(object sender, EventArgs e)

{

Arabic.Text = Arabic.Text + "4";

CheckArabicLenght();

SelectRomanNumber();

}

void ArabicNumberFive(object sender, EventArgs e)

{

Arabic.Text = Arabic.Text + "5";

CheckArabicLenght();

SelectRomanNumber();

}

void ArabicNumberSix(object sender, EventArgs e)

{

Arabic.Text = Arabic.Text + "6";

CheckArabicLenght();

SelectRomanNumber();

}

void ArabicNumberSeven(object sender, EventArgs e)

{

Arabic.Text = Arabic.Text + "7";

CheckArabicLenght();

SelectRomanNumber();

}

void ArabicNumberEight(object sender, EventArgs e)

{

Arabic.Text = Arabic.Text + "8";

CheckArabicLenght();

SelectRomanNumber();

}

void ArabicNumberNine(object sender, EventArgs e)

{

Arabic.Text = Arabic.Text + "9";

CheckArabicLenght();

SelectRomanNumber();

}

void DeleteArabic(object sender, EventArgs e)

{

ArabicLenght = ArabicLenght - 1;

if ( ArabicLenght <=0 )

{

ArabicLenght = 0;

Arabic.Text = Arabic.Text.Substring(0,ArabicLenght);

Arabic.Refresh();

button16.Enabled = false;

RomanEnable();

Roman.Text ="";

Roman.Refresh();

}

else

{

Arabic.Text = Arabic.Text.Substring(0,ArabicLenght);

Arabic.Refresh();

SelectRomanNumber();

Roman.Refresh();

}

}

void RomanEnable()

{

button1.Enabled = true;

button2.Enabled = true;

button3.Enabled = true;

button4.Enabled = true;

button5.Enabled = true;

button6.Enabled = true;

button7.Enabled = true;

button8.Enabled = true;

}

void RomanDisable()

{

button1.Enabled = false;

button2.Enabled = false;

button3.Enabled = false;

button4.Enabled = false;

button5.Enabled = false;

button6.Enabled = false;

button7.Enabled = false;

button8.Enabled = false;

}

void ArabicEnable()

{

button9.Enabled = true;

button10.Enabled = true;

button11.Enabled = true;

button12.Enabled = true;

button13.Enabled = true;

button14.Enabled = true;

button15.Enabled = true;

button16.Enabled = false;

button17.Enabled = true;

button18.Enabled = true;

button19.Enabled = true;

}

void ArabicDisable()

{

button9.Enabled = false;

button10.Enabled = false;

button11.Enabled = false;

button12.Enabled = false;

button13.Enabled = false;

button14.Enabled = false;

button15.Enabled = false;

button16.Enabled = false;

button17.Enabled = false;

button18.Enabled = false;

button19.Enabled = false;

}

void CheckArabicLenght()

{

ArabicLenght = ArabicLenght + 1;

if ( ArabicLenght > 0 )

{

button16.Enabled = true;

RomanDisable();

}

if (ArabicLenght > 4)

{

ArabicLenght = 4;

Arabic.Text = Arabic.Text.Substring(0,ArabicLenght);

}

Arabic.Refresh();

}

void CheckRomanLenght()

{

RomanLenght = RomanLenght + 1;

if (RomanLenght >0)

{

ArabicDisable();

}

if (RomanLenght > 15)

{

RomanLenght = 15;

Roman.Text = Roman.Text.Substring(0,RomanLenght);

}

Roman.Refresh();

}

void SelectArabicNumber()

{

IsSelected = false;

if ( Roman.Text != "" )

{

switch (Roman.Text)

{

case "I": Arabic.Text = "1";IsSelected = true;

break;

case "II": Arabic.Text = "2";IsSelected = true;

break;

case "III": Arabic.Text = "3";IsSelected = true;

break;

case "IV": Arabic.Text = "4";IsSelected = true;

break;

case "V": Arabic.Text = "5";IsSelected = true;

break;

case "VI": Arabic.Text = "6";IsSelected = true;

break;

case "VII": Arabic.Text = "7";IsSelected = true;

break;

case "VIII": Arabic.Text = "8";IsSelected = true;

break;

case "IX": Arabic.Text = "9";IsSelected = true;

break;

case "X": Arabic.Text = "10";IsSelected = true;

break;

case "XI": Arabic.Text = "11";IsSelected = true;

break;

case "XII": Arabic.Text = "12";IsSelected = true;

break;

case "XIII": Arabic.Text = "13";IsSelected = true;

break;

case "XIV": Arabic.Text = "14";IsSelected = true;

break;

case "XV": Arabic.Text = "15";IsSelected = true;

break;

case "XVI": Arabic.Text = "16";IsSelected = true;

break;

case "XVII": Arabic.Text = "17";IsSelected = true;

break;

case "XVIII": Arabic.Text = "18";IsSelected = true;

break;

case "XIX": Arabic.Text = "19";IsSelected = true;

break;

case "XX": Arabic.Text = "20";IsSelected = true;

break;

.........................................................................................................

............. and so on to the end ......................................

.........................................................................................................

case "MMMCMXCVIII": Arabic.Text = "3998";IsSelected = true;

break;

case "MMMCMXCIX": Arabic.Text = "3999";IsSelected = true;

break;

}

if (IsSelected == false)

{

RomanLenght = RomanLenght - 1;

Roman.Text = Roman.Text.Substring(0,RomanLenght);

}

}

}

void SelectRomanNumber()

{

IsSelected = false;

if ( Arabic.Text != "" )

{

switch (Arabic.Text)

{

case "1": Roman.Text = "I";IsSelected = true;

break;

case "2": Roman.Text = "II";IsSelected = true;

break;

case "3": Roman.Text = "III";IsSelected = true;

break;

case "4": Roman.Text = "IV";IsSelected = true;

break;

case "5": Roman.Text = "V";IsSelected = true;

break;

case "6": Roman.Text = "VI";IsSelected = true;

break;

case "7": Roman.Text = "VII";IsSelected = true;

break;

case "8": Roman.Text = "VIII";IsSelected = true;

break;

case "9": Roman.Text = "IX";IsSelected = true;

break;

case "10": Roman.Text = "X";IsSelected = true;

break;

case "11": Roman.Text = "XI";IsSelected = true;

break;

case "12": Roman.Text = "XII";IsSelected = true;

break;

case "13": Roman.Text = "XIII";IsSelected = true;

break;

case "14": Roman.Text = "XIV";IsSelected = true;

break;

case "15": Roman.Text = "XV";IsSelected = true;

break;

case "16": Roman.Text = "XVI";IsSelected = true;

break;

case "17": Roman.Text = "XVII";IsSelected = true;

break;

case "18": Roman.Text = "XVIII";IsSelected = true;

break;

case "19": Roman.Text = "XIX";IsSelected = true;

break;

case "20": Roman.Text = "XX";IsSelected = true;

break;

........................................................................................................

............. and so on to the end .........................................

.......................................................................................................

case "3998": Roman.Text = "MMMCMXCVIII";IsSelected = true;

break;

case "3999": Roman.Text = "MMMCMXCIX";IsSelected = true;

break;

}

if (IsSelected == false)

{

ArabicLenght = ArabicLenght - 1;

Arabic.Text = Arabic.Text.Substring(0,ArabicLenght);

}

}

}

}

}

Step four

Testing and debugging program.

General rule for testing program is : “ Never test program by Your self, it should be done by fellow programmer”.

Reason for that is that you usually would not see all errors in code , especially logical type of errors, and it is faster. Debugging program upon found errors is easy. This step should be repeated until no error is found, and program is functioning correct on the basis of beta tester (fellow programer) reports.

Step five

Writing documentation for the program, instruction, license agreement and patenting of software.

Documentation for program is similar to this article , only it should be far more specific in explanation of development of pseudo code, algorithm and program code. Instructions or user manual can be written in many different programs for creating user manuals or depending on complexity of program it can be written as simple *.txt document. There are different types of licence agreement. See the articles on these adreses :

Licence

Licence agreement

Software patent

Points of Interest

Since I am not a member of Code Project for a long time, I have not read many questions of other members , but I have realized that most of the questions was asked by the beginners in programming. Given that each question requires an adequate answer I chose to create this article that should become my general solution to a problem that has been very interesting to me and to other members of Code Project. Also it is intended to be an article from which every beginner can learn something new and interesting in development of algorithms and coding in C#.

History

Last revision 07.05.2012 Author.

Download links

Download full solution for IDE Sharp Develop 4.1 with open sorce code written in C. After downloading solution you can open it in IDE Sharp Develop and Build the release version of program. If you work in other type of IDE ( integrated development environment), you can open all documentation that contains sorce code, and simply copy it into your program.

Download solution with open source code

If you just want to use program you can download it here. After downloading just click on executable file. Important , program needs .Net framework 2.0 or higher to work correctly.

Download program

Licence

This article is public domain. Solution that is developed in IDE Sharp Develop , is covered with GNU LESSER GENERAL PUBLIC LICENSE Version 2.1, February 1999. Executable version of program is free.You can freely copy and distribute it if you want.

Author

All about the autor you can find at this adress.

Home page