Advertisement
Margoshinka

теперь вот так

Jan 31st, 2022
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.78 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.  
  10. namespace SYATP1
  11. {
  12.    
  13.  
  14.     class Program
  15.     {  
  16.         static string[,] dataFromTheTable;
  17.         static Dictionary<string, string> groupsAndPhrases;
  18.         static HashSet<string> countingTriads;
  19.         static int numberOfPhrases;
  20.         static int numberOfNames;
  21.         static int numberOfGroups;
  22.         static HashSet<string> groups1;
  23.         static HashSet<string> groups2;
  24.         static List<string> names;
  25.         static List<ValueTuple<string, int>> phrases;
  26.         static int[] rangesByGroups;
  27.         static int k = 0;
  28.         static int randomName;
  29.          static int randomGroup ;
  30.         static int randomPhrases;
  31.         static object MissingObject = Missing.Value;
  32.         static Word.Application wordApp;
  33.         static string wish;
  34.         static string triads;
  35.         static List<int> triads_;
  36.  
  37.  
  38.         static string wordDocTemplate = @"C:\Users\margo\source\repos\SYATP1\Поздравления.docx";
  39.         static Word.Document document = null;
  40.  
  41.         static void Readingdata()
  42.         {
  43.             Excel.Application excelApp = new Excel.Application();
  44.             //excelApp.Visible = true;
  45.             string file_path = @"C:\Users\margo\source\repos\SYATP1\Входные_данные.xlsx";
  46.             Excel.Workbook wb = excelApp.Workbooks.Open(file_path);
  47.             Excel.Worksheet ws1 = wb.Worksheets["Имена"];
  48.             Excel.Worksheet ws2 = wb.Worksheets["Группы и пожелания"];
  49.             Excel.Worksheet ws3 = wb.Worksheets["Настройки"];
  50.             numberOfPhrases = ws2.Rows.CurrentRegion.EntireRow.Count;
  51.             numberOfNames = ws1.Rows.CurrentRegion.EntireRow.Count;
  52.             dataFromTheTable = new string [numberOfPhrases,2];
  53.             for (int j = 0; j < 2; j++)
  54.                 for (int i = 0; i < numberOfPhrases; i++)
  55.                     dataFromTheTable[i, j] = ws2.Cells[i + 1, j + 1].Text.ToString();
  56.             phrases = new List<ValueTuple<string, int>>();
  57.  
  58.              for (int j = 0; j < 2; j++)
  59.                 for (int i = 0; i < numberOfPhrases; i++)
  60.                  Console.WriteLine (dataFromTheTable[i, j]);
  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.                 (string, int) tuple = (item.Key, 0);
  72.                 phrases.Add(tuple);
  73.             }
  74.             groups1 = new HashSet<string>();
  75.             groups2 = new HashSet<string>();
  76.             foreach (var item in groupsAndPhrases)
  77.                 groups1.Add( item.Value);
  78.              numberOfGroups = groups1.Count;
  79.                Console.WriteLine(numberOfGroups);
  80.             rangesByGroups = new int[numberOfGroups + 1];
  81.             for (int i = 0; i < numberOfGroups + 1; i++)
  82.             {
  83.                 rangesByGroups[i] = 1;
  84.  
  85.             }
  86.  
  87.            
  88.             foreach (var item in groupsAndPhrases)
  89.             {   if (!groups2.Add(item.Value))
  90.  
  91.                     rangesByGroups[k]++;
  92.  
  93.                
  94.                 else
  95.                
  96.                      k++;
  97.                
  98.  
  99.             }
  100.            
  101.             for (int i = 1; i < numberOfGroups; i++)
  102.             {
  103.                
  104.                 rangesByGroups[i + 1] += rangesByGroups[i];
  105.                
  106.             }
  107.  
  108.  
  109.  
  110.  
  111.                 for (int i = 1; i < numberOfGroups+1; i++)
  112.            
  113.                 Console.WriteLine(rangesByGroups[i]);
  114.  
  115.            
  116.             names = new List<string>();
  117.             for (int i = 0; i < numberOfNames; i++)
  118.                 names.Add( ws1.Cells[i+1, 1].Text.ToString());
  119.             foreach (var item in names)
  120.             {
  121.                 Console.WriteLine(item);
  122.             }
  123.             excelApp.Quit();
  124.         }
  125.         static bool proverka()
  126.         {   triads_.Sort();
  127.             for (int i = 0; i < 3; i++)
  128.             {
  129.                 triads += triads_[i].ToString();
  130.             }
  131.          
  132.             Console.WriteLine(triads);
  133.             return (countingTriads.Add(triads));
  134.            
  135.         }
  136.         static void Random()
  137.         {
  138.             triads_.Clear();
  139.             triads = "";
  140.             wish = "";
  141.             Random rnd = new Random();
  142.              randomName = rnd.Next(1, numberOfNames + 1);
  143.            
  144.             do
  145.             {
  146.                 for (int j = 0; j < 3; j++)
  147.                 {
  148.  
  149.  
  150.                     randomGroup = rnd.Next(1, numberOfGroups + 1);
  151.                     ValueTuple<string, int> [] l = new ValueTuple<string, int>[rangesByGroups[randomGroup] - rangesByGroups[randomGroup - 1]];
  152.                     phrases.CopyTo(rangesByGroups[randomGroup - 1], l, 0, rangesByGroups[randomGroup] - rangesByGroups[randomGroup - 1]);
  153.                    
  154.                     int rndom = l.Min(x => x.Item2);
  155.                    
  156.                    
  157.                     int minimumValueIndex = Array.FindIndex(l,x => x.Item2==rndom);
  158.                     int index = phrases.IndexOf(l[minimumValueIndex]);
  159.                     randomPhrases = index;
  160.                    
  161.                         triads_.Add(index);
  162.                         wish = wish + phrases[index].Item1 + ", ";
  163.                     int n2 = phrases[index].Item2;
  164.                     n2++;
  165.                     string n1 = phrases[index].Item1;
  166.                     (string, int) n = (n1, n2);
  167.                     phrases[index] = n;
  168.                 }
  169.             } while (!proverka());
  170.          
  171.            
  172.            
  173.  
  174.  
  175.  
  176.             }
  177.         static void writingToTheWord()
  178.         {
  179.             //object MissingObject = Missing.Value;
  180.             /*Word.Application*/ //wordApp = new Word.Application();
  181.             //wordApp.Visible = true;
  182.             //string wordDocTemplate = @"C:\Users\margo\source\repos\SYATP1\Поздравления.docx";
  183.             //Word.Document document = null;
  184.             try
  185.             {
  186.                 //document = wordApp.Documents.Add(wordDocTemplate);
  187.                 document.Bookmarks["name"].Range.Text = names[randomName-1];
  188.                 string wishNoGaps  = wish.Substring(0, wish.Length - 2);
  189.  
  190.                 document.Bookmarks["wish"].Range.Text = wishNoGaps;
  191.                 var endOfFile = document.Bookmarks["\\endofdoc"].Range;
  192.                 endOfFile.InsertBreak(Word.WdBreakType.wdPageBreak);
  193.                 endOfFile.InsertFile(@"C:\Users\margo\source\repos\SYATP1\Поздравления.docx");
  194.             }
  195.             catch (Exception e)
  196.             {
  197.                 document.Close(Word.WdSaveOptions.wdDoNotSaveChanges);
  198.                 wordApp.Quit();
  199.                 document = null;
  200.                 wordApp = null;
  201.                 Console.WriteLine(e.Message);
  202.             }
  203.            
  204.  
  205.         }
  206.  
  207.         static void Main(string[] args)
  208.         {
  209.            
  210.             Readingdata();
  211.             wordApp = new Word.Application();
  212.             wordApp.Visible = true;
  213.             document = wordApp.Documents.Add(wordDocTemplate);
  214.             countingTriads = new HashSet<string>();
  215.             triads_ = new List<int>();
  216.             for (int i = 0; i < 100; i++)
  217.             {
  218.                 Random();
  219.                 writingToTheWord();
  220.             }
  221.  
  222.             wordApp.Quit();
  223.         }
  224.     }
  225. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement