TodorMitev

RomanToArabic

Mar 7th, 2012
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.86 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. namespace Task11RomeToArabic
  7. {
  8.     class RomeToDec
  9.     {
  10.         static void Main(string[] args)
  11.         {
  12.             Console.WriteLine("Enter the number in Roman numeral system:");
  13.             string romanNumber = Console.ReadLine();
  14.             int decimalNumber = 0;
  15.             char[] romans = { 'M', 'D', 'C', 'L', 'X', 'V', 'I' };
  16.             int[] arabics = { 1000, 500, 100, 50, 10, 5, 1 };
  17.             for (int i = 0; i < romanNumber.Length-1; i++)
  18.             {
  19.                 int positionInRomans = 0;
  20.                 int positionInRomansNext = 0;
  21.                 for (int j = 0; j < romans.Length; j++)
  22.                 {
  23.                     if (romanNumber[i] == romans[j])
  24.                     {
  25.                         positionInRomans = j;
  26.                     }
  27.                 }
  28.                 for (int j = 0; j < romans.Length; j++)
  29.                 {
  30.                     if (romanNumber[i+1] == romans[j])
  31.                     {
  32.                         positionInRomansNext = j;
  33.                     }
  34.                 }
  35.                 if (positionInRomans <= positionInRomansNext)
  36.                 {
  37.                     decimalNumber += arabics[positionInRomans];
  38.                 }
  39.                 else
  40.                 {
  41.                     decimalNumber -= arabics[positionInRomans];
  42.                 }
  43.             }
  44.             char lastChar = romanNumber[romanNumber.Length - 1];
  45.             for (int i = 0; i < romans.Length; i++)
  46.             {
  47.                 int posLastChar = 0;
  48.                 if (lastChar == romans[i])
  49.                 {
  50.                     posLastChar = i;
  51.                 }
  52.                 decimalNumber += arabics[posLastChar];
  53.             }
  54.             Console.WriteLine(decimalNumber-6000);
  55.         }
  56.     }
  57. }
Advertisement
Add Comment
Please, Sign In to add comment