private void Decode(object sender, RoutedEventArgs e) { Interval interval = new Interval(0, 1); decimal message = decimal.Parse(messageToDecodeTextBox.Text); string outMessage = ""; TextBox[] probablities = new TextBox[itemsNumber]; char[] letters = new char[itemsNumber]; for (int i = 0; i < itemsNumber; i++) { probablities[i] = alphabet[i, 1]; } decimal[] probs = parseProbabilities(probablities); probs = ArithmeticCoding.MakeProportions(probs); for (int i = 0; i < probablities.Length; i++) { Interval[] intervals = ArithmeticCoding.MakeSubintervals(interval, probs); for (int j = 0; j < intervals.Length; j++) { if ((message > intervals[j].l) && (message < intervals[j].r)) { outMessage += alphabet[j, 0].Text.ToString(); if (((intervals[j].l + intervals[j].r) / 2) == message) { i = intervals.Length; } else { interval = new Interval(intervals[j].l, intervals[j].r); } } if (((intervals[j].l + intervals[j].r) / 2) == message) { j = intervals.Length; } } } resultDecodingTextBlock.Text = outMessage; resultDeCodingGrid.Visibility = Visibility.Visible; resultDeCodingGrid.Opacity = 0; resultDeCodingGrid.OpacityTransition = new ScalarTransition() { Duration = new TimeSpan(0, 0, 0, 0, 500) }; resultDeCodingGrid.Opacity = 1; }
private async void Calculate(object sender, RoutedEventArgs e) { try { char[] message = messageTextBox.Text.ToCharArray(); Interval interval = new Interval(); TextBox[] probablities = new TextBox[itemsNumber]; char[] letters = new char[itemsNumber]; for (int i = 0; i < itemsNumber; i++) { probablities[i] = alphabet[i, 1]; var characters = alphabet[i, 0].Text.ToCharArray(); letters[i] = characters[0]; } bool isMessageInAlphabet = false; for (int i = 0; i < message.Length; i++) { isMessageInAlphabet = checkLetterInAlphabet(message[i], letters); if (!isMessageInAlphabet) { break; } } if (!isMessageInAlphabet) { ContentDialog dialog = new ContentDialog { Title = "Error", Content = "Wiadomość niepoprawna", CloseButtonText = "ok" }; ContentDialogResult result = await dialog.ShowAsync(); return; } try { subIntervalsGrid.Children.Clear(); checkProbability(probablities); decimal[] probs = parseProbabilities(probablities); probs = ArithmeticCoding.MakeProportions(probs); for (int p = 0; p < message.GetLength(0); p++) { Interval[] intervals = ArithmeticCoding.MakeSubintervals(interval, probs); addSubinterval(intervals, message[p], letters); interval = ArithmeticCoding.SetCurrentInterval(intervals, letters, message[p]); } resultCodingGrid.Visibility = Visibility.Visible; resultCodingGrid.Opacity = 0; resultCodingGrid.OpacityTransition = new ScalarTransition() { Duration = new TimeSpan(0, 0, 0, 0, 500) }; resultCodingGrid.Opacity = 1; resultCodingTextBlock.Text = "Wynik: " + ((interval.l + interval.r) / 2).ToString(); subIntervalsGrid.Visibility = Visibility.Visible; subIntervalsGrid.Opacity = 0; subIntervalsGrid.OpacityTransition = new ScalarTransition() { Duration = new TimeSpan(0, 0, 0, 0, 500) }; subIntervalsGrid.Opacity = 1; } catch (Exception ex) { ContentDialog dialog = new ContentDialog { Title = "Error", Content = ex.Message, CloseButtonText = "ok" }; ContentDialogResult result = await dialog.ShowAsync(); } } catch (Exception ex) { ContentDialog dialog = new ContentDialog { Title = "Error", Content = ex.Message, CloseButtonText = "ok" }; ContentDialogResult result = await dialog.ShowAsync(); } }