Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Diagnostics;
- using System.IO;
- using System.Text;
- namespace CalculateMatrix
- {
- class Program
- {
- const int ConsoleW = 120;
- const int ConsoleH = 40;
- private static readonly Object __TASKLOCK__ = new Object();
- private static Random rng = new Random();
- private static Dictionary<string, Sequence> watchList = new Dictionary<string, Sequence>();
- private static Stopwatch watch = new Stopwatch();
- private static StreamWriter stream;
- private static int userXPos, userYPos, userRNG, userRNGElements;
- static void Main(string[] args)
- {
- Console.SetWindowSize(ConsoleW, ConsoleH);
- WelcomeMsg();
- int[,] XYmatrix = CreateMatrix(userXPos, userYPos, 1, userRNG);
- Console.WriteLine($"Random number generated matrix elements sum = {CalculateMatrix(XYmatrix)}\n");
- string RandomElements = GetRandomElements(XYmatrix, userRNGElements).ToString();
- string FullMatrix = ShowMatrix(XYmatrix).ToString();
- Console.WriteLine("File saving logs:\n");
- LockWrite(__TASKLOCK__, "random", RandomElements);
- LockWrite(__TASKLOCK__, "fullMatrix", FullMatrix);
- Console.ReadLine();
- }
- private static void LockWrite(object obj, string instance, string variable)
- {
- lock (obj)
- {
- WriteToFile(instance, variable);
- Console.WriteLine($"Instance \"{instance}\" writed to file: {watchList[instance].filename}\nOperation time: {watchList[instance].watch}");
- }
- }
- private static int GetNumericValue(string message)
- {
- Console.Write(message);
- int value;
- while (!Int32.TryParse(Console.ReadLine(), out value))
- {
- Console.Write("Wrong character type. You need to provide numeric value: ");
- }
- return value;
- }
- private static void WelcomeMsg()
- {
- Console.ForegroundColor = ConsoleColor.White;
- Console.WriteLine(@"Application will create 2D[Xpos,Ypos] array, fill it with random numbers from 1 to [RNG Amount].");
- userXPos = GetNumericValue("Xpos (array max X position number): ");
- userYPos = GetNumericValue("Ypos (array max Y position number): ");
- userRNG = GetNumericValue("Max range for random number generator: ");
- Console.WriteLine("Application will save to files user provided amount of random matrix elements.");
- userRNGElements = GetNumericValue("Amount of elements: ");
- }
- private static string GenerateFile(string instance)
- {
- string fDateElement = DateTime.Now.Day + "-" +
- DateTime.Now.Month + "-" +
- DateTime.Now.Year + "-" +
- DateTime.Now.Hour + "-" +
- DateTime.Now.Minute + "-" +
- DateTime.Now.Second;
- Guid UUID = Guid.NewGuid();
- string UniqueUUID = Convert.ToBase64String(UUID.ToByteArray());
- UniqueUUID = UniqueUUID.Replace("+", "");
- UniqueUUID = UniqueUUID.Replace("=", "");
- string filename = $"{instance}-{fDateElement}-{UniqueUUID}.txt";
- return filename;
- }
- private static void WriteToFile(string instance, string text)
- {
- watch.Start();
- string fname = GenerateFile(instance);
- stream = new StreamWriter(fname);
- stream.Write(text);
- stream.Close();
- watch.Stop();
- watchList.Add(instance, new Sequence { filename = fname, watch = watch.Elapsed });
- }
- private static StringBuilder ShowMatrix(int[,] matrix)
- {
- StringBuilder item = new StringBuilder();
- for (int i = 0; i < matrix.GetLength(0); i++)
- {
- for (int j = 0; j < matrix.GetLength(1); j++)
- {
- item.Append($"Item position [{i},{j}] = {matrix[i, j]}\r\n");
- }
- }
- return item;
- }
- private static ulong CalculateMatrix(int[,] matrix)
- {
- ulong sum = 0;
- for (int i = 0; i < matrix.GetLength(0); i++)
- {
- for (int j = 0; j < matrix.GetLength(1); j++)
- {
- sum += Convert.ToUInt64(matrix[i, j]);
- }
- }
- return sum;
- }
- private static int[,] CreateMatrix(int XPos, int YPos, int RNGmin, int RNGmax)
- {
- int[,] matrix = new int[XPos, YPos];
- for (int i = 0; i < matrix.GetLength(0); i++)
- {
- for (int j = 0; j < matrix.GetLength(1); j++)
- {
- matrix[i, j] = rng.Next(RNGmin, RNGmax);
- }
- }
- return matrix;
- }
- private static StringBuilder GetRandomElements(int[,] matrix, int amount)
- {
- StringBuilder text = new StringBuilder();
- int counter = 1;
- do
- {
- int cX = rng.Next(1, matrix.GetLength(0));
- int cY = rng.Next(1, matrix.GetLength(1));
- text.Append($"| Counter: {counter} | Matrix Position [{cX},{cY}] = {matrix[cX, cY]}\r\n");
- counter++;
- } while (counter != amount+1);
- return text;
- }
- }
- struct Sequence
- {
- public string filename { get; set; }
- public TimeSpan watch { get; set; }
- }
- }
Add Comment
Please, Sign In to add comment