fork download
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace CSharpProject {
  6. /// <summary>
  7. /// 単なる数値で表された金額を硬貨、札で表現しなおすクラス。
  8. /// </summary>
  9. public class Mony {
  10. //内部用の機械的アクセス用。
  11. private int[] datas = new int[9];
  12.  
  13. public int One { get { return datas[0]; } private set { datas[0] = value; } }
  14. public int Five { get { return datas[1]; } private set { datas[1] = value; } }
  15. //10
  16. public int Ten { get { return datas[2]; } private set { datas[2] = value; } }
  17. public int Fifty { get { return datas[3]; } private set { datas[3] = value; } }
  18. public int Hundred { get { return datas[4]; } private set { datas[4] = value; } }
  19. public int FiveHundred { get { return datas[5]; } private set { datas[5] = value; } }
  20. //1,000
  21. public int Thousand { get { return datas[6]; } private set { datas[6] = value; } }
  22. public int FiveThousand { get { return datas[7]; } private set { datas[7] = value; } }
  23. //10,000
  24. public int TehThousand { get { return datas[8]; } private set { datas[8] = value; } }
  25.  
  26. private int intValue;
  27.  
  28. /// <summary>
  29. /// このクラスを初期化します。
  30. /// </summary>
  31. /// <param name="value">金額</param>
  32. public Mony(int value) {
  33. ParseInt(value);
  34. }
  35.  
  36. /// <summary>
  37. /// intをパースしてMonyクラスにセットする
  38. /// </summary>
  39. /// <param name="num"></param>
  40. private void ParseInt(int num) {
  41. //Array.Clear(datas, 0, datas.Length);
  42. intValue = num;
  43.  
  44. for (int i = 1; i <= Math.Min(4, (int)Math.Log10(num) + 1); i++) {
  45. var value = GetDigitValue(num, i);
  46. if (value >= 5) {
  47. datas[(i - 1) * 2] = value - 5;
  48. datas[(i - 1) * 2 + 1]++;
  49. } else {
  50. datas[(i - 1) * 2] = value;
  51. }
  52. }
  53.  
  54. TehThousand = num / 10000;
  55. }
  56.  
  57. /// <summary>
  58. /// intの任意の桁の値を取得します。
  59. /// </summary>
  60. /// <param name="digit">下から数えた取得したい桁数</param>
  61. /// <param name="num">取得する元の数値</param>
  62. private int GetDigitValue(int num, int digit) {
  63. int numDighit = (int)Math.Log10(num) + 1;
  64. if (numDighit < digit || digit < 0)
  65. throw new ArgumentException("digit");
  66. for (int i = numDighit; i > digit; i--) {
  67. //消去したい桁数
  68. int deleateDigiht = (int)Math.Pow(10, (int)Math.Log10(num));
  69. //ex:4321→4→4000
  70. num -= num / deleateDigiht * deleateDigiht;
  71. }
  72. if (digit == 1)
  73. return num;
  74. return num / (int)Math.Pow(10, digit - 1);
  75. }
  76.  
  77. /// <summary>
  78. /// この構造体をintに変換します。
  79. /// </summary>
  80. /// <returns></returns>
  81. public int ToInt32() {
  82. return intValue;
  83. }
  84.  
  85. }
  86.  
  87. class Program {
  88. public static void Main() {
  89. //お釣り
  90. var m = new Mony(1234986);
  91. var s = string.Format("10,000yen>{0},5,000yen>{1},1,000yen>{2},500yen>{3},100yen{4},50yen>{5},10yen>{6},5yen>{7},1yen>{8}",
  92. m.TehThousand, m.FiveThousand, m.Thousand, m.FiveHundred, m.Hundred, m.Fifty,m.Ten, m.Five, m.One);
  93. Console.WriteLine(s);
  94. }
  95. }
  96. }
  97.  
Success #stdin #stdout 0.04s 37000KB
stdin
Standard input is empty
stdout
10,000yen>123,5,000yen>0,1,000yen>4,500yen>1,100yen4,50yen>1,10yen>3,5yen>1,1yen>1