Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using Word = Microsoft.Office.Interop.Word;
- using Excel = Microsoft.Office.Interop.Excel;
- using System.Reflection;
- namespace SYATP1
- {
- class Program
- {
- static string[,] dataFromTheTable;
- static Dictionary<string, string> groupsAndPhrases;
- static HashSet<string> countingTriads;
- static int numberOfPhrases;
- static int numberOfNames;
- static int numberOfGroups;
- static HashSet<string> groups1;
- static HashSet<string> groups2;
- static List<string> names;
- static List<ValueTuple<string, int>> phrases;
- static int[] rangesByGroups;
- static int k = 0;
- static int randomName;
- static int randomGroup ;
- static int randomPhrases;
- static object MissingObject = Missing.Value;
- static Word.Application wordApp;
- static string wish;
- static string triads;
- static List<int> triads_;
- static string wordDocTemplate = @"C:\Users\margo\source\repos\SYATP1\Поздравления.docx";
- static Word.Document document = null;
- static void Readingdata()
- {
- Excel.Application excelApp = new Excel.Application();
- //excelApp.Visible = true;
- string file_path = @"C:\Users\margo\source\repos\SYATP1\Входные_данные.xlsx";
- Excel.Workbook wb = excelApp.Workbooks.Open(file_path);
- Excel.Worksheet ws1 = wb.Worksheets["Имена"];
- Excel.Worksheet ws2 = wb.Worksheets["Группы и пожелания"];
- Excel.Worksheet ws3 = wb.Worksheets["Настройки"];
- numberOfPhrases = ws2.Rows.CurrentRegion.EntireRow.Count;
- numberOfNames = ws1.Rows.CurrentRegion.EntireRow.Count;
- dataFromTheTable = new string [numberOfPhrases,2];
- for (int j = 0; j < 2; j++)
- for (int i = 0; i < numberOfPhrases; i++)
- dataFromTheTable[i, j] = ws2.Cells[i + 1, j + 1].Text.ToString();
- phrases = new List<ValueTuple<string, int>>();
- for (int j = 0; j < 2; j++)
- for (int i = 0; i < numberOfPhrases; i++)
- Console.WriteLine (dataFromTheTable[i, j]);
- groupsAndPhrases = new Dictionary<string, string>();
- for (int i = 0; i < numberOfPhrases; i++)
- {
- groupsAndPhrases.Add(dataFromTheTable[i, 1], dataFromTheTable[i, 0]);
- }
- groupsAndPhrases = groupsAndPhrases.OrderBy(pair => pair.Value).ToDictionary(pair => pair.Key, pair => pair.Value);
- foreach (var item in groupsAndPhrases)
- {
- (string, int) tuple = (item.Key, 0);
- phrases.Add(tuple);
- }
- groups1 = new HashSet<string>();
- groups2 = new HashSet<string>();
- foreach (var item in groupsAndPhrases)
- groups1.Add( item.Value);
- numberOfGroups = groups1.Count;
- Console.WriteLine(numberOfGroups);
- rangesByGroups = new int[numberOfGroups + 1];
- for (int i = 0; i < numberOfGroups + 1; i++)
- {
- rangesByGroups[i] = 1;
- }
- foreach (var item in groupsAndPhrases)
- { if (!groups2.Add(item.Value))
- rangesByGroups[k]++;
- else
- k++;
- }
- for (int i = 1; i < numberOfGroups; i++)
- {
- rangesByGroups[i + 1] += rangesByGroups[i];
- }
- for (int i = 1; i < numberOfGroups+1; i++)
- Console.WriteLine(rangesByGroups[i]);
- names = new List<string>();
- for (int i = 0; i < numberOfNames; i++)
- names.Add( ws1.Cells[i+1, 1].Text.ToString());
- foreach (var item in names)
- {
- Console.WriteLine(item);
- }
- excelApp.Quit();
- }
- static bool proverka()
- { triads_.Sort();
- for (int i = 0; i < 3; i++)
- {
- triads += triads_[i].ToString();
- }
- Console.WriteLine(triads);
- return (countingTriads.Add(triads));
- }
- static void Random()
- {
- triads_.Clear();
- triads = "";
- wish = "";
- Random rnd = new Random();
- randomName = rnd.Next(1, numberOfNames + 1);
- do
- {
- for (int j = 0; j < 3; j++)
- {
- randomGroup = rnd.Next(1, numberOfGroups + 1);
- ValueTuple<string, int> [] l = new ValueTuple<string, int>[rangesByGroups[randomGroup] - rangesByGroups[randomGroup - 1]];
- phrases.CopyTo(rangesByGroups[randomGroup - 1], l, 0, rangesByGroups[randomGroup] - rangesByGroups[randomGroup - 1]);
- int rndom = l.Min(x => x.Item2);
- int minimumValueIndex = Array.FindIndex(l,x => x.Item2==rndom);
- int index = phrases.IndexOf(l[minimumValueIndex]);
- randomPhrases = index;
- triads_.Add(index);
- wish = wish + phrases[index].Item1 + ", ";
- int n2 = phrases[index].Item2;
- n2++;
- string n1 = phrases[index].Item1;
- (string, int) n = (n1, n2);
- phrases[index] = n;
- }
- } while (!proverka());
- }
- static void writingToTheWord()
- {
- //object MissingObject = Missing.Value;
- /*Word.Application*/ //wordApp = new Word.Application();
- //wordApp.Visible = true;
- //string wordDocTemplate = @"C:\Users\margo\source\repos\SYATP1\Поздравления.docx";
- //Word.Document document = null;
- try
- {
- //document = wordApp.Documents.Add(wordDocTemplate);
- document.Bookmarks["name"].Range.Text = names[randomName-1];
- string wishNoGaps = wish.Substring(0, wish.Length - 2);
- document.Bookmarks["wish"].Range.Text = wishNoGaps;
- var endOfFile = document.Bookmarks["\\endofdoc"].Range;
- endOfFile.InsertBreak(Word.WdBreakType.wdPageBreak);
- endOfFile.InsertFile(@"C:\Users\margo\source\repos\SYATP1\Поздравления.docx");
- }
- catch (Exception e)
- {
- document.Close(Word.WdSaveOptions.wdDoNotSaveChanges);
- wordApp.Quit();
- document = null;
- wordApp = null;
- Console.WriteLine(e.Message);
- }
- }
- static void Main(string[] args)
- {
- Readingdata();
- wordApp = new Word.Application();
- wordApp.Visible = true;
- document = wordApp.Documents.Add(wordDocTemplate);
- countingTriads = new HashSet<string>();
- triads_ = new List<int>();
- for (int i = 0; i < 100; i++)
- {
- Random();
- writingToTheWord();
- }
- wordApp.Quit();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement