Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace ConsoleApp12
- {
- class Number
- {
- int num; // המספר עצמו
- int[] Digits = new int[10]; // כמה פעמים קיימת כל ספרה
- bool flag; // אם כמות הספרות הזוגיות גדול/שווה לכמות הספרות האיזוגיות
- public Number()
- {
- Random r = new Random();
- int num = r.Next(-100000, 100000);
- Number n = new Number(num);
- this.num = n.num;
- this.Digits = n.Digits;
- this.flag = n.flag;
- }
- public Number(int num)
- { // הנחה שהמספר נמצא בתחום
- this.num = num;
- this.Digits = new int[10] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
- num = Math.Abs(num);
- if (num == 0)
- this.Digits[0] = 1;
- while (num > 0)
- {
- this.Digits[num % 10]++;
- num = num / 10;
- }
- this.MoreEvenThanOddDigs();
- }
- public int CountDig(int dig)
- { // בין 0 ל 9 dig הנחה ש
- return this.Digits[dig];
- }
- private void MoreEvenThanOddDigs()
- {
- int SumEven = this.Digits[0] + this.Digits[2] + this.Digits[4] + this.Digits[6] + this.Digits[8];
- int SumOdd = this.Digits[1] + this.Digits[3] + this.Digits[5] + this.Digits[7] + this.Digits[9];
- this.flag = SumEven >= SumOdd;
- }
- public bool GetFlag()
- {
- return this.flag;
- }
- public int GetNum()
- {
- return this.num;
- }
- public void Print()
- {
- Console.WriteLine("number = " + this.num);
- for (int i = 0; i < 10; i++)
- {
- Console.WriteLine("Digits[" + i + "] = " + this.Digits[i]);
- }
- Console.WriteLine("flag = " + this.flag);
- }
- }
- }
- //________________________________________________________________________________________________________________
- using System;
- using NewUnit4;// using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace ConsoleApp12
- {
- class Numbers
- {
- Node<Number> os;
- Node<Number> posMaxNum;
- public Numbers(int num)
- { // הנחה שהמספר בתחום
- this.os = new Node<Number>(new Number(num));
- this.posMaxNum = this.os;
- }
- public Numbers()
- {
- this.os = new Node<Number>(new Number());
- this.posMaxNum = this.os;
- }
- public void Max()
- { // הפניה לחוליה של המספר הראשון הגדול ביותר ברשימה posMaxNum פעולה שמכניסה ל
- Node<Number> pos = this.os;
- int max = this.os.GetValue().GetNum(); // ...המספר הראשון
- while (pos != null)
- {
- if (pos.GetValue().GetNum() > max)
- {
- this.posMaxNum = pos;
- max = pos.GetValue().GetNum();
- }
- pos = pos.GetNext();
- }
- }
- public int Count()
- {
- int counter = 0;
- Node<Number> pos = this.os;
- while (pos != null)
- {
- counter++;
- pos = pos.GetNext();
- }
- return counter;
- }
- public bool AddNum(int num)
- { // הנחה שהמספר בתחום
- Node<Number> pos = this.os;
- while (pos.HasNext())
- {
- if (pos.GetValue().GetNum() == num)
- {
- return false;
- }
- pos = pos.GetNext();
- }
- if (pos.GetValue().GetNum() == num)
- return false;
- pos.SetNext(new Node<Number>(new Number(num)));
- Max();
- return true;
- }
- public bool AddNum()
- {
- return AddNum((new Number()).GetNum());
- }
- public int CountDigInAllNums(int dig)
- { // בין 0 ל 9 dig הנחה ש
- int DigCounter = 0;
- Node<Number> pos = this.os;
- while (pos != null)
- {
- DigCounter += pos.GetValue().CountDig(dig);
- pos = pos.GetNext();
- }
- return DigCounter;
- }
- public Node<Number> GetPosMaxNum()
- {
- return this.posMaxNum;
- }
- public void Print()
- {
- Node<Number> pos = this.os;
- while (pos != null)
- {
- Console.Write("[" + pos.GetValue().GetNum() + "] --> ");
- pos = pos.GetNext();
- }
- Console.WriteLine("///");
- Console.WriteLine("posMaxNum = " + this.posMaxNum.GetValue().GetNum());
- Console.WriteLine("~~~~~~~~~~~~~~~~~~~~~~");
- }
- }
- }
- //________________________________________________________________________________________________________________
- using System;
- using NewUnit4;// using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace ConsoleApp12
- {
- class Program
- {
- public static Numbers MakeCollection(int n)
- { // טבעי. כלומר שלם וחיובי n הנחה ש
- n = Math.Abs(n); // ...למען הסר ספק
- Numbers nums = new Numbers(); // כעת, קיימת חוליה ראשונה
- int x; // זה מבלבל num כי לקרוא לזה ,x קראתי לזה // numbers שמוכלת ב number יהיה התכונה 'מספר' של המחלקה
- Random r = new Random();
- for (int i = 0; i < n ; i++)
- {
- x = r.Next(-100000, 100000);
- nums.AddNum(x);
- }
- return nums;
- }
- public static void HowManyTimes(Numbers nums)
- { // void הפעולה מדפיסה ולא מחזירה ולכן היא
- int dig = int.Parse(Console.ReadLine()); // הנחה שתיקלט ספרה בודדת
- Console.WriteLine(dig + " is in the list " + nums.CountDigInAllNums(dig) + " times");
- }
- public static bool Check(Numbers nums)
- {
- return nums.GetPosMaxNum().GetValue().GetFlag();
- }
- static void Main(string[] args)
- {
- // Console.WriteLine(new Number().GetNum() + "\n--------------------");
- // Console.WriteLine(new Number().GetNum() + "\n--------------------");
- // !למרות שהוא נכתב new כפי שניתן לראות יש בעיה חמורה: ההרצה לא מתייחסת לביטוי
- // ובגלל שאותו המספר מוגרל שוב ושוב- הוא לא יכנס לרשימה- כי יש תנאי שמכניס מספר רק אם הוא חדש ולא קיים כבר ברשימה
- Numbers nums = MakeCollection(5);
- nums.Print();
- HowManyTimes(nums);
- Console.WriteLine(Check(nums));
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement