Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Program.cs
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- namespace Lab3_02
- {
- class Program
- {
- public static Random rnd;
- /// <summary>
- /// הפעולה יוצרת יתד רנדומלי בעל טבעות רנדומליות באורך של לפחות שבע
- /// </summary>
- /// <returns> היתד הרנדומלי </returns>
- // סיבוכיות O(n)
- public static Peg RandomPeg()
- {
- rnd = new Random();
- int count = rnd.Next(7, 11);
- Peg p = new Peg();
- for (int i = 0; i < count; i++)
- {
- Ring r = new Ring();
- switch (rnd.Next(1, 3))
- {
- case 1:
- r.Size = 'b';
- break;
- case 2:
- r.Size = 's';
- break;
- }
- switch (rnd.Next(1, 3))
- {
- case 1:
- r.Color = "blue";
- break;
- case 2:
- r.Color = "red";
- break;
- }
- p.PutRing(r);
- }
- return p;
- }
- //O(n)
- static void Main(string[] args)
- {
- Peg p1 = RandomPeg();
- Console.WriteLine(p1.ToString());
- Peg p2 = new Peg();
- Peg p3 = new Peg();
- Sort(p1, p2, p3);
- Console.WriteLine(p1.ToString());
- }
- }
- }
- /////////
- Peg.cs
- using System;
- using Unit4;
- using Unit4.CollectionsLib;
- using System.Linq;
- using System.Text;
- namespace Lab3_02
- {
- public class Peg
- {
- private Stack<Ring> rings;
- public Peg()
- {
- rings = new Stack<Ring>();
- }
- public Peg(Ring ring)
- {
- rings = new Stack<Ring>();
- rings.Push(ring);
- }
- public Ring RemoveRing()
- {
- if (!this.IsPegEmpty())
- {
- return this.rings.Pop();
- }
- return null;
- }
- public void PutRing(Ring ring)
- {
- this.rings.Push(ring);
- }
- public bool IsPegEmpty()
- {
- return this.rings.IsEmpty();
- }
- public string ToString()
- {
- if (this.IsPegEmpty())
- return "Empty peg!\n";
- int count = 0;
- string st = "";
- Ring tempR;
- Stack<Ring> temp = new Stack<Ring>();
- while (!this.IsPegEmpty())
- {
- count++;
- tempR = this.rings.Pop();
- temp.Push(tempR);
- st += "\nRing number : " +count +" \n" +tempR.ToString();
- if (!this.IsPegEmpty())
- st += "\n";
- }
- while (!temp.IsEmpty())
- this.rings.Push(temp.Pop());
- return st;
- }
- }
- }
- //////
- Ring.cs
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- namespace Lab3_02
- {
- public class Ring
- {
- private char size;
- private string color;
- public Ring()
- {
- size = 'b';
- color = "blue";
- }
- public Ring(char size, string color)
- {
- this.size = size;
- this.color = color;
- }
- public char Size
- {
- get { return this.size; }
- set { this.size = value; }
- }
- public string Color
- {
- get { return this.color; }
- set { this.color = value; }
- }
- public override string ToString()
- {
- return "size - " + this.size + "\nColor - " + this.color;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement