C#實現單位換算器
本文實例為大傢分享瞭C#實現單位換算器的具體代碼,供大傢參考,具體內容如下
一、闡述
進制間轉換:十六進制、十進制、八進制、二進制。
長度間轉換:毫米、厘米、米、公裡、英寸、英尺、碼。
面積間轉換:平方毫米、平方厘米、平方米、平方公裡、平方英寸、平方英尺、平方碼。
體積間轉換:立方毫米、毫升、升、立方米、立方英寸、立方英尺、立方碼。
二、效果
1. 進制轉換
通過輸入即可轉換為另一個進制值。
2. 長度轉換
用戶輸入進行單位換算,可從目標中選擇需求單位。
選中需求後,輸入框解除隻讀,輸入即可進行換算。
3. 面積轉換
同樣,使用正則匹配對正數小數和正整數,而進制轉換另外規則匹配。
4. 體積轉換
當重選單位類型時會清空輸入框。
代碼
using System; using System.Text.RegularExpressions; using System.Windows.Forms; namespace UnitConversion { public partial class Form1 : Form { // 默認選項 private string unitItem = "進制"; // 用戶選擇單位度量 的 轉換基數 private double customConversionBase = 0; public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { unitComboBox.Items.Add("進制"); unitComboBox.Items.Add("長度"); unitComboBox.Items.Add("面積"); unitComboBox.Items.Add("體積"); } /* * 進制轉換 */ private void BaseConversion(TextBox text, string val) { if (text == textBox1 && Regex.IsMatch(val, @"[0-1]+$")) //二進制轉換 { long oct = Convert.ToInt64(val, 2); textBox2.Text = Convert.ToString(oct, 8); textBox3.Text = oct.ToString(); textBox4.Text = Convert.ToString(oct, 16); } else if (text == textBox2 && Regex.IsMatch(val, @"[0-7]+$")) //八進制轉換 { long oct = Convert.ToInt64(val, 8); textBox1.Text = Convert.ToString(oct, 2); textBox3.Text = oct.ToString(); textBox4.Text = Convert.ToString(oct, 16); } else if (text == textBox3 && Regex.IsMatch(val, @"[0-9]+$")) //十進制轉換 { long oct = Convert.ToInt64(val); textBox1.Text = Convert.ToString(oct, 2); textBox2.Text = Convert.ToString(oct, 8); textBox4.Text = Convert.ToString(oct, 16); } else if (text == textBox4 && Regex.IsMatch(val, @"[A-Fa-f0-9]+$")) //十六機制轉換 { long oct = Convert.ToInt64(val, 16); textBox1.Text = Convert.ToString(oct, 2); textBox2.Text = Convert.ToString(oct, 8); textBox3.Text = oct.ToString(); } } /* * 長度 */ private void lengthConversion(TextBox text, string val) { if (text == textBox1) { double mm = Convert.ToDouble(val); textBox2.Text = Convert.ToString(mm * 0.1); textBox3.Text = Convert.ToString(mm * 0.001); textBox4.Text = Convert.ToString(mm * 0.000_001); textBox5.Text = Convert.ToString(mm * customConversionBase); } else if (text == textBox2) { double cm = Convert.ToDouble(val); textBox1.Text = Convert.ToString(cm * 10); textBox3.Text = Convert.ToString(cm * 0.01); textBox4.Text = Convert.ToString(cm * 0.000_01); textBox5.Text = Convert.ToString(cm * customConversionBase * 10); } else if (text == textBox3) { double m = Convert.ToDouble(val); textBox1.Text = Convert.ToString(m * 1000); textBox2.Text = Convert.ToString(m * 100); textBox4.Text = Convert.ToString(m * 0.001); textBox5.Text = Convert.ToString(m * customConversionBase * 1000); } else if (text == textBox4) { double km = Convert.ToDouble(val); textBox1.Text = Convert.ToString(km * 1000_000); textBox2.Text = Convert.ToString(km * 100_000); textBox3.Text = Convert.ToString(km * 1000); textBox5.Text = Convert.ToString(km * customConversionBase * 1000_000); } else if (text == textBox5) { double en = Convert.ToDouble(val); textBox1.Text = (en / customConversionBase).ToString(); } } /* * 面積 */ private void areaConversion(TextBox text, string val) { if (text == textBox1) { double mm = Convert.ToDouble(val); textBox2.Text = Convert.ToString(mm * 0.01); textBox3.Text = Convert.ToString(mm * 0.000_001); textBox4.Text = Convert.ToString(mm * 0.000_000_000_001); textBox5.Text = Convert.ToString(mm * customConversionBase); } else if (text == textBox2) { double cm = Convert.ToDouble(val); textBox1.Text = Convert.ToString(cm * 100); textBox3.Text = Convert.ToString(cm * 0.000_1); textBox4.Text = Convert.ToString(cm * 0.000_000_000_1); textBox5.Text = Convert.ToString(cm * customConversionBase * 100); } else if (text == textBox3) { double m = Convert.ToDouble(val); textBox1.Text = Convert.ToString(m * 1_000_000); textBox2.Text = Convert.ToString(m * 10_000); textBox4.Text = Convert.ToString(m * 0.000_001); textBox5.Text = Convert.ToString(m * customConversionBase * 1_000_000); } else if (text == textBox4) { double km = Convert.ToDouble(val); textBox1.Text = Convert.ToString(km * 1000_000_000_000); textBox2.Text = Convert.ToString(km * 10_000_000_000); textBox3.Text = Convert.ToString(km * 1000_000); textBox5.Text = Convert.ToString(km * customConversionBase * 1000_000_000_000); } else if (text == textBox5) { double en = Convert.ToDouble(val); textBox1.Text = Convert.ToString(en / customConversionBase); } } /* * 體積 */ private void volumeConversion(TextBox text, string val) { if (text == textBox1) { double cm = Convert.ToDouble(val); textBox2.Text = Convert.ToString(cm); textBox3.Text = Convert.ToString(cm * 0.001); textBox4.Text = Convert.ToString(cm * 0.000_001); textBox5.Text = Convert.ToString(cm * customConversionBase); } else if (text == textBox2) { double mL = Convert.ToDouble(val); textBox1.Text = Convert.ToString(mL); textBox3.Text = Convert.ToString(mL * 0.001); textBox4.Text = Convert.ToString(mL * 0.000_001); textBox5.Text = Convert.ToString(mL * customConversionBase); } else if (text == textBox3) { double L = Convert.ToDouble(val); textBox1.Text = Convert.ToString(L * 1000); textBox2.Text = Convert.ToString(L * 1000); textBox4.Text = Convert.ToString(L * 0.001); textBox5.Text = Convert.ToString(L * customConversionBase * 1000); } else if (text == textBox4) { double m = Convert.ToDouble(val); textBox1.Text = Convert.ToString(m * 1000_000); textBox2.Text = Convert.ToString(m * 1000_000); textBox3.Text = Convert.ToString(m * 1000); textBox5.Text = Convert.ToString(m * customConversionBase * 1000_000); } else if (text == textBox5) { double en = Convert.ToDouble(val); textBox1.Text = Convert.ToString(en / customConversionBase); } } /* * 輸入換算 */ private void textBox_TextChanged(object sender, EventArgs e) { try { TextBox text = (TextBox) sender; string val = text.Text; // 不為空 及 正整數或正小數 (進制另外進一步匹配) if (text.Text == "") { return; } if (unitItem == "進制") { BaseConversion(text, val); } else if (Regex.IsMatch(val, @"(^[0-9]{1,}$)|(^[0-9]+(.[0-9]{1,})$)")) { switch (unitItem) { case "長度": lengthConversion(text, val); break; case "面積": areaConversion(text, val); break; case "體積": volumeConversion(text, val); break; } } } catch (Exception exception) {} } /* * 單位類型選擇 */ private void unitComboBox_SelectedIndexChanged(object sender, EventArgs e) { // 清空 及 復位 textBox1.Text = ""; textBox2.Text = ""; textBox3.Text = ""; textBox4.Text = ""; textBox5.Text = ""; textBox5.ReadOnly = true; label5.Text = ""; customConversionBase = 0; unitListBox.Items.Clear(); unitItem = unitComboBox.SelectedItem.ToString(); switch (unitItem) { case "進制": label1.Text = "Bin"; label2.Text = "Oct"; label3.Text = "Dec"; label4.Text = "Hex"; break; case "長度": label1.Text = "mm"; label2.Text = "cm"; label3.Text = "m"; label4.Text = "km"; unitListBox.Items.Add("inch"); unitListBox.Items.Add("foot"); unitListBox.Items.Add("yard"); break; case "面積": label1.Text = "m²"; label2.Text = "cm²"; label3.Text = "m²"; label4.Text = "km²"; unitListBox.Items.Add("inch²"); unitListBox.Items.Add("foot²"); unitListBox.Items.Add("yard²"); break; case "體積": label1.Text = "cm³"; label2.Text = "mL"; label3.Text = "L"; label4.Text = "m³"; unitListBox.Items.Add("inch³"); unitListBox.Items.Add("foot³"); unitListBox.Items.Add("yard³"); break; } } /* * 單位度量選擇 */ private void unitListBox_SelectedIndexChanged_1(object sender, EventArgs e) { textBox5.ReadOnly = false; if (unitListBox.SelectedItem != null) { label5.Text = unitListBox.SelectedItem.ToString(); switch (label5.Text) { case "inch": customConversionBase = 0.039_37; break; case "foot": customConversionBase = 0.003_281; break; case "yard": customConversionBase = 0.001_094; break; case "inch²": customConversionBase = 0.001_55; break; case "foot²": customConversionBase = 0.000_011; break; case "yard²": customConversionBase = 0.000_001; break; case "inch³": customConversionBase = 0.061_024; break; case "foot³": customConversionBase = 0.000_035; break; case "yard³": customConversionBase = 0.000_001; break; } } } } }
以上就是本文的全部內容,希望對大傢的學習有所幫助,也希望大傢多多支持WalkonNet。