Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text.RegularExpressions;
- namespace RomanNumberToDecimals
- {
- internal class Program
- {
- static void Main(string[] args)
- {
- Console.WriteLine("Irj be egy római számot");
- string numb = Console.ReadLine();
- string pattern = @"[IVXLCDM]";
- Regex r = new Regex(pattern);
- if (r.IsMatch(numb))
- {
- Console.WriteLine(RomanToInt(numb));
- }
- else
- {
- Console.WriteLine("Nem római szám");
- }
- Console.ReadKey();
- }
- // ez a függvény nem saját!
- public static int RomanToInt(string s)
- {
- int sum = 0;
- Dictionary<char, int> romanNumbersDictionary = new()
- {
- { 'I', 1 },
- { 'V', 5 },
- { 'X', 10 },
- { 'L', 50 },
- { 'C', 100 },
- { 'D', 500 },
- { 'M', 1000 }
- };
- for (int i = 0; i < s.Length; i++)
- {
- char currentRomanChar = s[i];
- romanNumbersDictionary.TryGetValue(currentRomanChar, out int num);
- if (i + 1 < s.Length && romanNumbersDictionary[s[i + 1]] > romanNumbersDictionary[currentRomanChar])
- {
- sum -= num;
- }
- else
- {
- sum += num;
- }
- }
- return sum;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement