我试图解决这个问题,但我陷入了困境之中。 我应该如何使这个代码更有可能使它执行此操作。 谢谢。
这是我的代码。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MS_Test
{
class Program
{
static Rule[] Rules = new Rule[]
{
new Rule(1000, "M"),
new Rule(900, "CM"),
new Rule(500, "D"),
new Rule(400, "CD"),
new Rule(100, "C"),
new Rule(90, "XC"),
new Rule(50, "L"),
new Rule(40, "XL"),
new Rule(10, "X"),
new Rule(9, "IX"),
new Rule(5, "V"),
new Rule(4, "IV"),
new Rule(1, "I"),
};
static void Main(string[] args)
{
// Let the user convert numbers until they close the program
Console.WriteLine("Enter a positive integer:");
int n1;
if (Int32.TryParse(Console.ReadLine(), out n1) && n1 > 0)
Console.WriteLine("{0} in roman numerals is: {1}", n1, Romanise(n1)); // Write out the result
else
Console.WriteLine("That's not a positive integer");
Console.WriteLine("Enter another positive integer:");
int n2;
if (Int32.TryParse(Console.ReadLine(), out n2) && n2 > 0)
Console.WriteLine("{0} in roman numerals is: {1}", n2, Romanise(n2)); // Write out the result
else
Console.WriteLine("That's not a positive integer");
Console.ReadLine();
}
private static string Romanise(int n)
{
if (n == 0) return ""; // Recursion termination
foreach (var rule in Rules) // Rules are in descending order
if (rule.N <= n)
return rule.Symbol + Romanise(n - rule.N); // Recurse
// If this line is reached then n < 0
throw new ArgumentOutOfRangeException("n must be greater than or equal to 0");
}
// Represents a substitution rule for a roman-numerals like numerical system
// Number 'N' is equivilant to string 'Symbol' in the system.
class Rule
{
public int N { get; set; }
public string Symbol { get; set; }
public Rule(int n, string symbol)
{
N = n;
Symbol = symbol;
}
}
}
}
期待您和所有人的有价值的答案,#Geeks&#34;
谢谢
答案 0 :(得分:0)
您必须提供有效罗马数字。它会起作用。
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MS_Test
{
class Program
{
static Rule[] Rules = new Rule[]
{
new Rule(1000, "M"),
new Rule(900, "CM"),
new Rule(500, "D"),
new Rule(400, "CD"),
new Rule(100, "C"),
new Rule(90, "XC"),
new Rule(50, "L"),
new Rule(40, "XL"),
new Rule(10, "X"),
new Rule(9, "IX"),
new Rule(5, "V"),
new Rule(4, "IV"),
new Rule(1, "I"),
};
static void Main(string[] args)
{
Console.WriteLine("Roman Number First: ");
string firstNumber = Console.ReadLine();
Console.WriteLine("Roman Number Second: ");
string secondNumber = Console.ReadLine();
int result = RomanToInteger(firstNumber) + RomanToInteger(secondNumber);
Console.WriteLine(Romanise(result));
Console.ReadLine();
}
private static Dictionary<char, int> RomanMap = new Dictionary<char, int>()
{
{'I', 1},
{'V', 5},
{'X', 10},
{'L', 50},
{'C', 100},
{'D', 500},
{'M', 1000}
};
public static int RomanToInteger(string roman)
{
int number = 0;
for (int i = 0; i < roman.Length; i++)
{
if (i + 1 < roman.Length && RomanMap[roman[i]] < RomanMap[roman[i + 1]])
{
number -= RomanMap[roman[i]];
}
else
{
number += RomanMap[roman[i]];
}
}
return number;
}
private static string Romanise(int n)
{
if (n == 0) return ""; // Recursion termination
foreach (var rule in Rules) // Rules are in descending order
if (rule.N <= n)
return rule.Symbol + Romanise(n - rule.N); // Recurse
// If this line is reached then n < 0
throw new ArgumentOutOfRangeException("n must be greater than or equal to 0");
}
// Represents a substitution rule for a roman-numerals like numerical system
// Number 'N' is equivilant to string 'Symbol' in the system.
class Rule
{
public int N { get; set; }
public string Symbol { get; set; }
public Rule(int n, string symbol)
{
N = n;
Symbol = symbol;
}
}
}
}