AmidamaruZXC

Untitled

Jun 6th, 2020
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.02 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Xml.Serialization;
  6. using System.Runtime.Serialization.Json;
  7. using System.Threading.Tasks;
  8. using MessageLib;
  9. using System.Runtime.InteropServices;
  10. using System.IO;
  11.  
  12. namespace ConsoleApp1
  13. {
  14.     public class Program
  15.     {
  16.         // Экземпляр класса Random для генерации случайных чисел
  17.         private static Random random = new Random();
  18.  
  19.         private const string Pattern = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789 ";
  20.  
  21.         private static string GetRandomContent(int length)
  22.         {
  23.             string result = "";
  24.             for (var i = 0; i < length; i++)
  25.                 result += Pattern[random.Next(0, Pattern.Length)];
  26.             return result;
  27.         }
  28.  
  29.         static void Main(string[] args)
  30.         {
  31.             // Цикл повтора решения
  32.             do
  33.             {
  34.                 Console.Clear(); // Очистка консоли
  35.  
  36.                 Console.Write("Введите число n(n >= 10 && n <= 1000): ");
  37.                 uint n;
  38.                 while (!uint.TryParse(Console.ReadLine(), out n) || n < 10 || n > 1000)
  39.                 {
  40.                     Console.WriteLine("Неверные входные данные!");
  41.                     Console.Write("Введите число n(n >= 10 && n <= 1000): ");
  42.                 }
  43.                 MessageBox messageBox = new MessageBox();
  44.  
  45.                 uint count = 0;
  46.                 while (count != n)
  47.                     try
  48.                     {
  49.                         Message tempMsg = new Message(GetRandomContent(random.Next(5, 100 + 1)), DateTime.Now);
  50.                         messageBox.ReceiveMail(tempMsg);
  51.                         count++;
  52.                     }
  53.                     catch (Exception ex)
  54.                     {
  55.                         Console.WriteLine(ex.Message);
  56.                     }
  57.  
  58.                 n /= 2;
  59.                 count = 0;
  60.                 while (count != n)
  61.                 {
  62.                     Dmessage tempMsg = new Dmessage(GetRandomContent(random.Next(5, 36 + 1)), DateTime.Now, random.Next(1, 1000 + 1));
  63.                     messageBox.ReceiveMail(tempMsg);
  64.                     count++;
  65.                 }
  66.  
  67.                 foreach (var message in messageBox)
  68.                     Console.WriteLine(message.ToString());
  69.  
  70.                 DataContractJsonSerializer jsonFormatter = new DataContractJsonSerializer(typeof(MessageBox));
  71.  
  72.  
  73.                 using (FileStream fs = new FileStream("messageBox.json", FileMode.Create))
  74.                 {
  75.                     jsonFormatter.WriteObject(fs, messageBox);
  76.                 }
  77.  
  78.                 Console.WriteLine("Для завершения работы нажмите Esc...\nДля продолжения любую иную клавишу...");
  79.             }
  80.             while (Console.ReadKey(true).Key != ConsoleKey.Escape);
  81.         }
  82.     }
  83. }
Advertisement
Add Comment
Please, Sign In to add comment