C#实现单位换算器
短信预约 -IT技能 免费直播动态提醒
本文实例为大家分享了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;
}
}
}
}
}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程网。
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341