Advertisement
VladSmirN

lab1_langprog

Feb 5th, 2022
1,244
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 9.92 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using Word = Microsoft.Office.Interop.Word;
  5. using Excel = Microsoft.Office.Interop.Excel;
  6.  
  7.  
  8. namespace SYATP1
  9. {
  10.     class WishGeneration
  11.     {
  12.         double x;
  13.         double y;
  14.         string template_path = @"C:\project\lab1_trim6_prog_data\template.docx";
  15.         string input_data_path = @"C:\project\lab1_trim6_prog_data\input_data.xlsx" ;
  16.         string save_doc_dir = @"C:\project\lab1_trim6_prog_data\" ;
  17.         int number_phrases;
  18.         int number_name;
  19.         int number_group;
  20.  
  21.         Dictionary< string ,List<Tuple<string,int>>> phrases = new Dictionary<string, List<Tuple<string, int>> >();
  22.         List<string> names = new List<string>();
  23.         List<string> groups = new List<string>();
  24.         HashSet<string> wishes = new HashSet<string>();
  25.         public WishGeneration()
  26.         {
  27.  
  28.         }
  29.         public  void read_data()
  30.         {
  31.             Excel.Application excel_application = new Excel.Application();
  32.  
  33.             try {
  34.  
  35.                 Console.WriteLine("Start read names and wish...");
  36.  
  37.  
  38.                 Excel.Workbook work_book = excel_application.Workbooks.Open(this.input_data_path);
  39.                 Excel.Worksheet work_sheet_name = work_book.Worksheets["Имена"];
  40.                 Excel.Worksheet work_sheet_wish = work_book.Worksheets[" Группы и пожелания"];
  41.                 Excel.Worksheet work_sheet_setting = work_book.Worksheets["Настройки"];
  42.  
  43.                 this.number_phrases = work_sheet_wish.Rows.CurrentRegion.EntireRow.Count;
  44.                 this.number_name = work_sheet_name.Rows.CurrentRegion.EntireRow.Count;
  45.  
  46.                 string group,wish;
  47.                 HashSet<string> _groups = new HashSet<string>();
  48.                 for (int i = 0; i < this.number_phrases; i++)
  49.                 {
  50.                     group = work_sheet_wish.Cells[i + 1, 1].Text.ToString();
  51.                     wish = work_sheet_wish.Cells[i + 1, 2].Text.ToString();
  52.  
  53.  
  54.                     if (this.phrases.ContainsKey(group))
  55.                     {
  56.                         this.phrases[group].Add(new Tuple<string, int>(wish, 0));
  57.                     }
  58.                     else
  59.                     {
  60.                         this.phrases[group] = new List<Tuple<string, int>>();
  61.                         this.phrases[group].Add(new Tuple<string, int>(wish, 0));
  62.                     }
  63.                          
  64.  
  65.                 }
  66.                 foreach (var item in this.phrases)
  67.                 {
  68.                     this.groups.Add(item.Key);
  69.                 }
  70.                 this.number_group = this.phrases.Count;
  71.                 //foreach (var item in this.groups)
  72.                  //   Console.WriteLine(item);
  73.  
  74.                 for (int i = 0; i < this.number_name; i++)
  75.                     this.names.Add(work_sheet_name.Cells[i + 1, 1].Text.ToString());
  76.  
  77.                 excel_application.Quit();
  78.                 Console.WriteLine("Finish reading names and wish.");
  79.  
  80.             }
  81.             catch (Exception e){
  82.            
  83.                 Console.WriteLine("data read error(((");
  84.                 excel_application.Quit();
  85.             }
  86.            
  87.  
  88.         }
  89.  
  90.         public bool check_combinations()
  91.         {
  92.  
  93.             HashSet<string> groups_hs = new HashSet<string>();
  94.             int number_combinations = 0;
  95.             for (int i = 0; i < this.groups.Count; ++i)
  96.                 for (int j = 0; j < this.groups.Count; ++j)
  97.                     for (int k = 0; k < this.groups.Count; ++k)
  98.                     {
  99.                         if (i == j || j == k || i == k)
  100.                             continue;
  101.  
  102.                         int Min  = Math.Min(i, Math.Min(j, k));
  103.                         int Max = Math.Max(i, Math.Max(j, k));
  104.                         int Mid = i + j + k - Min - Max;
  105.                         string groups_str = String.Format("{0}, {1}, {2}", Min, Mid, Max);
  106.                         if (!groups_hs.Contains(groups_str))
  107.                         {
  108.                             number_combinations += this.phrases[this.groups[i]].Count * this.phrases[this.groups[j]].Count * this.phrases[this.groups[k]].Count;
  109.                             groups_hs.Add(groups_str);
  110.                         }
  111.                     }
  112.  
  113.             Console.WriteLine("Возможных комбинаций:"+ number_combinations);
  114.  
  115.             if (number_combinations >= this.number_name)
  116.                 return true;
  117.             else
  118.             {
  119.                 Console.WriteLine("Возможных комбинаций меньше чем имен");
  120.                 return false;
  121.             }
  122.                
  123.         }
  124.          
  125.         public void generate_wish()
  126.         {
  127.             Console.WriteLine("Start generate wish...");
  128.             for (int j = 0; j < this.number_name; ++j)
  129.             {
  130.  
  131.                 int number_attempt = 0;
  132.  
  133.                
  134.                 while (true)
  135.                 {
  136.                     number_attempt++;
  137.                     if (number_attempt > 100000)
  138.                     {
  139.                         Console.WriteLine("нельзя сгенрить");
  140.                         return;
  141.                     }
  142.                    
  143.                     string wish = "";
  144.                     Random rnd = new Random();
  145.  
  146.  
  147.                     string group_1 = this.groups[rnd.Next(0, this.number_group)];
  148.                     string group_2 = this.groups[rnd.Next(0, this.number_group)];
  149.                     string group_3 = this.groups[rnd.Next(0, this.number_group)];
  150.  
  151.                     if (group_1 == group_2 || group_1 == group_3 || group_2 == group_3)
  152.                         continue;
  153.  
  154.  
  155.                     var phrase_1 = this.phrases[group_1].Where((x) => x.Item2 == this.phrases[group_1].Min(y => y.Item2)).ToList()[0];
  156.                     var phrase_2 = this.phrases[group_2].Where((x) => x.Item2 == this.phrases[group_2].Min(y => y.Item2)).ToList()[0];
  157.                     var phrase_3 = this.phrases[group_3].Where((x) => x.Item2 == this.phrases[group_3].Min(y => y.Item2)).ToList()[0];
  158.  
  159.                     List<string> sort_phrases = new List<string>();
  160.                     sort_phrases.Add(phrase_1.Item1);
  161.                     sort_phrases.Add(phrase_2.Item1);
  162.                     sort_phrases.Add(phrase_3.Item1);
  163.                     sort_phrases.Sort();
  164.  
  165.                     wish = String.Format("{0}, {1}, {2} !!!", sort_phrases[0], sort_phrases[1], sort_phrases[2]);
  166.                    
  167.                     if (wishes.Contains(wish))
  168.                         continue;
  169.                     else
  170.                     {
  171.                         //Console.WriteLine(wish);
  172.                         wishes.Add(wish);
  173.  
  174.                         var index = this.phrases[group_1].FindIndex(x => x.Item1 == phrase_1.Item1);
  175.                         this.phrases[group_1][index] = Tuple.Create(phrase_1.Item1, phrase_1.Item2 + 1);
  176.  
  177.                         index = this.phrases[group_2].FindIndex(x => x.Item1 == phrase_2.Item1);
  178.                         this.phrases[group_2][index] = Tuple.Create(phrase_2.Item1, phrase_2.Item2 + 1);
  179.  
  180.                         index = this.phrases[group_3].FindIndex(x => x.Item1 == phrase_3.Item1);
  181.                         this.phrases[group_3][index] = Tuple.Create(phrase_3.Item1, phrase_3.Item2 + 1);
  182.  
  183.                         break;
  184.                     }
  185.                 }
  186.             }
  187.  
  188.             Console.WriteLine("Finish generate wish.");
  189.             Console.WriteLine("number wish: " + wishes.Count);
  190.         }
  191.         public  string RandomString(int length=10)
  192.         {
  193.  
  194.             Random random = new Random();
  195.             const string chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
  196.             return new string(Enumerable.Repeat(chars, length)
  197.                 .Select(s => s[random.Next(s.Length)]).ToArray());
  198.         }
  199.  
  200.  
  201.         public void create_word()
  202.         {
  203.             Console.WriteLine("Start create word...");
  204.  
  205.             Word.Application wordApp = new Word.Application();
  206.             Word.Document document = wordApp.Documents.Add(this.template_path);
  207.  
  208.             try
  209.             {
  210.                 List<string> wishes_list = this.wishes.ToList();
  211.  
  212.                 for (int i = 0; i < this.number_name; ++i)
  213.                 {
  214.                    
  215.  
  216.                     document.Bookmarks["name"].Range.Text = this.names[i];
  217.                     document.Bookmarks["wish"].Range.Text = wishes_list[i];
  218.  
  219.                     var endOfFile = document.Bookmarks["\\endofdoc"].Range;
  220.                     endOfFile.InsertBreak(Word.WdBreakType.wdPageBreak);
  221.                     endOfFile.InsertFile(this.template_path);
  222.  
  223.  
  224.                 }
  225.  
  226.                 Word.Range rng = document.Bookmarks["alltext"].Range;
  227.                 rng.Font.Size = 14;
  228.                 rng.Font.Name = "Italic";
  229.  
  230.                 string fileName = RandomString();
  231.                 document.SaveAs2(this.save_doc_dir + fileName);
  232.  
  233.                 document.Close(Word.WdSaveOptions.wdDoNotSaveChanges);
  234.                 wordApp.Quit();
  235.                 document = null;
  236.                 wordApp = null;
  237.  
  238.             }
  239.             catch (Exception e)
  240.             {
  241.                 document.Close(Word.WdSaveOptions.wdDoNotSaveChanges);
  242.                 wordApp.Quit();
  243.                 document = null;
  244.                 wordApp = null;
  245.                 Console.WriteLine(e.Message);
  246.             }
  247.  
  248.             Console.WriteLine("Finish create word.");
  249.         }
  250.  
  251.     }
  252.  
  253.     class Program
  254.     {
  255.         static void Main(string[] args)
  256.         {
  257.  
  258.  
  259.             //
  260.             WishGeneration wish_generation = new WishGeneration();
  261.             wish_generation.read_data();
  262.             if (!wish_generation.check_combinations()) return;
  263.             wish_generation.generate_wish();
  264.             wish_generation.create_word();
  265.             Console.ReadKey();
  266.            
  267.         }
  268.     }
  269. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement