2016-11-03 1 views
-3

J'ai essayé de résoudre ce programme mais j'étais coincé entre ça. Comment devrais-je rendre ce code plus valnurable pour le faire effectuer cette opération. Merci.Comment entrer deux chiffres romains pour effectuer une opération mathématique qui donne le troisième en utilisant C#?

Voici mon code.

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

Dans l'attente de réponses valables de vous tous "Geeks".

Merci

+0

cela peut aider http://stackoverflow.com/questions/7040289/converting-integers-to-roman-numerals – user6594294

+1

Je ne sais pas comment faire ce code « mora valnurable » ... ni ne quelqu'un d'autre parce que le terme "mora valnurable" n'est pas compréhensible anglais. Veuillez réviser votre question. – spender

+0

@Qadeer J'ai essayé de cela aussi. Mais comme un newbee incapable de l'obtenir correctement. Est-il possible pour vous de m'aider pour ça? –

Répondre

0

Vous devrez fournir valide numéro romain. ça va marcher.

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