diff --git a/UnicodeAndPermutation/Form1.Designer.cs b/UnicodeAndPermutation/Form1.Designer.cs index 1b7d51f..17009ca 100644 --- a/UnicodeAndPermutation/Form1.Designer.cs +++ b/UnicodeAndPermutation/Form1.Designer.cs @@ -28,13 +28,92 @@ /// private void InitializeComponent() { - this.components = new System.ComponentModel.Container(); + this.ListboxWords = new System.Windows.Forms.ListBox(); + this.lbl2 = new System.Windows.Forms.Label(); + this.lbl1 = new System.Windows.Forms.Label(); + this.lblSum = new System.Windows.Forms.Label(); + this.txtWord = new System.Windows.Forms.TextBox(); + this.buttonPorcess = new System.Windows.Forms.Button(); + this.SuspendLayout(); + // + // ListboxWords + // + this.ListboxWords.FormattingEnabled = true; + this.ListboxWords.Location = new System.Drawing.Point(31, 67); + this.ListboxWords.Name = "ListboxWords"; + this.ListboxWords.Size = new System.Drawing.Size(112, 316); + this.ListboxWords.TabIndex = 9; + // + // lbl2 + // + this.lbl2.AutoSize = true; + this.lbl2.Location = new System.Drawing.Point(9, 67); + this.lbl2.Name = "lbl2"; + this.lbl2.Size = new System.Drawing.Size(13, 13); + this.lbl2.TabIndex = 6; + this.lbl2.Text = "2"; + // + // lbl1 + // + this.lbl1.AutoSize = true; + this.lbl1.Location = new System.Drawing.Point(9, 38); + this.lbl1.Name = "lbl1"; + this.lbl1.Size = new System.Drawing.Size(13, 13); + this.lbl1.TabIndex = 7; + this.lbl1.Text = "1"; + // + // lblSum + // + this.lblSum.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle; + this.lblSum.Location = new System.Drawing.Point(31, 38); + this.lblSum.Name = "lblSum"; + this.lblSum.Size = new System.Drawing.Size(112, 20); + this.lblSum.TabIndex = 8; + // + // txtWord + // + this.txtWord.Location = new System.Drawing.Point(31, 12); + this.txtWord.Name = "txtWord"; + this.txtWord.Size = new System.Drawing.Size(112, 20); + this.txtWord.TabIndex = 5; + this.txtWord.KeyDown += new System.Windows.Forms.KeyEventHandler(this.txtWord_KeyDown); + // + // buttonPorcess + // + this.buttonPorcess.Location = new System.Drawing.Point(149, 10); + this.buttonPorcess.Name = "buttonPorcess"; + this.buttonPorcess.Size = new System.Drawing.Size(75, 23); + this.buttonPorcess.TabIndex = 4; + this.buttonPorcess.Text = "Permute"; + this.buttonPorcess.UseVisualStyleBackColor = true; + this.buttonPorcess.Click += new System.EventHandler(this.buttonPorcess_Click); + // + // Form1 + // + this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; - this.ClientSize = new System.Drawing.Size(800, 450); - this.Text = "Form1"; + this.ClientSize = new System.Drawing.Size(237, 398); + this.Controls.Add(this.ListboxWords); + this.Controls.Add(this.lbl2); + this.Controls.Add(this.lbl1); + this.Controls.Add(this.lblSum); + this.Controls.Add(this.txtWord); + this.Controls.Add(this.buttonPorcess); + this.Name = "Form1"; + this.Text = "Unicode and Permutation"; + this.ResumeLayout(false); + this.PerformLayout(); + } #endregion + + private System.Windows.Forms.ListBox ListboxWords; + private System.Windows.Forms.Label lbl2; + private System.Windows.Forms.Label lbl1; + private System.Windows.Forms.Label lblSum; + private System.Windows.Forms.TextBox txtWord; + private System.Windows.Forms.Button buttonPorcess; } } diff --git a/UnicodeAndPermutation/Form1.cs b/UnicodeAndPermutation/Form1.cs index fac8c18..290c9ba 100644 --- a/UnicodeAndPermutation/Form1.cs +++ b/UnicodeAndPermutation/Form1.cs @@ -4,6 +4,7 @@ using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; +using System.Net; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; @@ -12,9 +13,128 @@ namespace UnicodeAndPermutation { public partial class Form1 : Form { + private List Words = new List(); public Form1() { InitializeComponent(); + txtWord.Focus(); } + + #region Show Permuted real words + private async void ShowRealwords() + { + Words.Clear(); + Permute(txtWord.Text.ToCharArray()); + var wordFiltered = new List(); + foreach (var word in Words) + { + var json = await GetWordsFromDic(word); + if (json.Length - word.Length > 100) + { + wordFiltered.Add(word); + } + } + + if (wordFiltered.Any()) + { + ListboxWords.DataSource = wordFiltered; + } + } + #endregion + + #region Sum Unicode Numbers + private void SumUnicodeNumbers(string word) + { + int n = 0; + foreach (var c in word) + { + n += (int)c; + } + + lblSum.Text = n.ToString(); + } + #endregion + + #region Permutation + /// + /// 1. 길이가 할당되지 않은 경우, 단어의 길이를 계산 + /// 2. 새롭게 조합된 string을 collection에 추가 + /// 3. 재귀적으로 Permute 함수를 호출하여 새로운 조합을 찾아냄 + /// + /// + /// + /// + private void Permute(char[] chars, int k = 0, int length = -1) + { + if (length == -1) + { + length = chars.Length; + } + + if (k == length) + { + Words.Add(new string(chars)); + Words = Words.Distinct().ToList(); + return; + } + + for (int i = k; i < length; i++) + { + Swap(ref chars[k], ref chars[i]); + Permute(chars, k + 1, length); + Swap(ref chars[k], ref chars[i]); + } + } + + /// + /// XOR Opertation을 이용하여 두 character를 치환 + /// + /// + /// + private void Swap(ref char a, ref char b) + { + if (a == b) return; + + a ^= b; + b ^= a; + a ^= b; + } + #endregion + + #region Get word information via open api + /// + /// https://glosbe.com/gapi/translate?from=eng&dest=eng&format=json&pretty=false&phrase= ? + /// 비동기적으로 단어의 뜻을 json 파일로 받아서 return + /// + /// + /// Json format + private async Task GetWordsFromDic(string word) + { + using (var client = new WebClient()) + { + var uri = new Uri($"https://glosbe.com/gapi/translate?from=eng&dest=eng&format=json&pretty=false&phrase={word}"); + return await client.DownloadStringTaskAsync(uri); + } + } + #endregion + + #region Events + private void buttonPorcess_Click(object sender, EventArgs e) + { + SumUnicodeNumbers(txtWord.Text); + ShowRealwords(); + } + + private void txtWord_KeyDown(object sender, KeyEventArgs e) + { + switch (e.KeyData) + { + case Keys.Enter: + SumUnicodeNumbers(txtWord.Text); + ShowRealwords(); + break; + } + } + #endregion } } diff --git a/UnicodeAndPermutation/Form1.resx b/UnicodeAndPermutation/Form1.resx new file mode 100644 index 0000000..1af7de1 --- /dev/null +++ b/UnicodeAndPermutation/Form1.resx @@ -0,0 +1,120 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + \ No newline at end of file diff --git a/UnicodeAndPermutation/UnicodeAndPermutation.csproj b/UnicodeAndPermutation/UnicodeAndPermutation.csproj index 81cc0aa..5e27ac1 100644 --- a/UnicodeAndPermutation/UnicodeAndPermutation.csproj +++ b/UnicodeAndPermutation/UnicodeAndPermutation.csproj @@ -53,6 +53,9 @@ + + Form1.cs + ResXFileCodeGenerator Resources.Designer.cs