Advertisement
Margoshinka

ну какова красота

Feb 1st, 2022
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 11.38 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using Word = Microsoft.Office.Interop.Word;
  7. using Excel = Microsoft.Office.Interop.Excel;
  8. using System.Reflection;
  9. using System.IO;
  10.  
  11.  
  12. namespace SYATP1
  13. {
  14.    
  15.  
  16.     class Program
  17.     {  
  18.         static string[,] dataFromTheTable;
  19.         static Dictionary<string, string> groupsAndPhrases;
  20.         static HashSet<string> countingTriads;
  21.         static int numberOfPhrases;
  22.         static int numberOfNames;
  23.         static int numberOfGroups;
  24.         static HashSet<string> groups1;
  25.         static HashSet<string> groups2;
  26.         static List<string> names;
  27.         static List<ValueTuple<string, int>> phrases;
  28.         static int[] rangesByGroups;
  29.         static int k = 0;
  30.         static int randomGroup ;
  31.         static int randomPhrases;
  32.         static object MissingObject = Missing.Value;
  33.         static Word.Application wordApp;
  34.         static string wish;
  35.         static string triads;
  36.         static List<int> triads_;
  37.         static List<string> wishes;
  38.         static string font;
  39.  
  40.         static string wordDocTemplate;
  41.         static Word.Document document = null;
  42.  
  43.         static void ReadingData()
  44.         {
  45.             Excel.Application excelApp = new Excel.Application();
  46.             //excelApp.Visible = true;
  47.             string file_path = @"C:\Users\margo\source\repos\SYATP1\Входные_данные.xlsx";
  48.             Excel.Workbook wb = excelApp.Workbooks.Open(file_path);
  49.             Excel.Worksheet ws1 = wb.Worksheets["Имена"];
  50.             Excel.Worksheet ws2 = wb.Worksheets["Группы и пожелания"];
  51.             Excel.Worksheet ws3 = wb.Worksheets["Настройки"];
  52.             numberOfPhrases = ws2.Rows.CurrentRegion.EntireRow.Count;
  53.             numberOfNames = ws1.Rows.CurrentRegion.EntireRow.Count;
  54.             dataFromTheTable = new string [numberOfPhrases,2];
  55.             for (int j = 0; j < 2; j++)
  56.                 for (int i = 0; i < numberOfPhrases; i++)
  57.                     dataFromTheTable[i, j] = ws2.Cells[i + 1, j + 1].Text.ToString();
  58.             phrases = new List<ValueTuple<string, int>>();
  59.             wordDocTemplate = ws3.Cells[1, 1].Text.ToString();
  60.             font = ws3.Cells[2, 1].Text.ToString();
  61.             groupsAndPhrases = new Dictionary<string, string>();
  62.             for (int i = 0; i < numberOfPhrases; i++)
  63.             {
  64.                 groupsAndPhrases.Add(dataFromTheTable[i, 1], dataFromTheTable[i, 0]);
  65.             }
  66.            
  67.             groupsAndPhrases = groupsAndPhrases.OrderBy(pair => pair.Value).ToDictionary(pair => pair.Key, pair => pair.Value);
  68.  
  69.             foreach (var item in groupsAndPhrases)
  70.             {
  71.                 Console.WriteLine(item.Key);
  72.                 (string, int) tuple = (item.Key, 0);
  73.                 phrases.Add(tuple);
  74.             }
  75.             groups1 = new HashSet<string>();
  76.             groups2 = new HashSet<string>();
  77.             foreach (var item in groupsAndPhrases)
  78.                 groups1.Add( item.Value);
  79.              numberOfGroups = groups1.Count;
  80.                Console.WriteLine(numberOfGroups);
  81.             rangesByGroups = new int[numberOfGroups + 1];
  82.             for (int i = 0; i < numberOfGroups + 1; i++)
  83.             {
  84.                 rangesByGroups[i] = 1;
  85.  
  86.             }
  87.  
  88.            
  89.             foreach (var item in groupsAndPhrases)
  90.             {   if (!groups2.Add(item.Value))
  91.  
  92.                     rangesByGroups[k]++;
  93.  
  94.                
  95.                 else
  96.                
  97.                      k++;
  98.                
  99.  
  100.             }
  101.            
  102.             for (int i = 1; i < numberOfGroups; i++)
  103.             {
  104.                
  105.                 rangesByGroups[i + 1] += rangesByGroups[i];
  106.                
  107.             }
  108.  
  109.  
  110.  
  111.  
  112.                 for (int i = 1; i < numberOfGroups+1; i++)
  113.            
  114.                 Console.WriteLine(rangesByGroups[i]);
  115.  
  116.            
  117.             names = new List<string>();
  118.             for (int i = 0; i < numberOfNames; i++)
  119.                 names.Add( ws1.Cells[i+1, 1].Text.ToString());
  120.             foreach (var item in names)
  121.             {
  122.                 Console.WriteLine(item);
  123.             }
  124.             excelApp.Quit();
  125.         }
  126.         static bool Proverka()
  127.         {   triads_.Sort();
  128.             for (int i = 0; i < 3; i++)
  129.             {
  130.                 triads += triads_[i].ToString();
  131.             }
  132.          
  133.             Console.WriteLine(triads);
  134.             bool ok = countingTriads.Add(triads);
  135.             if (!ok)
  136.             {
  137.                 triads_.Clear();
  138.                 triads = "";
  139.                 wish = "";
  140.             }
  141.             return ok;
  142.            
  143.         }
  144.         static void Random()
  145.         {
  146.             triads_.Clear();
  147.             triads = "";
  148.             wish = "";
  149.             Random rnd = new Random();
  150.              
  151.            
  152.             do
  153.             {
  154.                 for (int j = 0; j < 3; j++)
  155.                 {
  156.  
  157.  
  158.                     randomGroup = rnd.Next(1, numberOfGroups + 1);
  159.                     ValueTuple<string, int> [] l = new ValueTuple<string, int>[rangesByGroups[randomGroup] - rangesByGroups[randomGroup - 1]];
  160.                     phrases.CopyTo(rangesByGroups[randomGroup - 1], l, 0, rangesByGroups[randomGroup] - rangesByGroups[randomGroup - 1]);
  161.                    
  162.                     int rndom = l.Min(x => x.Item2);
  163.                    
  164.                    
  165.                     int minimumValueIndex = Array.FindIndex(l,x => x.Item2==rndom);
  166.                     int index = phrases.IndexOf(l[minimumValueIndex]);
  167.                     randomPhrases = index;
  168.                    
  169.                         triads_.Add(index);
  170.                         wish = wish + phrases[index].Item1 + ", ";
  171.                     int n2 = phrases[index].Item2;
  172.                     n2++;
  173.                     string n1 = phrases[index].Item1;
  174.                     (string, int) n = (n1, n2);
  175.                     phrases[index] = n;
  176.                 }
  177.             } while (!Proverka());
  178.             string wishNoGaps = wish.Substring(0, wish.Length - 2);
  179.             wishes.Add(wishNoGaps);
  180.            
  181.  
  182.  
  183.  
  184.             }
  185.         static void NewWordFile()
  186.         {
  187.             wordApp = new Word.Application();
  188.             wordApp.Visible = true;
  189.             document = wordApp.Documents.Add(wordDocTemplate);
  190.         }
  191.        static void NewPageInTheWord()
  192.         {
  193.             try
  194.             {
  195.                 var endOfFile = document.Bookmarks["\\endofdoc"].Range;
  196.                 endOfFile.InsertBreak(Word.WdBreakType.wdPageBreak);
  197.                 endOfFile.InsertFile(@wordDocTemplate);
  198.             }
  199.             catch (Exception e)
  200.             {
  201.                 document.Close(Word.WdSaveOptions.wdDoNotSaveChanges);
  202.                 wordApp.Quit();
  203.                 document = null;
  204.                 wordApp = null;
  205.                 Console.WriteLine(e.Message);
  206.             }
  207.         }
  208.         static void WritingToTheWord(int i)
  209.         {
  210.             //object MissingObject = Missing.Value;
  211.             /*Word.Application*/ //wordApp = new Word.Application();
  212.             //wordApp.Visible = true;
  213.             //string wordDocTemplate = @"C:\Users\margo\source\repos\SYATP1\Поздравления.docx";
  214.             //Word.Document document = null;
  215.             try
  216.             {
  217.                 //document = wordApp.Documents.Add(wordDocTemplate);
  218.                 Word.Range rng = document.Bookmarks["alltext"].Range;
  219.                 rng.Font.Size = 55;
  220.                 rng.Font.Name = font;
  221.                
  222.                
  223.                 document.Bookmarks["name"].Range.Text = names[i];
  224.                
  225.  
  226.                 document.Bookmarks["wish"].Range.Text = wishes[i];
  227.                
  228.                 /* var endOfFile = document.Bookmarks["\\endofdoc"].Range;
  229.                  endOfFile.InsertBreak(Word.WdBreakType.wdPageBreak);
  230.                  endOfFile.InsertFile(@"C:\Users\margo\source\repos\SYATP1\Поздравления.docx");*/
  231.             }
  232.             catch (Exception e)
  233.             {
  234.                 document.Close(Word.WdSaveOptions.wdDoNotSaveChanges);
  235.                 wordApp.Quit();
  236.                 document = null;
  237.                 wordApp = null;
  238.                 Console.WriteLine(e.Message);
  239.             }
  240.            
  241.  
  242.         }
  243.         static string RandomString()
  244.         {
  245.             Random rnd = new Random();
  246.             int length = rnd.Next(5, 10);
  247.  
  248.             string str = "";
  249.  
  250.  
  251.             while (length != 0)
  252.             {
  253.                 str += (char)rnd.Next(97, 123);
  254.                 length--;
  255.             }
  256.             Console.WriteLine(str);
  257.             return str;
  258.            // Console.ReadKey();
  259.         }
  260.        
  261.         static void SaveDocument()
  262.         {
  263.             string fileName = RandomString();
  264.             try { document.SaveAs2(@"C:\Users\margo\source\repos\SYATP1\Сгенерированные поздравления\" + fileName); }
  265.             catch (System.Runtime.InteropServices.COMException)
  266.             { Directory.CreateDirectory(@"C:\Users\margo\source\repos\SYATP1\Сгенерированные поздравления");
  267.                 document.SaveAs2(@"C:\Users\margo\source\repos\SYATP1\Сгенерированные поздравления\" + fileName);
  268.             }
  269.            
  270.             document.Close(Word.WdSaveOptions.wdDoNotSaveChanges);
  271.             wordApp.Quit();
  272.             document = null;
  273.             wordApp = null;
  274.         }
  275.        static int Cominations(int allNumbers, int perGroup)
  276.         {
  277.             if (allNumbers > 13)
  278.             {
  279.                 Console.WriteLine("Too big number!");
  280.                 return -1;
  281.             }
  282.  
  283.             return Factorial(allNumbers) / Factorial(allNumbers - perGroup);
  284.         }
  285.  
  286.         static int Factorial(int number)
  287.         {
  288.             int n = 1;
  289.  
  290.             for (int i = 1; i < number; i++)
  291.             {
  292.                 n *= i;
  293.             }
  294.  
  295.             return n;
  296.         }
  297.             static void Main(string[] args)
  298.         {
  299.             string buf;
  300.             int x=0;
  301.             bool ok;
  302.             do
  303.             {
  304.                 try
  305.                 {
  306.                     Console.WriteLine("Введите количество людей для оценки");
  307.                     buf = Console.ReadLine();
  308.                     x = int.Parse(buf);
  309.                     ok = true;
  310.                 }
  311.                 catch (FormatException)
  312.                 {
  313.                     Console.WriteLine("Error");
  314.                     ok = false;
  315.                 }
  316.             } while (!ok);
  317.             int c = Cominations(x,3);
  318.             ReadingData();
  319.             NewWordFile();
  320.             countingTriads = new HashSet<string>();
  321.             triads_ = new List<int>();
  322.             wishes = new List<string>();
  323.             for (int i = 0; i < 20; i++)
  324.             {
  325.                 Random();
  326.                
  327.             }
  328.             WritingToTheWord(0);
  329.             for (int i = 1; i < 20; i++)
  330.             {
  331.                 NewPageInTheWord();
  332.                 WritingToTheWord(i);
  333.                
  334.             }
  335.             SaveDocument();
  336.            
  337.  
  338.             Console.WriteLine("Генерация окончена");
  339.             Console.ReadKey();
  340.         }
  341.     }
  342. }
  343.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement