Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using Word = Microsoft.Office.Interop.Word;
- using Excel = Microsoft.Office.Interop.Excel;
- namespace SYATP1
- {
- class WishGeneration
- {
- double x;
- double y;
- string template_path = @"C:\project\lab1_trim6_prog_data\template.docx";
- string input_data_path = @"C:\project\lab1_trim6_prog_data\input_data.xlsx" ;
- string save_doc_dir = @"C:\project\lab1_trim6_prog_data\" ;
- int number_phrases;
- int number_name;
- int number_group;
- Dictionary< string ,List<Tuple<string,int>>> phrases = new Dictionary<string, List<Tuple<string, int>> >();
- List<string> names = new List<string>();
- List<string> groups = new List<string>();
- HashSet<string> wishes = new HashSet<string>();
- public WishGeneration()
- {
- }
- public void read_data()
- {
- Excel.Application excel_application = new Excel.Application();
- try {
- Console.WriteLine("Start read names and wish...");
- Excel.Workbook work_book = excel_application.Workbooks.Open(this.input_data_path);
- Excel.Worksheet work_sheet_name = work_book.Worksheets["Имена"];
- Excel.Worksheet work_sheet_wish = work_book.Worksheets[" Группы и пожелания"];
- Excel.Worksheet work_sheet_setting = work_book.Worksheets["Настройки"];
- this.number_phrases = work_sheet_wish.Rows.CurrentRegion.EntireRow.Count;
- this.number_name = work_sheet_name.Rows.CurrentRegion.EntireRow.Count;
- string group,wish;
- HashSet<string> _groups = new HashSet<string>();
- for (int i = 0; i < this.number_phrases; i++)
- {
- group = work_sheet_wish.Cells[i + 1, 1].Text.ToString();
- wish = work_sheet_wish.Cells[i + 1, 2].Text.ToString();
- if (this.phrases.ContainsKey(group))
- {
- this.phrases[group].Add(new Tuple<string, int>(wish, 0));
- }
- else
- {
- this.phrases[group] = new List<Tuple<string, int>>();
- this.phrases[group].Add(new Tuple<string, int>(wish, 0));
- }
- }
- foreach (var item in this.phrases)
- {
- this.groups.Add(item.Key);
- }
- this.number_group = this.phrases.Count;
- //foreach (var item in this.groups)
- // Console.WriteLine(item);
- for (int i = 0; i < this.number_name; i++)
- this.names.Add(work_sheet_name.Cells[i + 1, 1].Text.ToString());
- excel_application.Quit();
- Console.WriteLine("Finish reading names and wish.");
- }
- catch (Exception e){
- Console.WriteLine("data read error(((");
- excel_application.Quit();
- }
- }
- public bool check_combinations()
- {
- HashSet<string> groups_hs = new HashSet<string>();
- int number_combinations = 0;
- for (int i = 0; i < this.groups.Count; ++i)
- for (int j = 0; j < this.groups.Count; ++j)
- for (int k = 0; k < this.groups.Count; ++k)
- {
- if (i == j || j == k || i == k)
- continue;
- int Min = Math.Min(i, Math.Min(j, k));
- int Max = Math.Max(i, Math.Max(j, k));
- int Mid = i + j + k - Min - Max;
- string groups_str = String.Format("{0}, {1}, {2}", Min, Mid, Max);
- if (!groups_hs.Contains(groups_str))
- {
- number_combinations += this.phrases[this.groups[i]].Count * this.phrases[this.groups[j]].Count * this.phrases[this.groups[k]].Count;
- groups_hs.Add(groups_str);
- }
- }
- Console.WriteLine("Возможных комбинаций:"+ number_combinations);
- if (number_combinations >= this.number_name)
- return true;
- else
- {
- Console.WriteLine("Возможных комбинаций меньше чем имен");
- return false;
- }
- }
- public void generate_wish()
- {
- Console.WriteLine("Start generate wish...");
- for (int j = 0; j < this.number_name; ++j)
- {
- int number_attempt = 0;
- while (true)
- {
- number_attempt++;
- if (number_attempt > 100000)
- {
- Console.WriteLine("нельзя сгенрить");
- return;
- }
- string wish = "";
- Random rnd = new Random();
- string group_1 = this.groups[rnd.Next(0, this.number_group)];
- string group_2 = this.groups[rnd.Next(0, this.number_group)];
- string group_3 = this.groups[rnd.Next(0, this.number_group)];
- if (group_1 == group_2 || group_1 == group_3 || group_2 == group_3)
- continue;
- var phrase_1 = this.phrases[group_1].Where((x) => x.Item2 == this.phrases[group_1].Min(y => y.Item2)).ToList()[0];
- var phrase_2 = this.phrases[group_2].Where((x) => x.Item2 == this.phrases[group_2].Min(y => y.Item2)).ToList()[0];
- var phrase_3 = this.phrases[group_3].Where((x) => x.Item2 == this.phrases[group_3].Min(y => y.Item2)).ToList()[0];
- List<string> sort_phrases = new List<string>();
- sort_phrases.Add(phrase_1.Item1);
- sort_phrases.Add(phrase_2.Item1);
- sort_phrases.Add(phrase_3.Item1);
- sort_phrases.Sort();
- wish = String.Format("{0}, {1}, {2} !!!", sort_phrases[0], sort_phrases[1], sort_phrases[2]);
- if (wishes.Contains(wish))
- continue;
- else
- {
- //Console.WriteLine(wish);
- wishes.Add(wish);
- var index = this.phrases[group_1].FindIndex(x => x.Item1 == phrase_1.Item1);
- this.phrases[group_1][index] = Tuple.Create(phrase_1.Item1, phrase_1.Item2 + 1);
- index = this.phrases[group_2].FindIndex(x => x.Item1 == phrase_2.Item1);
- this.phrases[group_2][index] = Tuple.Create(phrase_2.Item1, phrase_2.Item2 + 1);
- index = this.phrases[group_3].FindIndex(x => x.Item1 == phrase_3.Item1);
- this.phrases[group_3][index] = Tuple.Create(phrase_3.Item1, phrase_3.Item2 + 1);
- break;
- }
- }
- }
- Console.WriteLine("Finish generate wish.");
- Console.WriteLine("number wish: " + wishes.Count);
- }
- public string RandomString(int length=10)
- {
- Random random = new Random();
- const string chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
- return new string(Enumerable.Repeat(chars, length)
- .Select(s => s[random.Next(s.Length)]).ToArray());
- }
- public void create_word()
- {
- Console.WriteLine("Start create word...");
- Word.Application wordApp = new Word.Application();
- Word.Document document = wordApp.Documents.Add(this.template_path);
- try
- {
- List<string> wishes_list = this.wishes.ToList();
- for (int i = 0; i < this.number_name; ++i)
- {
- document.Bookmarks["name"].Range.Text = this.names[i];
- document.Bookmarks["wish"].Range.Text = wishes_list[i];
- var endOfFile = document.Bookmarks["\\endofdoc"].Range;
- endOfFile.InsertBreak(Word.WdBreakType.wdPageBreak);
- endOfFile.InsertFile(this.template_path);
- }
- Word.Range rng = document.Bookmarks["alltext"].Range;
- rng.Font.Size = 14;
- rng.Font.Name = "Italic";
- string fileName = RandomString();
- document.SaveAs2(this.save_doc_dir + fileName);
- document.Close(Word.WdSaveOptions.wdDoNotSaveChanges);
- wordApp.Quit();
- document = null;
- wordApp = null;
- }
- catch (Exception e)
- {
- document.Close(Word.WdSaveOptions.wdDoNotSaveChanges);
- wordApp.Quit();
- document = null;
- wordApp = null;
- Console.WriteLine(e.Message);
- }
- Console.WriteLine("Finish create word.");
- }
- }
- class Program
- {
- static void Main(string[] args)
- {
- //
- WishGeneration wish_generation = new WishGeneration();
- wish_generation.read_data();
- if (!wish_generation.check_combinations()) return;
- wish_generation.generate_wish();
- wish_generation.create_word();
- Console.ReadKey();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement