Advertisement
AvengersAssemble

TestQ3-4

Mar 18th, 2015
275
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.96 KB | None | 0 0
  1. Program.cs
  2.  
  3. using System;
  4. using Unit4.CollectionsLib;
  5. using System.Linq;
  6. using System.Text;
  7.  
  8. namespace Q4
  9. {
  10.     class Program
  11.     {
  12.         //טענת כניסה: הפעולה מקבלת רשימה
  13.         //טענת יציאה: הפעולה מחזירה את אורך הרשימה
  14.         //סיבוכיות O(n)
  15.         public static int Length(Node<int> l)
  16.         {
  17.             int count = 0;
  18.             Node<int> n = l;
  19.             while (n != null)
  20.             {
  21.                 count++;
  22.                 n = n.GetNext();
  23.             }
  24.             return count;
  25.         }
  26.  
  27.         //טענת כניסה: הפעולה מקבלת רשימה ואינדקס (הערה - המספור מתחיל מ-0!!!) המציין מיקום של איבר ברשימה
  28.         //טענת יציאה: הפעולה מחזירה מצביע לאיבר הנמצא במקום זה ברשימה
  29.         //סיבוכיות O(n)
  30.         public static Node<int> GetPosAt(Node<int> l, int index)
  31.         {
  32.             Node<int> pos = l;
  33.             for (int i = 0; i < index; i++)
  34.                 pos = pos.GetNext();
  35.             return pos;
  36.         }
  37.  
  38.         public static int MiddleInNode(Node<int> l)
  39.         {
  40.             int length = Length(l);
  41.             if (length % 2 == 0)
  42.                 return 0;
  43.             return GetPosAt(l, length / 2).GetInfo();
  44.         }
  45.  
  46.         public static Queue<int> CreateQueue(Stack<Node<int>> s)
  47.         {
  48.             Queue<int> q = new Queue<int>();
  49.             while (!s.IsEmpty())
  50.             {
  51.                 q.Insert(MiddleInNode(s.Pop()));
  52.                 Console.WriteLine(q);
  53.             }
  54.             return q;
  55.         }
  56.  
  57.         //returns the smallest value stored in a stack
  58.         public static int Smallest(Queue<int> q)
  59.         {
  60.             int x = 0, min = int.MaxValue, n = 0;
  61.             if (!q.IsEmpty())
  62.             {
  63.                 x = q.Remove();
  64.                 n = Smallest(q);
  65.                 if (min == int.MaxValue)
  66.                     min = x;
  67.                 if (n < min)
  68.                     min = n;
  69.             }
  70.             if (x != 0)
  71.                 q.Insert(x);
  72.             return min;
  73.         }
  74.  
  75.         //removes all instances of a given value from a stack
  76.         public static void RemoveAllX(Queue<int> q, int x)
  77.         {
  78.             int y = 0;
  79.             if (!q.IsEmpty())
  80.             {
  81.                 y = q.Remove();
  82.                 RemoveAllX(q, x);
  83.             }
  84.             if (y != x && y != 0)
  85.                 q.Insert(y);
  86.         }
  87.         //Sorts a stack in a descending order
  88.         public static Queue<int> Sort(Queue<int> q)
  89.         {
  90.             int next;
  91.             Queue<int> sorted = new Queue<int>();
  92.             while (!q.IsEmpty())
  93.             {
  94.                 next = Smallest(q);
  95.                 sorted.Insert(next);
  96.                 RemoveAllX(q, next);
  97.                 Console.WriteLine(sorted);
  98.             }
  99.             return sorted;
  100.         }
  101.  
  102.         //******
  103.         public bool IsFound(Node<Person> l, Person k)
  104.         {
  105.             Node<Person> tmp = l;
  106.             while (tmp != null)
  107.             {
  108.                 if (tmp.GetInfo().Equelsper(k))
  109.                     return true;
  110.                 tmp = tmp.GetNext();
  111.             }
  112.             return false;
  113.         }
  114.  
  115.         //טענת כניסה: הפעולה מקבלת רשימה
  116.         //טענת פלט: הפעולה מדפיסה את איברי הרשימה
  117.         //סיבוכיות O(n)
  118.         public static void PrintNode(Node<int> n)
  119.         {
  120.             Node<int> l = n;
  121.             while (l != null)
  122.             {
  123.                 if (l.GetNext() != null)
  124.                     Console.Write(l.GetInfo() + " --> ");
  125.                 else
  126.                     Console.WriteLine(l.GetInfo() + ".");
  127.                 l = l.GetNext();
  128.             }
  129.         }
  130.         static void Main(string[] args)
  131.         {
  132.             Stack<Node<int>> s = new Stack<Node<int>>();
  133.             Node<int> tmp = new Node<int>(22, new Node<int>(7, new Node<int>(8, new Node<int>(5, new Node<int>(3, new Node<int>(12, new Node<int>(2)))))));
  134.             PrintNode(tmp);
  135.             s.Push(tmp);
  136.             tmp = new Node<int>(2, new Node<int>(7, new Node<int>(11, new Node<int>(3, new Node<int>(23, new Node<int>(14, new Node<int>(2, new Node<int>(66))))))));
  137.             PrintNode(tmp);
  138.             s.Push(tmp);
  139.             tmp = new Node<int>(8, new Node<int>(12, new Node<int>(3, new Node<int>(5, new Node<int>(6)))));
  140.             PrintNode(tmp);
  141.             s.Push(tmp);
  142.             tmp = new Node<int>(21, new Node<int>(9, new Node<int>(7, new Node<int>(6, new Node<int>(25, new Node<int>(22, new Node<int>(1, new Node<int>(4, new Node<int>(17)))))))));
  143.             PrintNode(tmp);
  144.             s.Push(tmp);
  145.             Queue<int> q = new Queue<int>();
  146.             q = CreateQueue(s);
  147.             q = Sort(q);
  148.             Console.WriteLine(q);
  149.         }
  150.     }
  151. }
  152.  
  153.  
  154.  
  155.  
  156.  
  157.  
  158.  
  159.  
  160.  
  161.  
  162. ***********************
  163. Person.cs
  164. using System;
  165. using System.Collections.Generic;
  166. using System.Linq;
  167. using System.Text;
  168.  
  169. namespace Q4
  170. {
  171.     public class Person
  172.     {
  173.         private string name;
  174.         private string surname;
  175.         private int age;
  176.  
  177.         public Person(string name, string surname, int age)
  178.         {
  179.             this.name = name;
  180.             this.surname = surname;
  181.             this.age = age;
  182.         }
  183.  
  184.         public bool Equelsper(Person p)
  185.         {
  186.             return this.name.Equals(p.Name) && this.surname.Equals(p.Surname) && this.age.Equals(p.Age);
  187.         }
  188.         public string Name
  189.         {
  190.             get { return this.name; }
  191.             set { this.name = value; }
  192.         }
  193.  
  194.         public string Surname
  195.         {
  196.             get { return this.surname; }
  197.             set { this.surname = value; }
  198.         }
  199.  
  200.         public int Age
  201.         {
  202.             get { return this.age; }
  203.             set { this.age = value; }
  204.         }
  205.     }
  206. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement