2016-11-24 5 views
-1

私は
999999999までの任意の数字で動作する数値トランスレータを作成しようとしており、それを文字列表現に変換しています。Number Translator最大999999999語

入力= 999999999 出力=九百九十九億九百九十九千、九百九十九

これを処理するためのより良い方法上の任意のヘルプは理解されるであろう。

私は多くのアイデアをこの前の解決策の を使用しました。

converting numbers in to words C#

public partial class MainWindow : Window 
{ 

    int inputNumber; 



    public MainWindow() 
    { 
     InitializeComponent(); 
    } 

    private void Translate_Click(object sender, RoutedEventArgs e) 
    { 

     if (int.TryParse(txtInput.Text, out inputNumber)) 
     { 
      TranslateNumber(); 
     } 

    } 

    private void TranslateNumber() 
    { 

     var unitsMap = new[] { " ", " One ", " Two ", " Three ", " Four ", " Five ", " Six ", " Seven ", " Eight ", " Nine ", " Ten ", " Eleven ", " Twelve ", " Thirteen ", " Fourteen ", " Fifteen ", " Sixteen ", " Seventeen ", " Eighteen ", " Nineteen " }; 
     var tensMap = new[] { " ", " Ten ", " Twenty ", " Thirty ", " Forty ", " Fifty ", " Sixty ", " Seventy ", " Eighty ", " Ninety " }; 


     if (inputNumber == 0) 
     { 
      tBlkOutput.Text += "zero"; 
     } 




     if ((inputNumber/100000000) > 0) 
     { 
      // needs a number betwwen 1 and 9 
      tBlkOutput.Text += unitsMap[inputNumber/100000000] + " Hundred Million "; 
      inputNumber %= 100000000; 
     } 

     if ((inputNumber/1000000) > 0) 
     { 
      // need to be able to list between 1 million and 99 million 
      if ((inputNumber/10000000) > 0) 
      { 
       tBlkOutput.Text += tensMap[inputNumber/10000000]; 
       inputNumber %= 10000000; 
      } 
      if ((inputNumber/1000000) > 0) 
      { 
       tBlkOutput.Text += unitsMap[inputNumber/1000000]; 
       inputNumber %= 1000000; 
      } 

      tBlkOutput.Text += " Million "; 
     } 
     if ((inputNumber/100000) > 0) 
     { 
      // needs a number betwwen 1 and 9 
      tBlkOutput.Text += unitsMap[inputNumber/1000000] + " Hundred Thousand "; 
      inputNumber %= 1000000; 
     } 

     if ((inputNumber/10000) > 0) 
     { 
      if ((inputNumber/100000) > 0) 
      { 
       tBlkOutput.Text += tensMap[inputNumber/10000]; 
       inputNumber %= 10000; 
      } 
      if ((inputNumber/1000) > 0) 
      { 
       tBlkOutput.Text += unitsMap[inputNumber/1000]; 
       inputNumber %= 1000; 
      } 
      tBlkOutput.Text += " Thousand "; 
     } 

     if ((inputNumber/100) > 0) 
     { 
      tBlkOutput.Text += unitsMap[inputNumber/100] + " Hundred and "; 
      inputNumber %= 100; 
     } 
     if ((inputNumber/10) > 0) 
     { 
      tBlkOutput.Text += tensMap[inputNumber/10]; 
      inputNumber %= 10; 
     } 
     if (inputNumber > 0) 
     { 
      tBlkOutput.Text += unitsMap[inputNumber]; 
     } 



    } 

    private void Reset_Click(object sender, RoutedEventArgs e) 
    { 
     tBlkOutput.Text = ""; 
     inputNumber = 0; 
     txtInput.Text = ""; 
     txtInput.Focus(); 
    } 
} 

答えて

0

私は、彼らがために必要な、今ではほとんどの部分のために働くの文場合、一部を入れて、私が間違って行っていた場所を考え出しました。

public partial class MainWindow : Window 
{ 

    int inputNumber; 



    public MainWindow() 
    { 
     InitializeComponent(); 
    } 

    private void Translate_Click(object sender, RoutedEventArgs e) 
    { 

     if (int.TryParse(txtInput.Text, out inputNumber)) 
     { 
      TranslateNumber(); 
     } 

    } 

    private void TranslateNumber() 
    { 

     var unitsMap = new[] { " ", " One ", " Two ", " Three ", " Four ", " Five ", " Six ", " Seven ", " Eight ", " Nine ", " Ten ", " Eleven ", " Twelve ", " Thirteen ", " Fourteen ", " Fifteen ", " Sixteen ", " Seventeen ", " Eighteen ", " Nineteen " }; 
     var tensMap = new[] { " ", " Ten ", " Twenty ", " Thirty ", " Forty ", " Fifty ", " Sixty ", " Seventy ", " Eighty ", " Ninety " }; 


     if (inputNumber == 0) 
     { 
      tBlkOutput.Text += "zero"; 
     } 






     if ((inputNumber/1000000) > 0) 
     { 
      if ((inputNumber/100000000) > 0) 
      { 
       // needs a number betwwen 1 and 9 
       tBlkOutput.Text += unitsMap[inputNumber/100000000] + " Hundred and "; 
       inputNumber %= 100000000; 
      } 

      // need to be able to list between 1 million and 99 million 
      if ((inputNumber/10000000) > 0) 
      { 
       tBlkOutput.Text += tensMap[inputNumber/10000000]; 
       inputNumber %= 10000000; 
      } 
      if ((inputNumber/1000000) > 0) 
      { 
       tBlkOutput.Text += unitsMap[inputNumber/1000000]; 
       inputNumber %= 1000000; 
      } 

      tBlkOutput.Text += " Million "; 
     } 


     if ((inputNumber/1000) > 0) 
     { 
      if ((inputNumber/100000) > 0) 
      { 
       // needs a number betwwen 1 and 9 
       tBlkOutput.Text += unitsMap[inputNumber/100000] + " Hundred and "; 
       inputNumber %= 100000; 
      } 

      if ((inputNumber/10000) > 0) 
      { 
       tBlkOutput.Text += tensMap[inputNumber/10000]; 
       inputNumber %= 10000; 
      } 
      if ((inputNumber/1000) > 0) 
      { 
       tBlkOutput.Text += unitsMap[inputNumber/1000]; 
       inputNumber %= 1000; 
      } 
      tBlkOutput.Text += " Thousand "; 
     } 

     if ((inputNumber/100) > 0) 
     { 
      tBlkOutput.Text += unitsMap[inputNumber/100] + " Hundred and "; 
      inputNumber %= 100; 
     } 
     if ((inputNumber/10) > 0) 
     { 
      tBlkOutput.Text += tensMap[inputNumber/10]; 
      inputNumber %= 10; 
     } 
     if (inputNumber > 0) 
     { 
      tBlkOutput.Text += unitsMap[inputNumber]; 
     } 



    } 

    private void Reset_Click(object sender, RoutedEventArgs e) 
    { 
     tBlkOutput.Text = ""; 
     inputNumber = 0; 
     txtInput.Text = ""; 
     txtInput.Focus(); 
    } 
} 
関連する問題