C#实现单位换算器
程序员文章站
2022-03-01 13:21:44
本文实例为大家分享了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; } } } } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
上一篇: ASP.NET Core 3.0轻量级角色API控制授权库
下一篇: C#实现温度转换功能