1- using System . Collections . Generic ;
1+ using System ;
2+ using System . Collections . Generic ;
3+ using System . Linq ;
24using System . Windows ;
35using System . Windows . Controls ;
46using AlgorithmsLibrary ;
@@ -96,47 +98,61 @@ private void LinearCodesType52ClicButton(object sender, RoutedEventArgs e)
9698
9799 private void EncodeClicButton ( object sender , RoutedEventArgs e )
98100 {
101+ string str = "" ;
99102 if ( TextForEncoding . Text == "" )
100103 {
101104 //MessageWindow
102105 }
103106 switch ( IndexOfCurrentAlgorithm )
104107 {
105108 case 0 :
109+ TextAfterDecoding . Text = string . Empty ;
110+
106111 var Huf = HuffmanAlgm . Encode ( TextForEncoding . Text ) ;
107112 EncodedText . Text = Huf . GetAnswer ( ) ;
108113 foreach ( var i in Huf . GetData ( ) )
109- FriqDictionary . Text += i . Key . ToString ( ) + " " + i . Value . ToString ( ) + '\n ' ;
110- CompressionRatio . Text = HuffmanAlgm . CalculateCompressionRatio ( TextForEncoding . Text , EncodedText . Text ) . ToString ( ) ;
114+ str += i . Key . ToString ( ) + " " + i . Value . ToString ( ) + '\n ' ;
115+ FriqDictionary . Text = str ;
116+ CompressionRatio . Text = Huf . GetCompressionRatio ( ) . ToString ( ) ;
111117 break ;
112118 case 1 :
119+ TextAfterDecoding . Text = string . Empty ;
120+
113121 var Sha = ShannonFanoAlgm . Encode ( TextForEncoding . Text ) ;
114122 EncodedText . Text = Sha . GetAnswer ( ) ;
115123 foreach ( var i in Sha . GetData ( ) )
116- FriqDictionary . Text += i . Key . ToString ( ) + " " + i . Value . ToString ( ) + '\n ' ;
117- CompressionRatio . Text = ShannonFanoAlgm . CalculateCompressionRatio ( TextForEncoding . Text , EncodedText . Text ) . ToString ( ) ;
124+ str += i . Key . ToString ( ) + " " + i . Value . ToString ( ) + '\n ' ;
125+ FriqDictionary . Text = str ;
126+ CompressionRatio . Text = Sha . GetCompressionRatio ( ) . ToString ( ) ;
118127 break ;
119128 case 2 :
129+ TextAfterDecoding . Text = string . Empty ;
130+
120131 var Ari = ArithmeticCodingAlgm . Encode ( TextForEncoding . Text ) ;
121132 EncodedText . Text = Ari . GetAnswer ( ) ;
122133 foreach ( var i in Ari . GetData ( ) . GetData ( ) )
123- FriqDictionary . Text += i . Key . ToString ( ) + " " + i . Value . ToString ( ) + '\n ' ;
124- CompressionRatio . Text = ArithmeticCodingAlgm . CalculateCompressionRatio ( TextForEncoding . Text , EncodedText . Text ) . ToString ( ) ;
134+ str += i . Key . ToString ( ) + " " + i . Value . ToString ( ) + '\n ' ;
135+ FriqDictionary . Text = str ;
136+ CompressionRatio . Text = Ari . GetCompressionRatio ( ) . ToString ( ) ;
125137 break ;
126138 case 3 :
127139 var Rle = RLEAlgm . Encode ( Text1ForEncoding . Text ) ;
128140 foreach ( var i in Rle . GetAnswer ( ) )
129- Encoded1Text . Text += i . ToString ( ) ;
130- CompressionRatio . Text = RLEAlgm . CalculateCompressionRatio ( Text1ForEncoding . Text , Encoded1Text . Text ) . ToString ( ) ;
141+ str += i . ToString ( ) ;
142+ Encoded1Text . Text = str ;
143+ // отловить ошибку
144+ CompressionRatio1 . Text = Rle . GetCompressionRatio ( ) . ToString ( ) ;
131145 break ;
132146 case 4 :
133147 var Lz = LZ77Algm . Encode ( Text1ForEncoding . Text ) ;
134148 foreach ( var i in Lz . GetAnswer ( ) )
135- Encoded1Text . Text += i . ToString ( ) ;
136- CompressionRatio . Text = LZ77Algm . CalculateCompressionRatio ( Text1ForEncoding . Text , Encoded1Text . Text ) . ToString ( ) ;
149+ str += i . ToString ( ) ;
150+ Encoded1Text . Text = str ;
151+ // отловить ошибку
152+ CompressionRatio1 . Text = Lz . GetCompressionRatio ( ) . ToString ( ) ;
137153 break ;
138154 case 5 :
139- var Ham = HammingAlgm . Encode ( Text1ForEncoding . Text ) ;
155+ var Ham = HammingAlgm . Encode ( Text2ForEncoding . Text ) ;
140156 Encoded2Text . Text = Ham . GetAnswer ( ) ;
141157 break ;
142158 case 6 :
@@ -149,11 +165,60 @@ private void DecodeClicButton(object sender, RoutedEventArgs e)
149165
150166 // в зависимости от алгоритма запустить декодировку
151167 // проверка на пустоту полей для исходого текста
168+ if ( TextForEncoding . Text == "" )
169+ {
170+ //MessageWindow
171+ }
172+ switch ( IndexOfCurrentAlgorithm )
173+ {
174+ case 0 :
175+ var Huf = HuffmanAlgm . Decode ( CreateDictionary ( FriqDictionary . Text ) , EncodedText . Text ) ;
176+ TextAfterDecoding . Text = Huf . GetAnswer ( ) ;
177+ break ;
178+ case 1 :
179+ var Sha = ShannonFanoAlgm . Decode ( CreateDictionary ( FriqDictionary . Text ) , EncodedText . Text ) ;
180+ TextAfterDecoding . Text = Sha . GetAnswer ( ) ;
181+ break ;
182+ case 2 :
183+ var Ari = ArithmeticCodingAlgm . Decode ( CreateDictionary ( FriqDictionary . Text ) . ToDictionary ( x => x . Key , x=> int . Parse ( x . Value ) ) ,
184+ EncodedText . Text , TextForEncoding . Text . Length ) ;
185+ TextAfterDecoding . Text = Ari . GetAnswer ( ) ;
186+ break ;
187+ case 3 :
188+ //var Rle = RLEAlgm.Decode(Encoded1Text.Text);
189+ //Text1AfterDecoding.Text = Rle.GetAnswer();
190+ Text1AfterDecoding . Text = "Пока не готово" ;
191+ // отловить ошибку
192+ break ;
193+ case 4 :
194+ //var Lz = LZ77Algm.Decode(Encoded1Text.Text);
195+ //Text1AfterDecoding.Text = Lz.GetAnswer();
196+ Text1AfterDecoding . Text = "Пока не готово" ;
197+ // отловить ошибку
198+ break ;
199+ case 5 :
200+ // пока не готово
201+ var Ham = HammingAlgm . Decode ( Encoded2Text . Text ) ;
202+ Text2AfterDecoding . Text = Ham . GetAnswer ( ) ;
203+ break ;
204+ case 6 :
205+ break ;
206+ }
152207 }
153208
154209 private void EncodeFromFileClicButton ( object sender , RoutedEventArgs e )
155210 {
156211 // диалоговое окно
212+
213+ Microsoft . Win32 . OpenFileDialog dlg = new Microsoft . Win32 . OpenFileDialog ( ) ;
214+ dlg . DefaultExt = ".txt" ;
215+ dlg . Filter = "Files|*.txt" ;
216+ Nullable < bool > result = dlg . ShowDialog ( ) ;
217+ if ( result == true )
218+ {
219+ string filename = dlg . FileName ;
220+
221+ }
157222 }
158223
159224 private void DecodeFromFileClicButton ( object sender , RoutedEventArgs e )
@@ -164,6 +229,12 @@ private void DecodeFromFileClicButton(object sender, RoutedEventArgs e)
164229 private void ClearAllClicButton ( object sender , RoutedEventArgs e )
165230 {
166231 //в зависимости от алгоритма очистить поля
232+ if ( new List < int > ( ) { 0 , 1 , 2 } . Contains ( IndexOfCurrentAlgorithm ) )
233+ ClearHuf_Fano_Arith_Border ( ) ;
234+ if ( new List < int > ( ) { 3 , 4 } . Contains ( IndexOfCurrentAlgorithm ) )
235+ ClearRLE_LZ77_Border ( ) ;
236+ if ( new List < int > ( ) { 5 } . Contains ( IndexOfCurrentAlgorithm ) )
237+ ClearHam_Border ( ) ;
167238 }
168239
169240 private void EnterDown_Dictionary ( object sender , System . Windows . Input . KeyEventArgs e )
@@ -197,5 +268,16 @@ void ClearHam_Border()
197268 Text2AfterDecoding . Text = string . Empty ;
198269
199270 }
271+ Dictionary < char , string > CreateDictionary ( string str )
272+ {
273+ Dictionary < char , string > dic = new Dictionary < char , string > ( ) ;
274+ string [ ] item = str . Split ( new char [ ] { '\n ' } , StringSplitOptions . RemoveEmptyEntries ) ;
275+ foreach ( var it in item )
276+ {
277+ string [ ] s = it . Split ( ) ;
278+ dic . Add ( it [ 0 ] , s [ s . Length - 1 ] ) ;
279+ }
280+ return dic ;
281+ }
200282 }
201283}
0 commit comments