C#实现单位换算器
作者:Link2Points 时间:2022-04-20 07:25:55
本文实例为大家分享了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;
}
}
}
}
}
来源:https://blog.csdn.net/Link2Points/article/details/118691379
标签:C#,单位,换算器
0
投稿
猜你喜欢
Java HtmlEmail 邮件发送的简单实现代码
2023-04-14 21:29:25
深入理解Java设计模式之简单工厂模式
2023-12-06 07:16:36
详解Java中自定义注解的使用
2023-11-27 07:36:30
一步步教你把SpringBoot项目打包成Docker镜像
2022-01-10 12:21:02
C++实现图书馆管理系统源码
2022-05-19 14:30:02
Java设计模式之命令模式
2022-06-17 22:49:07
Spring security登录过程逻辑详解
2022-09-04 19:58:20
C#使用NOPI库实现导入Excel文档
2022-12-23 07:56:28
Android 详解Studio引用Library与导入jar
2022-05-23 17:53:14
C#表达式目录树示例详解
2021-10-01 01:52:46
Android利用Hero实现列表与详情页无缝切换动画
2023-07-22 08:04:39
Flutter进阶之实现动画效果(九)
2023-04-11 12:20:06
深入理解Java责任链模式实现灵活的请求处理流程
2022-06-06 23:33:53
kotlin源码结构层次详解
2022-08-19 14:24:48
Java同步锁Synchronized底层源码和原理剖析(推荐)
2023-09-25 08:36:22
android 判断网络是否可用与连接的网络是否能上网
2021-10-08 02:04:34
SpringAOP 如何通过JoinPoint获取参数名和值
2023-11-01 00:50:36
Unity3D更改默认的脚本编辑器
2021-06-05 14:58:53
Android自定义WaveProgressView实现水波纹加载需求
2021-07-29 02:06:00
Flutter实现底部导航栏
2023-05-18 15:10:26