C# 单位换算器
Posted Link2Points
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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计算机的单位换算,KB、MB是怎样换算的?