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;
- using System.IO;
- 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 randomGroup ;
- static int randomPhrases;
- static object MissingObject = Missing.Value;
- static Word.Application wordApp;
- static string wish;
- static string triads;
- static List<int> triads_;
- static List<string> wishes;
- static string font;
- static string wordDocTemplate;
- 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>>();
- wordDocTemplate = ws3.Cells[1, 1].Text.ToString();
- font = ws3.Cells[2, 1].Text.ToString();
- 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)
- {
- Console.WriteLine(item.Key);
- (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);
- bool ok = countingTriads.Add(triads);
- if (!ok)
- {
- triads_.Clear();
- triads = "";
- wish = "";
- }
- return ok;
- }
- static void Random()
- {
- triads_.Clear();
- triads = "";
- wish = "";
- Random rnd = new Random();
- 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());
- string wishNoGaps = wish.Substring(0, wish.Length - 2);
- wishes.Add(wishNoGaps);
- }
- static void NewWordFile()
- {
- wordApp = new Word.Application();
- wordApp.Visible = true;
- document = wordApp.Documents.Add(wordDocTemplate);
- }
- static void NewPageInTheWord()
- {
- try
- {
- var endOfFile = document.Bookmarks["\\endofdoc"].Range;
- endOfFile.InsertBreak(Word.WdBreakType.wdPageBreak);
- endOfFile.InsertFile(@wordDocTemplate);
- }
- catch (Exception e)
- {
- document.Close(Word.WdSaveOptions.wdDoNotSaveChanges);
- wordApp.Quit();
- document = null;
- wordApp = null;
- Console.WriteLine(e.Message);
- }
- }
- static void WritingToTheWord(int i)
- {
- //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);
- Word.Range rng = document.Bookmarks["alltext"].Range;
- rng.Font.Size = 55;
- rng.Font.Name = font;
- document.Bookmarks["name"].Range.Text = names[i];
- document.Bookmarks["wish"].Range.Text = wishes[i];
- /* 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 string RandomString()
- {
- Random rnd = new Random();
- int length = rnd.Next(5, 10);
- string str = "";
- while (length != 0)
- {
- str += (char)rnd.Next(97, 123);
- length--;
- }
- Console.WriteLine(str);
- return str;
- // Console.ReadKey();
- }
- static void SaveDocument()
- {
- string fileName = RandomString();
- try { document.SaveAs2(@"C:\Users\margo\source\repos\SYATP1\Сгенерированные поздравления\" + fileName); }
- catch (System.Runtime.InteropServices.COMException)
- { Directory.CreateDirectory(@"C:\Users\margo\source\repos\SYATP1\Сгенерированные поздравления");
- document.SaveAs2(@"C:\Users\margo\source\repos\SYATP1\Сгенерированные поздравления\" + fileName);
- }
- document.Close(Word.WdSaveOptions.wdDoNotSaveChanges);
- wordApp.Quit();
- document = null;
- wordApp = null;
- }
- static int Cominations(int allNumbers, int perGroup)
- {
- if (allNumbers > 13)
- {
- Console.WriteLine("Too big number!");
- return -1;
- }
- return Factorial(allNumbers) / Factorial(allNumbers - perGroup);
- }
- static int Factorial(int number)
- {
- int n = 1;
- for (int i = 1; i < number; i++)
- {
- n *= i;
- }
- return n;
- }
- static void Main(string[] args)
- {
- string buf;
- int x=0;
- bool ok;
- do
- {
- try
- {
- Console.WriteLine("Введите количество людей для оценки");
- buf = Console.ReadLine();
- x = int.Parse(buf);
- ok = true;
- }
- catch (FormatException)
- {
- Console.WriteLine("Error");
- ok = false;
- }
- } while (!ok);
- int c = Cominations(x,3);
- ReadingData();
- NewWordFile();
- countingTriads = new HashSet<string>();
- triads_ = new List<int>();
- wishes = new List<string>();
- for (int i = 0; i < 20; i++)
- {
- Random();
- }
- WritingToTheWord(0);
- for (int i = 1; i < 20; i++)
- {
- NewPageInTheWord();
- WritingToTheWord(i);
- }
- SaveDocument();
- Console.WriteLine("Генерация окончена");
- Console.ReadKey();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement