Advertisement
AvengersAssemble

Lab5

Nov 2nd, 2014
201
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.81 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3. using System.Text;
  4. using Unit4;
  5. using Unit4.CollectionsLib;
  6.  
  7. namespace Lab4
  8. {
  9.     class Program
  10.     {
  11.         //Q1
  12.         private static Random rnd = new Random();
  13.         public static Node<int> RandomNode()
  14.         {
  15.             int length, next;
  16.             length = rnd.Next(5, 11);
  17.             next = rnd.Next(10, 100);
  18.             Node<int> n = new Node<int>(next);
  19.             Node<int> n2;
  20.             for (int i = 1; i < length; i++)
  21.             {
  22.                 next = rnd.Next(10, 100);
  23.                 n2 = new Node<int>(next);
  24.                 n2.SetNext(n);
  25.                 n = n2;
  26.             }
  27.             return n;
  28.         }
  29.  
  30.         //Q2
  31.         public static Stack<Node<int>> NodeStack()
  32.         {
  33.             Stack<Node<int>> stk = new Stack<Node<int>>();
  34.             for (int k = 0; k < 10; k++)
  35.                 stk.Push(RandomNode());
  36.             return stk;
  37.         }
  38.         public static void PrintNodeStack(Stack<Node<int>> stk)
  39.         {
  40.             Stack<Node<int>> forreturn = new Stack<Node<int>>();
  41.             Node<int> temp;
  42.             while (stk.IsEmpty() == false)
  43.             {
  44.                 temp = stk.Pop();
  45.                 forreturn.Push(temp);
  46.                 while (temp != null)
  47.                 {
  48.                     if (temp.GetNext() == null) // אם זה האחרון הוא לא מדפיס "," בסוף
  49.                     {
  50.                         Console.Write(temp.ToString());
  51.                     }
  52.                     else
  53.                     {
  54.  
  55.                         Console.Write(temp.ToString() + ",");
  56.                     }
  57.                     temp = temp.GetNext();
  58.                 }
  59.  
  60.                 Console.WriteLine();
  61.             }
  62.  
  63.             while (forreturn.IsEmpty() == false)
  64.             {
  65.                 stk.Push(forreturn.Pop());
  66.             }
  67.         }
  68.  
  69.         //Q3
  70.         public static Node<int> LongestNode(Stack<Node<int>> s)
  71.         {
  72.             int count, count2 = 0;
  73.             Stack<Node<int>> forreturn = new Stack<Node<int>>();
  74.             Node<int> temp, temp2;
  75.             Node<int> longest = null;
  76.             while (!s.IsEmpty())
  77.             {
  78.                 count = 0;
  79.                 temp = s.Pop();
  80.                 forreturn.Push(temp);
  81.                 temp2 = temp;
  82.                 if (longest == null)
  83.                     longest = temp;
  84.                 while (temp != null)
  85.                 {
  86.                     count++;
  87.                     temp = temp.GetNext();
  88.                 }
  89.                 if (count > count2)
  90.                 {
  91.                     longest = temp2;
  92.                     count2 = count;
  93.                 }
  94.             }
  95.             while (!forreturn.IsEmpty())
  96.                 s.Push(forreturn.Pop());
  97.             return longest;
  98.         }
  99.  
  100.         // Q4
  101.         public static Stack<int> BiggestInNode(Stack<Node<int>> stk)
  102.         {
  103.             Console.WriteLine("Biggest Numbers:");
  104.             Stack<int> NumStack = new Stack<int>();
  105.             Stack<int> tempStack = new Stack<int>();
  106.             Stack<Node<int>> forreturn = new Stack<Node<int>>();
  107.             Node<int> temp;
  108.             while (stk.IsEmpty() == false)
  109.             {
  110.                 temp = stk.Pop();
  111.                 forreturn.Push(temp);
  112.                 int max = temp.GetInfo();
  113.                 while (temp != null)
  114.                 {
  115.                     if (temp.GetInfo() > max)
  116.                     {
  117.                         max = temp.GetInfo();
  118.                     }
  119.                     temp = temp.GetNext();
  120.                 }
  121.                 tempStack.Push(max);
  122.             }
  123.             while (!tempStack.IsEmpty())
  124.                 NumStack.Push(tempStack.Pop());
  125.             while (!forreturn.IsEmpty())
  126.                 stk.Push(forreturn.Pop());
  127.             return NumStack;
  128.         }
  129.  
  130.         //Q5
  131.         public static void DeleteShortest(Stack<Node<int>> s)
  132.         {
  133.             int count, count2 = int.MaxValue;
  134.             Stack<Node<int>> forreturn = new Stack<Node<int>>();
  135.             Node<int> temp, temp2;
  136.             Node<int> shortest = null;
  137.             while (!s.IsEmpty())
  138.             {
  139.                 count = 0;
  140.                 temp = s.Pop();
  141.                 forreturn.Push(temp);
  142.                 temp2 = temp;
  143.                 if (shortest == null)
  144.                     shortest = temp;
  145.                 while (temp != null)
  146.                 {
  147.                     count++;
  148.                     temp = temp.GetNext();
  149.                 }
  150.                 if (count < count2)
  151.                 {
  152.                     shortest = temp2;
  153.                     count2 = count;
  154.                 }
  155.             }
  156.             while (!forreturn.IsEmpty())
  157.             {
  158.                 temp = forreturn.Pop();
  159.                 if (temp != shortest)
  160.                     s.Push(temp);
  161.             }
  162.         }
  163.  
  164.         //Q7
  165.         public static bool IsFound(Stack<Flower> s, string name)
  166.         {
  167.             while (!s.IsEmpty())
  168.                 if (s.Pop().Name == name)
  169.                     return true;
  170.             return false;
  171.         }
  172.  
  173.         //Q8
  174.         public static double TotalPrice(Node<Flower> flwrs)
  175.         {
  176.             double sum = 0;
  177.             while (flwrs != null)
  178.             {
  179.                 sum = sum + flwrs.GetInfo().Price;
  180.                 flwrs = flwrs.GetNext();
  181.             }
  182.             return sum;
  183.         }
  184.  
  185.        //Q9
  186.         public static Flower HighestFlower(Node<Stack<Flower>> flwrs)
  187.         {
  188.             double max = 0;
  189.             Stack<Flower> temp;
  190.             double prc = 0;
  191.             Flower big = new Flower("temp", "temp", 0);
  192.             Flower tempflower;
  193.             while (flwrs != null)
  194.             {
  195.                 temp = flwrs.GetInfo();
  196.                 max = temp.Top().Price;
  197.                 while (temp.IsEmpty() == false)
  198.                 {
  199.                     tempflower = temp.Pop();
  200.                     prc = tempflower.Price;
  201.                     if (prc > max)
  202.                     {
  203.                         max = prc;
  204.                         big = tempflower;
  205.                     }
  206.                 }
  207.                 flwrs = flwrs.GetNext();
  208.             }
  209.             return big;
  210.         }
  211.  
  212.         static void Main(string[] args)
  213.         {
  214.             Node<int> n = RandomNode();
  215.             Stack<Node<int>> stk = NodeStack();
  216.             Console.WriteLine("\nStack of nodes:");
  217.             PrintNodeStack(stk);
  218.             Console.WriteLine("\nLongest node first value");
  219.             Console.WriteLine(LongestNode(stk));
  220.             Console.WriteLine();
  221.             Console.WriteLine(BiggestInNode(stk));
  222.             DeleteShortest(stk);
  223.             Console.WriteLine("\nShortest list removed:");
  224.             PrintNodeStack(stk);
  225.         }
  226.     }
  227. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement