本文实例为大家分享了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 customconversionba = 0; public form1() { initializecomponent(); } private void form1_load(object nder, eventargs e) { unitcombobox.items.add("进制"); unitcombobox.items.add("长度"); unitcombobox.items.add("面积"); unitcombobox.items.add("体积"); } /* * 进制转换 */ private void baconversion(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); } el 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); } el 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); } el 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 * customconversionba); } el 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 * customconversionba * 10); } el 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 * customconversionba * 1000); } el if (text == textbox4) { double km = convert.todouble(v基督教讲道讲章al); textbox1.text = convert.tostring(km * 1000_000); textbox2.text = convert.tostring(km * 100_000); textbox3.t白令海峡有多宽ext = convert.tostring(km * 1000); textbox5.text = convert.tostring(km * customconversionba * 1000_000); } el if (text == textbox5) { double en = convert.todouble(val); textbox1.text = (en / customconversionba).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 * customconversionba); } el 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 * customconversionba * 100); } el 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 * customconversionba * 1_000_000); } el 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 * customconversionba * 1000_000_000_000); } el if (text == textbox5) { double en = convert.todouble(val); textbox1.text = convert.tostring(en / customconversionba); } } /* * 体积 */ 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 * customconversionba); } el 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 * customconversionba); } el 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 * cust猜谜omconversionba * 1000); } el 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 * customconversionba * 1000_000); } el if (text == textbox5) { double en = convert.todouble(val); textbox1.text = convert.tostring(en / customconversionba); } } /* * 输入换算 */ private void textbox_textchanged(object nder, eventargs e) { try { textbox text = (textbox) nder; string val = text.text; // 不为空 及 正整数或正小数 (进制另外进一步匹配) if (text.text == "") { return; } if (unititem == "进制") { baconversion(text, val); } el if (regex.ismatch(val, @"(^[0-9]{1,}$)|(^[0-9]+(.[0-9]{1,})$)")) { switch (unititem) { ca "长度": lengthconversion(text, val); break; ca "面积": areaconversion(text, val); break; ca "体积": volumeconversion(text, val); break; } } } catch (exception exception) {} } /* * 单位类型选择 */ private void unitcombobox_lectedindexchanged(object nder, eventargs e) { // 清空 及 复位 textbox1.text = ""; textbox2.text = ""; textbox3.text = ""; textbox4.text = ""; textbox5.text = ""; textbox5.readonly = true; label5.text = ""; customconversionba = 0; unitlistbox.items.clear(); unititem = unitcombobox.lecteditem.tostring(); switch (unititem) { ca "进制": label1.text = "bin"; label2.text = "oct"; label3.text = "dec"; label4.text = "hex"; break; ca "长度": 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; ca "面积": 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; ca "体积": label1.text = "cm³"; label2.text = "ml"; label3.text = "l"; label4.text = "m³"; unitlistbox.items.add("inch³"); unitlistbox.items.add("foo长春大学排名t³"); unitlistbox.items.add("yard³"); break; } } /* * 单位度量选择 */ private void unitlistbox_lectedindexchanged_1(object nder, eventargs e) { textbox5.readonly = fal; if (unitlistbox.lecteditem != null) { label5.text = unitlistbox.lecteditem.tostring(); switch (label5.text) { ca "inch": customconversionba = 0.039_37; break; ca "foot": customconversionba = 0.003_281; break; ca "yard": customconversionba = 0.001_094; break; ca "inch²": customconversionba = 0.001_55; break; ca "foot²": customconversionba = 0.000_011; break; ca "yard²": customconversionba = 0.000_001; break; ca "inch³": customconversionba = 0.061_024; break; ca "foot³": customconversionba = 0.000_035; break; ca "yard³": customconversionba = 0.000_001; break; } } } }}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持www.887551.com。
本文发布于:2023-04-04 16:19:32,感谢您对本站的认可!
本文链接:https://www.wtabcd.cn/fanwen/zuowen/084f8fef1cdb62ae910b3ecfa74a0600.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文word下载地址:C#实现单位换算器.doc
本文 PDF 下载地址:C#实现单位换算器.pdf
留言与评论(共有 0 条评论) |