Explorar el Código

Все способы работают

Imagara hace 3 años
padre
commit
155cb8177c
Se han modificado 2 ficheros con 21 adiciones y 12 borrados
  1. 1 1
      Encrypter/MainWindow.xaml
  2. 20 11
      Encrypter/MainWindow.xaml.cs

+ 1 - 1
Encrypter/MainWindow.xaml

@@ -59,7 +59,7 @@
                         <Button Width="200"
                                 Height="40"
                                 Content="Расшифровать"
-                                Click="sposob1_encrypt"/>
+                                Click="sposob1_decrypt"/>
                     </StackPanel>
                 </Grid>
             </TabItem>

+ 20 - 11
Encrypter/MainWindow.xaml.cs

@@ -38,25 +38,34 @@ namespace Encrypter
             foreach (Byte b in encodedBytes)
                 output += $"{b + Convert.ToInt32(dictionarys.Where(item => item.Key == Convert.ToChar(b)).Select(item => item.Value).FirstOrDefault())}%";
             sposob1_output.Text = output;
+        }
 
+        public int CountOfCoincidences(string a, string[] mass)
+        {
+            int count = 0;
+            for (int i = 0; i < mass.Length; i++)
+                if (mass[i] == a)
+                    count++;
+            return count;
         }
+
         private void sposob1_decrypt(object sender, RoutedEventArgs e)
         {
-            //not working
             string input = sposob1_input2.Text, output = "";
 
-            Dictionary<char, int> dictionarys = input.GroupBy(x => x).ToDictionary(x => x.Key, x => x.Count());
-            int uniqueSymbCount = dictionarys.Count();
-            int pos = 0;
+            string[] mass = input.Split('%');
 
-            while (input.IndexOf('%') != -1)
+            int[,] array = new int[mass.Length, 2];
+
+            for (int i = 0; i < mass.Length; i++)
             {
-                byte temp = Convert.ToByte(Convert.ToInt32(input.Substring(0, input.IndexOf('%'))) - Convert.ToInt32(dictionarys.Where(item => item.Key == Convert.ToChar(b)).Select(item => item.Value).FirstOrDefault()));
-                pos++;
-                output += Convert.ToChar(temp);
-                input = input.Remove(0, input.IndexOf('%') + 1);
+                array[i, 0] = Convert.ToInt32(mass[i]);
+                array[i, 1] = CountOfCoincidences(mass[i], mass);
             }
 
+            for (int i = 0; i < mass.Length; i++)
+                output += Convert.ToChar(Convert.ToByte(array[i, 0] - array[i, 1]));
+
             sposob1_output2.Text = output;
         }
         private void sposob2_encrypt(object sender, RoutedEventArgs e)
@@ -67,7 +76,7 @@ namespace Encrypter
             Byte[] encodedBytes = ascii.GetBytes(input);
             foreach (Byte b in encodedBytes)
             {
-                output += $"{b+pos}%";
+                output += $"{b + pos}%";
                 pos++;
             }
             sposob2_output.Text = output;
@@ -82,7 +91,7 @@ namespace Encrypter
                 byte temp = Convert.ToByte(Convert.ToInt32(input.Substring(0, input.IndexOf('%'))) - pos);
                 pos++;
                 output += Convert.ToChar(temp);
-                input = input.Remove(0, input.IndexOf('%') +1);
+                input = input.Remove(0, input.IndexOf('%') + 1);
             }
             sposob2_output2.Text = output;
         }