2016-11-03 2 views
-3

私はこのプログラムを解決しようとしましたが、その間に詰まっていました。 この操作を実行するために、このコードをより価値のあるものにする必要があります。 ありがとうございます。2つのローマ数字を入力してC#を使用して3番目の数学演算を実行する方法は?

ここに私のコードです。

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; 
       } 
      } 
     } 
    } 

皆さんからの貴重な回答を楽しみにしています。

あなたは

+0

これは私が「モーラvalnurable」このコードを作成する方法を知っている...も行いませんhttp://stackoverflow.com/questions/7040289/converting-integers-to-roman-numerals – user6594294

+1

役立つかもしれません"mora valnurable"という用語は理解できない英語であるため、誰でもあなたの質問を改訂してください。 – spender

+0

@Qadeerこれも試しました。しかし、新生児が適切にそれを得ることができないように。これであなたが私を助けることは可能でしょうか? –

答えて

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; 
      } 
     } 
    } 
} 
関連する問題