Advertisement
Guest User

Untitled

a guest
Dec 7th, 2017
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.09 KB | None | 0 0
  1.  
  2.  
  3.         Int32 sum = 0;
  4.         Int32 lenghtBack = 5;
  5.         array<String^> ^tmp;
  6.         array<Int32^> ^freekey;
  7.         array<Boolean^> ^boolea;
  8.         array<Int32^> ^resheto;
  9.         array<String^> ^words;
  10.  
  11.         Int32 mnoget = 0;
  12.         Int32 fullWeight = 0;
  13.  
  14.         void fill_word() {
  15.             //Заполнить двоичную штуку автофилом
  16.             textBox1->Text = "";
  17.  
  18.             array<String^> ^words = gcnew array <String^>(lenghtBack);
  19.             for (int i = 0; i < words->Length; i++)
  20.             {
  21.                 words[i] = Convert::ToString(rand() % 2);
  22.                 textBox1->Text += words[i];
  23.  
  24.  
  25.             }
  26.  
  27.         }
  28.         void calc_word()
  29.         {
  30.             String ^text = textBox1->Text;
  31.  
  32.             words = text->Split(gcnew array<Char> {  ' ', ',', ':', '.' }); //Массив с string
  33.  
  34.             boolea = gcnew array<Boolean^>((words->Length - 1)*lenghtBack); //Массив с двоичным открытым кодом
  35.  
  36.             for (int i = 0; i < words->Length - 1; i++) {
  37.                 for (int j = 0; j < lenghtBack; j++)
  38.                 {
  39.                     if ((words[i][j]) == '0')
  40.                         boolea[i*lenghtBack + j] = false;
  41.                     else if ((words[i][j]) == ' ')
  42.                         continue;
  43.                     //даже зайти не должен, так прост
  44.                     else boolea[i*lenghtBack + j] = true;
  45.                 }
  46.  
  47.             }
  48.         }
  49.  
  50.         void fill_backpack()
  51.         {
  52.  
  53.  
  54.             //textBox2
  55.  
  56.  
  57.             Int32 count = 0;
  58.             srand(time(0));
  59.             Int32 tmptime = 0;
  60.             String^ stringBackPackEasy = "";
  61.  
  62.             for (int i = 0; i < lenghtBack; i++)
  63.             {
  64.                 if (count > 15) {
  65.                     sum = 0;
  66.                     count = 0;
  67.                     tmptime = 0;
  68.                     i = -1;
  69.                     stringBackPackEasy = "";
  70.  
  71.                 }
  72.  
  73.                 else
  74.                 {
  75.                     //Cпросить ПРО ЭТО!!!!
  76.                     tmptime = (rand() % (100)) / (lenghtBack - i);
  77.                     if (sum < tmptime) {
  78.                         sum += tmptime;
  79.                         if (i == lenghtBack - 1)
  80.                             stringBackPackEasy += tmptime;
  81.                         else
  82.                             stringBackPackEasy += tmptime + " ";
  83.  
  84.                     }
  85.                     else {
  86.                         tmptime += (rand() % 100) / (lenghtBack - i);
  87.                         i--;
  88.                     }
  89.                 }
  90.                 count++;
  91.  
  92.             }
  93.  
  94.             textBox2->Text = stringBackPackEasy;
  95.             tmp = stringBackPackEasy->Split(gcnew array<Char> {  ' ', ',', ':', '.' });
  96.  
  97.         }
  98.         void calc_close_key()
  99.         {
  100.  
  101.             tmp = textBox2->Text->Split(gcnew array<Char> {  ' ', ',', ':', '.' });
  102.             lenghtBack = tmp->Length; //Длина рюкзака
  103.             sum = 0;
  104.  
  105.  
  106.             for (int i = 0; i < lenghtBack; i++)
  107.             {
  108.                 try
  109.                 {
  110.                     sum += Convert::ToInt32(tmp[i]);
  111.                 }
  112.                 catch (...)
  113.                 {
  114.                     MessageBox::Show(" Ошибка в введенных данных, введите данные рюкзака");
  115.                 }
  116.             }
  117.         }
  118.         void make_resheto()
  119.         {
  120.             //Решето Эрато??сфера?
  121.             resheto = gcnew array<Int32^>(sum + 1);
  122.             for (int i = 0; i < sum; i++) {
  123.                 resheto[i] = i;
  124.             }
  125.  
  126.             for (int i = 2; i * i <= sum; i++)
  127.             {
  128.                 if (resheto[i])
  129.                     //Если текущее число не равно 0 - начинаем от него искать сложные
  130.                     for (int j = i*i; j <= sum; j += i)
  131.                         //И обнуляем их ячейки, чтобы больше не проверять их в цикле
  132.                         resheto[j] = nullptr;
  133.             }
  134.             resheto->Sort(resheto);
  135.             resheto->Reverse(resheto);
  136.         }
  137.  
  138.  
  139.  
  140.         void make_open_key()
  141.         {
  142.             make_resheto();
  143.             mnoget = 0; //Множитель должен быть взаимно простым числом с модулем, например
  144.  
  145.             if ((textBox5->TextLength == 0) && (textBox5->TextLength == 0))
  146.  
  147.  
  148.             {
  149.                 for (int i = 0; i < resheto->Length / 6; i++)
  150.                 {
  151.                     if (resheto[i] != Convert::ToInt32(0))
  152.                         mnoget = Convert::ToInt32(resheto[i]);
  153.                 }
  154.                 fullWeight = rand() % 100 + sum; //Значение модуля должно быть больше суммы всех чисел последовательности, например
  155.                 textBox5->Text = Convert::ToString(mnoget);
  156.                 textBox6->Text = Convert::ToString(fullWeight);
  157.  
  158.  
  159.             }
  160.  
  161.  
  162.             else
  163.             {
  164.                 mnoget = Convert::ToInt32(textBox5->Text);
  165.                 fullWeight = Convert::ToInt32(textBox6->Text);
  166.             }
  167.             freekey = gcnew array<Int32^>(lenghtBack);
  168.  
  169.             textBox3->Text = "";
  170.  
  171.             for (int i = 0; i < freekey->Length; i++)
  172.             {
  173.                 freekey[i] = (Convert::ToInt32(tmp[i]) * mnoget) % fullWeight;
  174.                 textBox3->Text += freekey[i] + " ";
  175.             }
  176.         }
  177.         void make_code_phrase()
  178.         {
  179.             textBox4->Text = "";
  180.  
  181.             for (int i = 0; i < words->Length - 1; i++)
  182.             {
  183.                 Int32 SumOfCode = 0;
  184.                 for (int j = 0; j < lenghtBack; j++)
  185.                 {
  186.                     {
  187.                         if (boolea[i*lenghtBack + j]->Equals(true))
  188.                         {
  189.                             SumOfCode = SumOfCode + Convert::ToInt32(freekey[(i*lenghtBack + j) % lenghtBack]);
  190.  
  191.                         }
  192.                         else
  193.                             continue;
  194.                     }
  195.                 }
  196.                 textBox4->Text += Convert::ToString(SumOfCode) + " ";
  197.             }
  198.  
  199.  
  200.  
  201.         }
  202.  
  203.         void makebin() {
  204.             array<String^> ^temp = textBox1->Text->Split(gcnew array<Char> {  ' ', ',', ':', '.' });
  205.             String^ tmpforbinfill = "";
  206.             for (int i = 0; i < temp->Length; i++)
  207.             {
  208.                 tmpforbinfill += Convert::ToInt32(temp[i]);
  209.             }
  210.  
  211.             imp->Text = Convert::ToString(Convert::ToInt32(tmpforbinfill, 2));
  212.  
  213.         }
  214.  
  215.  
  216.         void calc_bin()
  217.         {
  218.             textBox1->Text = "";
  219.  
  220.  
  221.             array <String^> ^arrfortmpreadstr = gcnew array <String^>(0);
  222.             arrfortmpreadstr = imp->Text->Split(gcnew array<Char> {  ' ', ',', ':', '.' });
  223.             array <Int32^> ^arrfortmpreadint = gcnew array <Int32^>(arrfortmpreadstr->Length);
  224.             for (int i = 0; i < arrfortmpreadstr->Length; i++)
  225.             {
  226.                 try {
  227.                     arrfortmpreadint[i] = Convert::ToInt32(arrfortmpreadstr[i]);
  228.                     Int32 temp = Convert::ToInt32(arrfortmpreadstr[i]);
  229.                     arrfortmpreadstr[i] = Convert::ToString(temp, 2);
  230.                     Int32 tmp_lenght = arrfortmpreadstr[i]->Length;
  231.  
  232.  
  233.                     while (tmp_lenght < lenghtBack)
  234.                     {
  235.                         //пока текст в двоичной штуке меньше чем длина рюкзака вставляем нули слева
  236.  
  237.                         arrfortmpreadstr[i] = arrfortmpreadstr[i]->Insert(0, "0");
  238.                         tmp_lenght++;
  239.  
  240.                     }
  241.                 }
  242.                 catch (Exception^ ex)
  243.                 {
  244.                     MessageBox::Show(ex->Message);
  245.  
  246.                 }
  247.             }
  248.  
  249.  
  250.             Int32 idx = 0;
  251.  
  252.             while (idx != arrfortmpreadstr->Length)
  253.             {
  254.  
  255.                 textBox1->Text += Convert::ToString(arrfortmpreadstr[idx]) + " ";
  256.                 idx++;
  257.  
  258.             }
  259.  
  260.  
  261.  
  262.  
  263.  
  264.  
  265.  
  266.  
  267.         }
  268.  
  269.  
  270.  
  271.         void fill_box()
  272.         {
  273.             fill_word();
  274.             makebin();
  275.             fill_backpack();
  276.             make_open_key();
  277.  
  278.         }
  279.         void clean_box()
  280.         {
  281.  
  282.             sum = 0;
  283.             lenghtBack = 5;
  284.  
  285.             tmp->Resize(tmp, 0);
  286.  
  287.  
  288.             freekey->Resize(tmp, 0);
  289.  
  290.             boolea->Resize(tmp, 0);
  291.  
  292.  
  293.             resheto->Resize(tmp, 0);
  294.  
  295.             textBox1->Text = "";
  296.             textBox2->Text = "";
  297.             textBox3->Text = "";
  298.             textBox4->Text = "";
  299.             textBox5->Text = "";
  300.             textBox6->Text = "";
  301.             imp->Text = "";
  302.             fullWeight = 0;
  303.             mnoget = 0;
  304.  
  305.         }
  306.  
  307.  
  308.     private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {
  309.         textBox5->Text = "";
  310.         textBox6->Text = "";
  311.         fill_box();
  312.  
  313.     };
  314.     private: System::Void button2_Click(System::Object^  sender, System::EventArgs^  e) {
  315.         clean_box();
  316.         GC::Collect();
  317.     }
  318.  
  319.     private: System::Void button3_Click(System::Object^  sender, System::EventArgs^  e) {
  320.  
  321.  
  322.         calc_bin();
  323.         calc_word();
  324.         calc_close_key();
  325.         make_open_key();
  326.         make_code_phrase();
  327.  
  328.     }
  329.  
  330.  
  331.     };
  332. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement