Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Roman to Arabic number conversion without semantic control
//Title of this code //Rextester.Program.Main is the entry point for your code. Don't change it. //Compiler version 4.0.30319.17929 for Microsoft (R) .NET Framework 4.5 using System; using System.Collections.Generic; using System.Linq; using System.Text.RegularExpressions; namespace Rextester { public class Program { public static void Main(string[] args) { Console.WriteLine(r2a("")); Console.WriteLine(r2a("i")); Console.WriteLine(r2a("ii")); Console.WriteLine(r2a("iii")); Console.WriteLine(r2a("iv")); Console.WriteLine(r2a("v")); Console.WriteLine(r2a("vi")); Console.WriteLine(r2a("vii")); Console.WriteLine(r2a("viii")); Console.WriteLine(r2a("ix")); Console.WriteLine(r2a("x")); Console.WriteLine(r2a("xi")); Console.WriteLine(r2a("xii")); Console.WriteLine(r2a("xiii")); Console.WriteLine(r2a("xiv")); Console.WriteLine(r2a("xv")); Console.WriteLine(r2a("xvi")); Console.WriteLine(r2a("xvii")); Console.WriteLine(r2a("xviii")); Console.WriteLine(r2a("xix")); Console.WriteLine(r2a("xx")); Console.WriteLine(r2a("xxxiv")); Console.WriteLine(r2a("pippoxivei")); Console.WriteLine(r2a("xxvii") + "/" + r2a("viii") + "/" + r2a("mcmlxxiv")); } public static int r2a(string roman) { int total = 0; int[] arabic = null; List<int> list = new List<int>(); Dictionary<char, int> dic = new Dictionary<char, int>() { {'I', 1}, {'V', 5}, {'X', 10}, {'L', 50}, {'C', 100}, {'D', 500}, {'M', 1000} }; if(string.IsNullOrEmpty(roman)) return 0; foreach(char c in roman.ToUpper()) if(dic.ContainsKey(c)) list.Add(dic[c]); arabic = list.ToArray(); for(int x = 0; x < arabic.Length - 1; x++) { if(arabic[x] < arabic[x + 1]) { arabic[x] = arabic[x + 1] - arabic[x]; arabic[x + 1] = 0; x += 2; } } foreach(int val in arabic) total += val; return total; } } }
run
|
edit
|
history
|
help
0
Problem: fb_series
Encryption & Decryption AES C#
sample
TestCodec1
Live tv 2021
C# programming exercise 2
Testing 008
Explicit Interface Examplle
Unix timestamp to DateTime
code1