Advertisement
AvengersAssemble

תיחנק דולב

Feb 7th, 2015
215
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.96 KB | None | 0 0
  1. Program.cs
  2.  
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Text;
  7.  
  8. namespace Lab3_02
  9. {
  10.     class Program
  11.     {
  12.         public static Random rnd;
  13.         /// <summary>
  14.         /// הפעולה יוצרת יתד רנדומלי בעל טבעות רנדומליות באורך של לפחות שבע
  15.         /// </summary>
  16.         /// <returns> היתד הרנדומלי </returns>
  17.         // סיבוכיות O(n)
  18.         public static Peg RandomPeg()
  19.         {
  20.             rnd = new Random();
  21.             int count = rnd.Next(7, 11);
  22.             Peg p = new Peg();
  23.             for (int i = 0; i < count; i++)
  24.             {
  25.                 Ring r = new Ring();
  26.                 switch (rnd.Next(1, 3))
  27.                 {
  28.                     case 1:
  29.                         r.Size = 'b';
  30.                         break;
  31.                     case 2:
  32.                         r.Size = 's';
  33.                         break;
  34.                 }
  35.                 switch (rnd.Next(1, 3))
  36.                 {
  37.                     case 1:
  38.                         r.Color = "blue";
  39.                         break;
  40.                     case 2:
  41.                         r.Color = "red";
  42.                         break;
  43.                 }
  44.                 p.PutRing(r);
  45.             }
  46.             return p;
  47.         }
  48.  
  49.         //O(n)
  50.         static void Main(string[] args)
  51.         {
  52.             Peg p1 = RandomPeg();
  53.             Console.WriteLine(p1.ToString());
  54.             Peg p2 = new Peg();
  55.             Peg p3 = new Peg();
  56.             Sort(p1, p2, p3);
  57.             Console.WriteLine(p1.ToString());
  58.         }
  59.     }
  60. }
  61.  
  62. /////////
  63.  
  64. Peg.cs
  65.  
  66. using System;
  67. using Unit4;
  68. using Unit4.CollectionsLib;
  69. using System.Linq;
  70. using System.Text;
  71.  
  72. namespace Lab3_02
  73. {
  74.     public class Peg
  75.     {
  76.         private Stack<Ring> rings;
  77.         public Peg()
  78.         {
  79.             rings = new Stack<Ring>();
  80.         }
  81.         public Peg(Ring ring)
  82.         {
  83.             rings = new Stack<Ring>();
  84.             rings.Push(ring);
  85.         }
  86.         public Ring RemoveRing()
  87.         {
  88.             if (!this.IsPegEmpty())
  89.             {
  90.                 return this.rings.Pop();
  91.             }
  92.             return null;
  93.         }
  94.         public void PutRing(Ring ring)
  95.         {
  96.             this.rings.Push(ring);
  97.         }
  98.         public bool IsPegEmpty()
  99.         {
  100.             return this.rings.IsEmpty();
  101.         }
  102.         public string ToString()
  103.         {
  104.             if (this.IsPegEmpty())
  105.                 return "Empty peg!\n";
  106.             int count = 0;
  107.             string st = "";
  108.             Ring tempR;
  109.             Stack<Ring> temp = new Stack<Ring>();
  110.             while (!this.IsPegEmpty())
  111.             {
  112.                 count++;
  113.                 tempR = this.rings.Pop();
  114.                 temp.Push(tempR);
  115.                 st += "\nRing number : " +count +"  \n" +tempR.ToString();
  116.                 if (!this.IsPegEmpty())
  117.                     st += "\n";
  118.             }
  119.             while (!temp.IsEmpty())
  120.                 this.rings.Push(temp.Pop());
  121.             return st;
  122.         }
  123.     }
  124. }
  125.  
  126. //////
  127.  
  128. Ring.cs
  129.  
  130. using System;
  131. using System.Collections.Generic;
  132. using System.Linq;
  133. using System.Text;
  134.  
  135. namespace Lab3_02
  136. {
  137.     public class Ring
  138.     {
  139.         private char size;
  140.         private string color;
  141.         public Ring()
  142.         {
  143.             size = 'b';
  144.             color = "blue";
  145.         }
  146.         public Ring(char size, string color)
  147.         {
  148.             this.size = size;
  149.             this.color = color;
  150.         }
  151.         public char Size
  152.         {
  153.             get { return this.size; }
  154.             set { this.size = value; }
  155.         }
  156.         public string Color
  157.         {
  158.             get { return this.color; }
  159.             set { this.color = value; }
  160.         }
  161.  
  162.         public override string ToString()
  163.         {
  164.             return "size - " + this.size + "\nColor - " + this.color;
  165.         }
  166.     }
  167. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement