Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace Фраза
- {
- class Program
- {
- struct Pair // СТруктура буква - вероятность - код
- {
- public char letter;
- public double prob;
- public string code;
- public Pair (char letter, double prob, string code)
- {
- this.letter = letter;
- this.prob = prob;
- this.code = code;
- }
- }
- public class PairProbFirst : Comparer<Pair>
- {
- }
- static void Main(string[] args)
- {
- Console.WriteLine("Введите фразу: ");
- string line = Console.ReadLine();
- line = line.ToLower();
- Dictionary<char, double> letters = new Dictionary<char, double>();
- int count = 0;
- for (int i = 0; i < line.Length; i++)//добавление всех встречающихся символов кроме пробелов в словарь
- {
- char temp = line[i];
- if (temp != ' ')
- {
- if (letters.ContainsKey(temp))
- {
- double value = letters[temp];
- value++;
- letters.Remove(temp);
- letters.Add(temp, value);
- count++;
- }
- else
- {
- letters.Add(temp, 1);
- count++;
- }
- }
- }
- List<char> changed = new List<char>();
- foreach (KeyValuePair<char, double> pair in letters)
- {
- if (changed.Contains(pair.Key)==false)
- {
- double prob = pair.Value / count;
- char letter = pair.Key;
- letters.Remove(letter);
- letters.Add(letter, prob);
- changed.Add(letter);
- }
- }
- List<Pair> pairs = new List<Pair>();
- foreach (KeyValuePair<char, double> pair in letters)
- {
- pairs.Add(new Pair(pair.Key, pair.Value, "0"));
- }
- //foreach (var pair in letters.OrderBy(pair => pair.Value))
- //{
- // Console.WriteLine("{0} - {1}", pair.Key, pair.Value);
- //}
- //Создать лист структур и переопределить компоратор для нее
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement