Advertisement
Guest User

Untitled

a guest
May 27th, 2017
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.28 KB | None | 0 0
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6.  
  7. namespace ConsoleApplication1
  8. {
  9.  
  10. public class BlindAbacus : IEnumerable
  11. {
  12. public override string ToString()
  13. {
  14. StringBuilder wynik = new StringBuilder();
  15. for (int i = iloscrzedow - 1; i >= 0; i--)
  16. {
  17. wynik.Append(rzedy[i].ToString());
  18. wynik.AppendLine();
  19. }
  20. return wynik.ToString();
  21. }
  22. public class ComputationException : Exception { }
  23. public class IncorrectRowValueException : Exception { }
  24. public class SomeoneThereException : Exception { }
  25. public int podstawa;
  26. public Row[] rzedy;
  27. bool[] zajete;
  28. int iloscrzedow;
  29.  
  30. public static BlindAbacus nowyAbakus(int liczba, int podstawa)
  31. {
  32. if(liczba<0) throw new ComputationException();
  33. int ilerzedow = 0;
  34. int temp = liczba;
  35. while (temp > 0)
  36. {
  37. temp/=podstawa;
  38. ilerzedow++;
  39. }
  40. BlindAbacus wynik = new BlindAbacus(podstawa, ilerzedow);
  41. for (int i = 0; i < ilerzedow; i++)
  42. {
  43. wynik[i] = new Row(podstawa, liczba % podstawa);
  44. liczba /= podstawa;
  45. }
  46. return wynik;
  47. }
  48. public BlindAbacus(int x, int y)
  49. {
  50. podstawa = x;
  51. iloscrzedow = y;
  52. rzedy = new Row[y];
  53. zajete = new bool[y];
  54. for (int i = 0; i < y; i++) this[i] = new Row(x);
  55. }
  56. public static BlindAbacus operator +(BlindAbacus a, BlindAbacus b)
  57. {
  58. if (a.podstawa != b.podstawa) throw new ComputationException();
  59. return nowyAbakus((int)a + (int)b, a.podstawa);
  60. }
  61. public static BlindAbacus operator -(BlindAbacus a, BlindAbacus b)
  62. {
  63. if (a.podstawa != b.podstawa) throw new ComputationException();
  64. return nowyAbakus((int)a - (int)b, a.podstawa);
  65. }
  66. public static BlindAbacus operator ++(BlindAbacus a)
  67. {
  68. return nowyAbakus((int)a + 1, a.podstawa);
  69. }
  70. public static BlindAbacus operator --(BlindAbacus a)
  71. {
  72. return nowyAbakus((int)a - 1, a.podstawa);
  73. }
  74. public static implicit operator int(BlindAbacus m)
  75. {
  76. int wynik = 0;
  77. int mnozyciel=1;
  78. for(int i=0; i<m.iloscrzedow; i++)
  79. {
  80. wynik += m.rzedy[i].liczba * mnozyciel;
  81. mnozyciel*=m.podstawa;
  82. }
  83. return wynik;
  84. }
  85. public Row this[int i]
  86. {
  87. get
  88. {
  89. return rzedy[i];
  90. }
  91. set
  92. {
  93. rzedy[i] = value;
  94. }
  95. }
  96. public class Row
  97. {
  98. public int podstawa;
  99. public int liczba;
  100. public Row(int p, int l)
  101. {
  102. podstawa = p;
  103. liczba = l;
  104. }
  105. public Row(int p)
  106. {
  107. podstawa = p;
  108. liczba = 0;
  109. }
  110. public void add(int x)
  111. {
  112. if ((x + liczba) >= podstawa) throw new IncorrectRowValueException();
  113. liczba += x;
  114. }
  115. public void remove(int x)
  116. {
  117. if (x > liczba) throw new IncorrectRowValueException();
  118. liczba -= x;
  119. }
  120. public override string ToString()
  121. {
  122. StringBuilder wynik = new StringBuilder();
  123. for (int i = 0; i < liczba; i++) wynik.Append("I");
  124. for (int i = 0; i < podstawa-1; i++) wynik.Append("-");
  125. for (int i = 0; i < podstawa-1-liczba; i++) wynik.Append("I");
  126. return wynik.ToString();
  127. }
  128. }
  129. public IEnumerator GetEnumerator()
  130. {
  131. return new MojEnumerator(rzedy,zajete);
  132. }
  133. public class MojEnumerator : IEnumerator
  134. {
  135. object[] lista;
  136. bool[] czyzajete;
  137. public MojEnumerator(object[] par, bool[] anybodythere)
  138. {
  139. lista = par;
  140. czyzajete=anybodythere;
  141. }
  142. int position = -1;
  143. public bool MoveNext()
  144. {
  145. if (position >= 0) czyzajete[position] = false;
  146. if (position >= lista.Length - 1) return false;
  147. if (czyzajete[position + 1]) throw new SomeoneThereException();
  148. position++;
  149. czyzajete[position] = true;
  150. return true;
  151. }
  152. public object Current
  153. {
  154. get
  155. {
  156. return lista[position];
  157. }
  158. }
  159. public void Reset() { position = -1; }
  160. }
  161. }
  162. class Program
  163. {
  164. public static void Desc(BlindAbacus a)
  165. {
  166. Console.WriteLine(a);
  167. Console.WriteLine("Value: " + (int) a);
  168. }
  169. public static void Main()
  170. {
  171. BlindAbacus test = new BlindAbacus(7,7);
  172. for(int i = 0; i <7; i++)
  173. test[i].add(i);
  174. Console.WriteLine(test);
  175. int line = 0;
  176. foreach(BlindAbacus.Row r in test) {
  177. Console.WriteLine(++line + "outer :" + r);
  178. if(line == 5) try {
  179. foreach(BlindAbacus.Row s in test)
  180. Console.WriteLine(" inner :" + s);
  181. }catch(BlindAbacus.SomeoneThereException) {
  182. Console.WriteLine("Someone there");
  183. }
  184. }
  185. foreach(BlindAbacus.Row r in test)
  186. Console.WriteLine(" outer :" + r);
  187. Console.ReadLine();
  188. }
  189. }
  190. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement