Initial commit

This commit is contained in:
2026-08-22 17:55:19 +03:00 Unverified
commit accadb4462
8 changed files with 736 additions and 0 deletions
+240
View File
@@ -0,0 +1,240 @@
using System.Data;
using System.Drawing.Drawing2D;
using System.IO;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Microsoft.Win32;
namespace DSPS_Lab1;
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
private char[] File;
private int Total;
private int[] Count;
private int[,] CountPair; // first index - current char, second index - next char
private float[] Probability;
private float[,] CombinedProbability;
private float[,] ConditionalProbability;
public MainWindow()
{
InitializeComponent();
Count = new int[256];
CountPair = new int[256, 256];
Probability = new float[256];
CombinedProbability = new float[256, 256];
ConditionalProbability = new float[256, 256];
}
private async void OpenFile(object sender, RoutedEventArgs e)
{
var dlg = new OpenFileDialog
{
Title = "Open file",
Filter = "All files|*.*"
};
bool? result = dlg.ShowDialog();
if (result == true && !string.IsNullOrEmpty(dlg.FileName))
{
try
{
string content = await System.IO.File.ReadAllTextAsync(dlg.FileName, Encoding.UTF8);
this.FileName.Content = System.IO.Path.GetFileName(dlg.FileName);
SourceTextBox.Text = content;
this.File = content.ToCharArray();
}
catch (IOException ex)
{
MessageBox.Show(this, $"Failed to read file: {ex.Message}", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
}
await CalculateTask1();
await CalculateTask2();
await CalculateTask3();
}
}
private async Task CalculateTask1()
{
var result = await Task.Run(() =>
{
float H = 0;
float check = 0;
DataTable dt = new DataTable();
foreach (char c in File)
{
if (c > 255) continue;
Count[c]++;
Total++;
}
DataRow row = dt.NewRow();
for (int i = 0; i < Count.Length; i++)
{
dt.Columns.Add(i.ToString(), typeof(float));
float p = (float)Count[i] / Total;
Probability[i] = p;
check += p;
row[i] = Math.Round(p * 100) / 100;
if (Count[i] == 0) continue;
H += p * (float)Math.Log2(p);
}
dt.Rows.Add(row);
return (dt, H);
});
DataGridTask1.ItemsSource = result.dt.DefaultView;
Entropy1AmountLabel.Content = (-result.H).ToString();
double max = (1 / 256.0) * Math.Log2(1 / 256.0) * -256;
MaxEntropyAmountLabel.Content = max.ToString();
Coefficient1.Content = ((max + result.H)/max).ToString();
}
private async Task CalculateTask2()
{
var result = await Task.Run(() =>
{
Total = 0;
float H = 0;
float check = 0;
DataTable dt = new DataTable();
for (int i = 0; i < File.Length - 1; i++)
{
if (File[i + 1] > 255 || File[i] > 255) continue;
CountPair[File[i], File[i + 1]]++;
Total++;
}
dt.Columns.Add("Char", typeof(string));
for (int i = 0; i < CountPair.GetLength(0); i++)
dt.Columns.Add(i.ToString(), typeof(float));
for (int i = 0; i < CountPair.GetLength(0); i++)
{
DataRow row = dt.NewRow();
row[0] = i.ToString();
for (int j = 0; j < CountPair.GetLength(1); j++)
{
float p = (float)CountPair[i, j] / Total;
CombinedProbability[i, j] = p;
check += p;
row[j+1] = Math.Round(p * 1000) / 1000;
if (CountPair[i, j] == 0) continue;
H += p * (float)Math.Log2(p);
}
dt.Rows.Add(row);
}
return (dt, H);
});
DataGridTask2.ItemsSource = result.dt.DefaultView;
CombinedEntropyLabel.Content = (-result.H).ToString();
double max = (1 / 256.0) * Math.Log2(1 / 256.0) * -256;
MaxEntropy2AmountLabel.Content = max.ToString();
Coefficient2.Content = ((max + result.H) / max).ToString();
}
private async Task CalculateTask3()
{
var result = await Task.Run(() =>
{
Total = 0;
float[] Hm = new float[256];
float H = 0;
float[] check = new float[255];
DataTable dt = new DataTable();
for (int i = 0; i < File.Length - 1; i++)
{
if (File[i + 1] > 255 || File[i] > 255) continue;
CountPair[File[i], File[i + 1]]++;
Total++;
}
dt.Columns.Add("Char", typeof(string));
for (int i = 0; i < CountPair.GetLength(0); i++)
dt.Columns.Add(i.ToString(), typeof(float));
for (int i = 0; i < CountPair.GetLength(0); i++)
{
Total = Enumerable.Range(0, CountPair.GetLength(1)).Select(col => CountPair[i, col]).Sum();
if (Total == 0) continue;
DataRow row = dt.NewRow();
row[0] = i.ToString();
for (int j = 0; j < CountPair.GetLength(1); j++)
{
float p = (float)CountPair[i, j] / Total;
ConditionalProbability[i, j] = p;
check[i] += p;
row[j + 1] = Math.Round(p * 1000) / 1000;
if (CountPair[i, j] == 0) continue;
Hm[i] += p * (float)Math.Log2(p);
}
dt.Rows.Add(row);
H += Hm[i] * Probability[i];
}
return (dt, H);
});
DataGridTask3.ItemsSource = result.dt.DefaultView;
ConditionalEntropyLabel.Content = (-result.H).ToString();
double max = (1 / 256.0) * Math.Log2(1 / 256.0) * -256;
MaxEntropy3AmountLabel.Content = max.ToString();
Coefficient3.Content = ((max + result.H) / max).ToString();
}
private void DataGridTask1_SelectedCellsChanged(object sender, SelectedCellsChangedEventArgs e)
{
if (e.AddedCells.Count > 0)
{
var cellInfo = e.AddedCells[0];
if (cellInfo.Item is System.Data.DataRowView rowView)
{
int colIndex = cellInfo.Column.DisplayIndex - 1;
Task1Helper.Content = ((Char)colIndex).ToString() + " " + Probability[colIndex + 1].ToString();
}
}
}
private void DataGridTask2_SelectedCellsChanged(object sender, SelectedCellsChangedEventArgs e)
{
if (e.AddedCells.Count > 0)
{
var cellInfo = e.AddedCells[0];
if (cellInfo.Item is System.Data.DataRowView rowView)
{
int rowIndex = rowView.DataView.Table.Rows.IndexOf(rowView.Row);
int colIndex = cellInfo.Column.DisplayIndex - 1;
Task2Helper.Content = ((Char)rowIndex).ToString() + ((Char)colIndex).ToString() + " " + CombinedProbability[rowIndex, colIndex].ToString();
}
}
}
private void DataGridTask3_SelectedCellsChanged(object sender, SelectedCellsChangedEventArgs e)
{
if (e.AddedCells.Count > 0)
{
var cellInfo = e.AddedCells[0];
if (cellInfo.Item is System.Data.DataRowView rowView)
{
int rowIndex = rowView.DataView.Table.Rows.IndexOf(rowView.Row);
int colIndex = cellInfo.Column.DisplayIndex - 1;
Task3Helper.Content = ((Char)rowIndex).ToString() + ((Char)colIndex).ToString() + " " + ConditionalProbability[rowIndex, colIndex].ToString();
}
}
}
}