Advertisement
Guest User

עבודה במחשבים מתן numbers

a guest
Feb 15th, 2019
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.38 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace ConsoleApp12
  8. {
  9. class Number
  10. {
  11. int num; // המספר עצמו
  12. int[] Digits = new int[10]; // כמה פעמים קיימת כל ספרה
  13. bool flag; // אם כמות הספרות הזוגיות גדול/שווה לכמות הספרות האיזוגיות
  14.  
  15. public Number()
  16. {
  17. Random r = new Random();
  18. int num = r.Next(-100000, 100000);
  19. Number n = new Number(num);
  20. this.num = n.num;
  21. this.Digits = n.Digits;
  22. this.flag = n.flag;
  23. }
  24. public Number(int num)
  25. { // הנחה שהמספר נמצא בתחום
  26. this.num = num;
  27. this.Digits = new int[10] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
  28. num = Math.Abs(num);
  29. if (num == 0)
  30. this.Digits[0] = 1;
  31. while (num > 0)
  32. {
  33. this.Digits[num % 10]++;
  34. num = num / 10;
  35. }
  36. this.MoreEvenThanOddDigs();
  37. }
  38.  
  39. public int CountDig(int dig)
  40. { // בין 0 ל 9 dig הנחה ש
  41. return this.Digits[dig];
  42. }
  43. private void MoreEvenThanOddDigs()
  44. {
  45. int SumEven = this.Digits[0] + this.Digits[2] + this.Digits[4] + this.Digits[6] + this.Digits[8];
  46. int SumOdd = this.Digits[1] + this.Digits[3] + this.Digits[5] + this.Digits[7] + this.Digits[9];
  47. this.flag = SumEven >= SumOdd;
  48. }
  49.  
  50. public bool GetFlag()
  51. {
  52. return this.flag;
  53. }
  54. public int GetNum()
  55. {
  56. return this.num;
  57. }
  58.  
  59. public void Print()
  60. {
  61. Console.WriteLine("number = " + this.num);
  62. for (int i = 0; i < 10; i++)
  63. {
  64. Console.WriteLine("Digits[" + i + "] = " + this.Digits[i]);
  65. }
  66. Console.WriteLine("flag = " + this.flag);
  67. }
  68. }
  69. }
  70. //________________________________________________________________________________________________________________
  71. using System;
  72. using NewUnit4;// using System.Collections.Generic;
  73. using System.Linq;
  74. using System.Text;
  75. using System.Threading.Tasks;
  76.  
  77. namespace ConsoleApp12
  78. {
  79. class Numbers
  80. {
  81. Node<Number> os;
  82. Node<Number> posMaxNum;
  83.  
  84. public Numbers(int num)
  85. { // הנחה שהמספר בתחום
  86. this.os = new Node<Number>(new Number(num));
  87. this.posMaxNum = this.os;
  88. }
  89. public Numbers()
  90. {
  91. this.os = new Node<Number>(new Number());
  92. this.posMaxNum = this.os;
  93. }
  94. public void Max()
  95. { // הפניה לחוליה של המספר הראשון הגדול ביותר ברשימה posMaxNum פעולה שמכניסה ל
  96. Node<Number> pos = this.os;
  97. int max = this.os.GetValue().GetNum(); // ...המספר הראשון
  98. while (pos != null)
  99. {
  100. if (pos.GetValue().GetNum() > max)
  101. {
  102. this.posMaxNum = pos;
  103. max = pos.GetValue().GetNum();
  104. }
  105. pos = pos.GetNext();
  106. }
  107. }
  108.  
  109. public int Count()
  110. {
  111. int counter = 0;
  112. Node<Number> pos = this.os;
  113. while (pos != null)
  114. {
  115. counter++;
  116. pos = pos.GetNext();
  117. }
  118. return counter;
  119. }
  120.  
  121. public bool AddNum(int num)
  122. { // הנחה שהמספר בתחום
  123. Node<Number> pos = this.os;
  124. while (pos.HasNext())
  125. {
  126. if (pos.GetValue().GetNum() == num)
  127. {
  128. return false;
  129. }
  130. pos = pos.GetNext();
  131. }
  132. if (pos.GetValue().GetNum() == num)
  133. return false;
  134. pos.SetNext(new Node<Number>(new Number(num)));
  135. Max();
  136. return true;
  137. }
  138. public bool AddNum()
  139. {
  140. return AddNum((new Number()).GetNum());
  141. }
  142.  
  143. public int CountDigInAllNums(int dig)
  144. { // בין 0 ל 9 dig הנחה ש
  145. int DigCounter = 0;
  146. Node<Number> pos = this.os;
  147. while (pos != null)
  148. {
  149. DigCounter += pos.GetValue().CountDig(dig);
  150. pos = pos.GetNext();
  151. }
  152. return DigCounter;
  153. }
  154.  
  155. public Node<Number> GetPosMaxNum()
  156. {
  157. return this.posMaxNum;
  158. }
  159.  
  160.  
  161. public void Print()
  162. {
  163. Node<Number> pos = this.os;
  164. while (pos != null)
  165. {
  166. Console.Write("[" + pos.GetValue().GetNum() + "] --> ");
  167. pos = pos.GetNext();
  168. }
  169. Console.WriteLine("///");
  170. Console.WriteLine("posMaxNum = " + this.posMaxNum.GetValue().GetNum());
  171. Console.WriteLine("~~~~~~~~~~~~~~~~~~~~~~");
  172. }
  173. }
  174. }
  175. //________________________________________________________________________________________________________________
  176. using System;
  177. using NewUnit4;// using System.Collections.Generic;
  178. using System.Linq;
  179. using System.Text;
  180. using System.Threading.Tasks;
  181.  
  182. namespace ConsoleApp12
  183. {
  184. class Program
  185. {
  186. public static Numbers MakeCollection(int n)
  187. { // טבעי. כלומר שלם וחיובי n הנחה ש
  188. n = Math.Abs(n); // ...למען הסר ספק
  189. Numbers nums = new Numbers(); // כעת, קיימת חוליה ראשונה
  190. int x; // זה מבלבל num כי לקרוא לזה ,x קראתי לזה // numbers שמוכלת ב number יהיה התכונה 'מספר' של המחלקה
  191. Random r = new Random();
  192. for (int i = 0; i < n ; i++)
  193. {
  194. x = r.Next(-100000, 100000);
  195. nums.AddNum(x);
  196. }
  197. return nums;
  198. }
  199. public static void HowManyTimes(Numbers nums)
  200. { // void הפעולה מדפיסה ולא מחזירה ולכן היא
  201. int dig = int.Parse(Console.ReadLine()); // הנחה שתיקלט ספרה בודדת
  202. Console.WriteLine(dig + " is in the list " + nums.CountDigInAllNums(dig) + " times");
  203. }
  204. public static bool Check(Numbers nums)
  205. {
  206. return nums.GetPosMaxNum().GetValue().GetFlag();
  207. }
  208. static void Main(string[] args)
  209. {
  210. // Console.WriteLine(new Number().GetNum() + "\n--------------------");
  211. // Console.WriteLine(new Number().GetNum() + "\n--------------------");
  212. // !למרות שהוא נכתב new כפי שניתן לראות יש בעיה חמורה: ההרצה לא מתייחסת לביטוי
  213. // ובגלל שאותו המספר מוגרל שוב ושוב- הוא לא יכנס לרשימה- כי יש תנאי שמכניס מספר רק אם הוא חדש ולא קיים כבר ברשימה
  214.  
  215. Numbers nums = MakeCollection(5);
  216. nums.Print();
  217. HowManyTimes(nums);
  218. Console.WriteLine(Check(nums));
  219. }
  220. }
  221. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement