datenshiddd

How i was learning C# - random code, simple array operations

Jan 2nd, 2017
40
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.82 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.IO;
  5. using System.Text;
  6.  
  7. namespace CalculateMatrix
  8. {
  9.     class Program
  10.     {
  11.         const int ConsoleW = 120;
  12.         const int ConsoleH = 40;
  13.  
  14.         private static readonly Object __TASKLOCK__ = new Object();
  15.  
  16.         private static Random rng = new Random();
  17.         private static Dictionary<string, Sequence> watchList = new Dictionary<string, Sequence>();
  18.         private static Stopwatch watch = new Stopwatch();
  19.         private static StreamWriter stream;
  20.         private static int userXPos, userYPos, userRNG, userRNGElements;
  21.  
  22.         static void Main(string[] args)
  23.         {
  24.             Console.SetWindowSize(ConsoleW, ConsoleH);
  25.  
  26.             WelcomeMsg();
  27.  
  28.             int[,] XYmatrix = CreateMatrix(userXPos, userYPos, 1, userRNG);
  29.             Console.WriteLine($"Random number generated matrix elements sum = {CalculateMatrix(XYmatrix)}\n");
  30.  
  31.             string RandomElements = GetRandomElements(XYmatrix, userRNGElements).ToString();
  32.             string FullMatrix = ShowMatrix(XYmatrix).ToString();
  33.  
  34.             Console.WriteLine("File saving logs:\n");
  35.  
  36.             LockWrite(__TASKLOCK__, "random", RandomElements);
  37.             LockWrite(__TASKLOCK__, "fullMatrix", FullMatrix);
  38.  
  39.             Console.ReadLine();
  40.         }
  41.  
  42.         private static void LockWrite(object obj, string instance, string variable)
  43.         {
  44.             lock (obj)
  45.             {
  46.                 WriteToFile(instance, variable);
  47.                 Console.WriteLine($"Instance \"{instance}\" writed to file: {watchList[instance].filename}\nOperation time: {watchList[instance].watch}");
  48.             }
  49.         }
  50.  
  51.         private static int GetNumericValue(string message)
  52.         {
  53.             Console.Write(message);
  54.             int value;
  55.             while (!Int32.TryParse(Console.ReadLine(), out value))
  56.             {
  57.                 Console.Write("Wrong character type. You need to provide numeric value: ");
  58.             }
  59.             return value;
  60.         }
  61.  
  62.         private static void WelcomeMsg()
  63.         {
  64.             Console.ForegroundColor = ConsoleColor.White;
  65.             Console.WriteLine(@"Application will create 2D[Xpos,Ypos] array, fill it with random numbers from 1 to [RNG Amount].");
  66.             userXPos = GetNumericValue("Xpos (array max X position number): ");
  67.             userYPos = GetNumericValue("Ypos (array max Y position number): ");
  68.             userRNG = GetNumericValue("Max range for random number generator: ");
  69.             Console.WriteLine("Application will save to files user provided amount of random matrix elements.");
  70.             userRNGElements = GetNumericValue("Amount of elements: ");
  71.         }
  72.  
  73.         private static string GenerateFile(string instance)
  74.         {
  75.             string fDateElement = DateTime.Now.Day + "-" +
  76.                                   DateTime.Now.Month + "-" +
  77.                                   DateTime.Now.Year + "-" +
  78.                                   DateTime.Now.Hour + "-" +
  79.                                   DateTime.Now.Minute + "-" +
  80.                                   DateTime.Now.Second;
  81.             Guid UUID = Guid.NewGuid();
  82.             string UniqueUUID = Convert.ToBase64String(UUID.ToByteArray());
  83.             UniqueUUID = UniqueUUID.Replace("+", "");
  84.             UniqueUUID = UniqueUUID.Replace("=", "");
  85.             string filename = $"{instance}-{fDateElement}-{UniqueUUID}.txt";
  86.             return filename;
  87.         }
  88.  
  89.         private static void WriteToFile(string instance, string text)
  90.         {
  91.             watch.Start();
  92.             string fname = GenerateFile(instance);
  93.             stream = new StreamWriter(fname);
  94.             stream.Write(text);
  95.             stream.Close();
  96.             watch.Stop();
  97.             watchList.Add(instance, new Sequence { filename = fname, watch = watch.Elapsed });
  98.         }
  99.  
  100.         private static StringBuilder ShowMatrix(int[,] matrix)
  101.         {
  102.             StringBuilder item = new StringBuilder();
  103.  
  104.             for (int i = 0; i < matrix.GetLength(0); i++)
  105.             {
  106.                 for (int j = 0; j < matrix.GetLength(1); j++)
  107.                 {
  108.                     item.Append($"Item position [{i},{j}] = {matrix[i, j]}\r\n");
  109.                 }
  110.             }
  111.  
  112.             return item;
  113.         }
  114.  
  115.         private static ulong CalculateMatrix(int[,] matrix)
  116.         {
  117.             ulong sum = 0;
  118.  
  119.             for (int i = 0; i < matrix.GetLength(0); i++)
  120.             {
  121.                 for (int j = 0; j < matrix.GetLength(1); j++)
  122.                 {
  123.                     sum += Convert.ToUInt64(matrix[i, j]);
  124.                 }
  125.             }
  126.  
  127.             return sum;
  128.         }
  129.  
  130.         private static int[,] CreateMatrix(int XPos, int YPos, int RNGmin, int RNGmax)
  131.         {
  132.             int[,] matrix = new int[XPos, YPos];
  133.  
  134.             for (int i = 0; i < matrix.GetLength(0); i++)
  135.             {
  136.                 for (int j = 0; j < matrix.GetLength(1); j++)
  137.                 {
  138.                     matrix[i, j] = rng.Next(RNGmin, RNGmax);
  139.                 }
  140.             }
  141.  
  142.             return matrix;
  143.         }
  144.  
  145.         private static StringBuilder GetRandomElements(int[,] matrix, int amount)
  146.         {
  147.             StringBuilder text = new StringBuilder();
  148.             int counter = 1;
  149.  
  150.             do
  151.             {
  152.                 int cX = rng.Next(1, matrix.GetLength(0));
  153.                 int cY = rng.Next(1, matrix.GetLength(1));
  154.                 text.Append($"| Counter: {counter} | Matrix Position [{cX},{cY}] = {matrix[cX, cY]}\r\n");
  155.                 counter++;
  156.             } while (counter != amount+1);
  157.  
  158.             return text;
  159.         }
  160.     }
  161.  
  162.     struct Sequence
  163.     {
  164.         public string filename { get; set; }
  165.         public TimeSpan watch { get; set; }
  166.     }
  167. }
Add Comment
Please, Sign In to add comment