Ejemplo n.º 1
0
 /// <summary>
 /// Sorts the droids based on model
 /// </summary>
 /// <param name="toSort"></param>
 public void SortModel(List<Droid> toSort)
 {
     MyStack<UtilityDroid> utilityStack = new MyStack<UtilityDroid>();
     MyStack<AstromechDroid> astroStack = new MyStack<AstromechDroid>();
     MyStack<ProtocolDroid> protoStack = new MyStack<ProtocolDroid>();
     MyStack<JanitorDroid> janitorStack = new MyStack<JanitorDroid>();
     //Separates the List based on class
     foreach (Droid d in toSort)
     {
         if (d is AstromechDroid)
             astroStack.Add((AstromechDroid)d);
         else if (d is JanitorDroid)
             janitorStack.Add((JanitorDroid)d);
         else if (d is UtilityDroid)
             utilityStack.Add((UtilityDroid)d);
         else if (d is ProtocolDroid)
             protoStack.Add((ProtocolDroid)d);
     }
     // puts the stacks into a Queue
     MyQueue<Droid> tmpQue = new MyQueue<Droid>();
     while (astroStack.Count > 0)
         tmpQue.Add(astroStack.Get());
     while (janitorStack.Count > 0)
         tmpQue.Add(janitorStack.Get());
     while (utilityStack.Count > 0)
         tmpQue.Add(utilityStack.Get());
     while (protoStack.Count > 0)
         tmpQue.Add(protoStack.Get());
     //Relies on the fact that a list is a reference object
     toSort.Clear();
     while (tmpQue.Count > 0)
         toSort.Add(tmpQue.Get());
 }
Ejemplo n.º 2
0
        public static void Main(string[] args)
        {   //1
            ArrayList list     = new ArrayList();
            Student   student  = new Student("Vasya", 18);
            Random    random   = new Random();
            string    myString = "Here's a string";

            for (var i = 0; i < 5; i++)
            {
                list.Add(random.Next(0, 100));
            }
            list.Add(myString);
            list.Add(student);
            list.RemoveAt(0);
            Console.WriteLine("Our Arraylist collection:\n");
            foreach (object o in list)
            {
                Console.WriteLine(o);
            }

            Console.WriteLine($"\nCount of elements: {list.Count}");

            for (var i = 0; i < list.Count; i++)
            {
                if (list[i] == myString)
                {
                    Console.WriteLine("Строка myString найдена!");
                }
            }


            //2
            MyDictionary <double, string> dictionary_1 = new MyDictionary <double, string>();

            double[] keys = { 1, 2.1 };
            dictionary_1.Add(1.5, "Hello");
            dictionary_1.Add(2.1, "World");
            dictionary_1.Add(1, "Banana");
            dictionary_1.Delete(1);

            foreach (KeyValuePair <double, string> pair in dictionary_1)
            {
                Console.WriteLine("{0}, {1}", pair.Key, pair.Value);
            }

            MyQueue <string> queue_1 = new MyQueue <string>();

            foreach (KeyValuePair <double, string> pair in dictionary_1)
            {
                queue_1.Add(pair.Value);
            }

            foreach (string item in queue_1)
            {
                Console.WriteLine(item);
            }

            string s = "Hello";

            foreach (string item in queue_1)
            {
                if (item == s)
                {
                    Console.WriteLine("An item has been finded in a queue");
                }
            }


            //3
            MyDictionary <double, Student> dictionary_2 = new MyDictionary <double, Student>();
            Student student_1 = new Student("Petya", 20);
            Student student_2 = new Student("Masha", 17);

            dictionary_2.Add(1, student_1);
            dictionary_2.Add(2, student_2);

            MyQueue <Student> queue_2 = new MyQueue <Student>();

            foreach (KeyValuePair <double, Student> pair in dictionary_2)
            {
                queue_2.Add(pair.Value);
            }

            foreach (Student item in queue_2)
            {
                Console.WriteLine(item);
            }

            Student student_3 = new Student("Petya", 20);

            foreach (Student item in queue_2)
            {
                if ((item.Name == student_3.Name) && (item.Age == student_3.Age))
                {
                    Console.WriteLine("Студент Петя уже существует!");
                }
            }

            //4
            ObservableCollection <Student> students = new ObservableCollection <Student>
            {
                student_1,
                student_2
            };

            students.CollectionChanged += Students_CollectionChanged;

            students.Add(student_3);
            students.Remove(student_2);

            Console.ReadKey();
        }
Ejemplo n.º 3
0
        static void Main(string[] args)
        {
            string    test1 = "The implementation done with integers";
            string    test2 = "the implementation done with characters";
            const int ONE   = 1;
            const int TWO   = 2;
            const int THREE = 3;
            const int FOUR  = 4;
            const int FIVE  = 5;

            const char SONE   = '1';
            const char STWO   = '2';
            const char STHREE = '3';
            const char SFOUR  = '4';
            const char SFIVE  = '5';



            int size   = 0;
            int choice = 0;

            Queue <int>   q   = new Queue <int>();
            MyQueue <int> mq  = new MyQueue <int>(ref q);
            List <int>    lst = new List <int>();

            Queue <char>   s    = new Queue <char>();
            MyQueue <char> sq   = new MyQueue <char>(ref s);
            List <char>    slst = new List <char>();


            mq.Add(ONE);
            mq.Add(TWO);
            lst.Add(THREE);
            mq.ChangeImpl(ref lst);
            mq.Add(FOUR);
            mq.Add(FIVE);

            Console.WriteLine("{0}", test1);

            size = mq.size();

            for (int i = 0; i < size; i++)
            {
                Console.Write("{0}", mq.Get());
                mq.Remove();
            }

            mq.Clear();
            Console.WriteLine();
            Console.WriteLine("{0}", test2);

            sq.Add(SONE);
            sq.Add(STWO);
            slst.Add(STHREE);
            sq.ChangeImpl(ref slst);
            sq.Add(SFOUR);
            sq.Add(SFIVE);

            size = sq.size();

            for (int i = 0; i < size; i++)
            {
                Console.Write("{0}", sq.Get());
                sq.Remove();
            }

            sq.Clear();

            Console.WriteLine();
            Console.ReadLine();
        }   // end main
Ejemplo n.º 4
0
 public void AddingSinglePatientShouldHaveSize1()
 {
     queue.Add(patient);
     Assert.AreEqual(1, queue.Size());
 }