日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學(xué)無先后,達(dá)者為師

網(wǎng)站首頁 編程語言 正文

C#實(shí)現(xiàn)單位換算器_C#教程

作者:Link2Points ? 更新時間: 2022-03-31 編程語言

本文實(shí)例為大家分享了C#實(shí)現(xiàn)單位換算器的具體代碼,供大家參考,具體內(nèi)容如下

一、闡述

進(jìn)制間轉(zhuǎn)換:十六進(jìn)制、十進(jìn)制、八進(jìn)制、二進(jìn)制。
長度間轉(zhuǎn)換:毫米、厘米、米、公里、英寸、英尺、碼。
面積間轉(zhuǎn)換:平方毫米、平方厘米、平方米、平方公里、平方英寸、平方英尺、平方碼。
體積間轉(zhuǎn)換:立方毫米、毫升、升、立方米、立方英寸、立方英尺、立方碼。

二、效果

1. 進(jìn)制轉(zhuǎn)換

通過輸入即可轉(zhuǎn)換為另一個進(jìn)制值。

2. 長度轉(zhuǎn)換

用戶輸入進(jìn)行單位換算,可從目標(biāo)中選擇需求單位。

選中需求后,輸入框解除只讀,輸入即可進(jìn)行換算。

3. 面積轉(zhuǎn)換

同樣,使用正則匹配對正數(shù)小數(shù)和正整數(shù),而進(jìn)制轉(zhuǎn)換另外規(guī)則匹配。

4. 體積轉(zhuǎn)換

當(dāng)重選單位類型時會清空輸入框。

代碼

using System;
using System.Text.RegularExpressions;
using System.Windows.Forms;

namespace UnitConversion
{
? public partial class Form1 : Form
? {
? ? // 默認(rèn)選項(xiàng)
? ? private string unitItem = "進(jìn)制";
? ? // 用戶選擇單位度量 的 轉(zhuǎn)換基數(shù)
? ? private double customConversionBase = 0;
? ? public Form1()
? ? {
? ? ? InitializeComponent();
? ? }

? ? private void Form1_Load(object sender, EventArgs e)
? ? {
? ? ? unitComboBox.Items.Add("進(jìn)制");
? ? ? unitComboBox.Items.Add("長度");
? ? ? unitComboBox.Items.Add("面積");
? ? ? unitComboBox.Items.Add("體積");
? ? }
? ? /*
? ? ?* 進(jìn)制轉(zhuǎn)換
? ? ?*/
? ? private void BaseConversion(TextBox text, string val)
? ? {
? ? ? if (text == textBox1 && Regex.IsMatch(val, @"[0-1]+$")) ? ? ? ? ? ? ? ?//二進(jìn)制轉(zhuǎn)換
? ? ? {
? ? ? ? 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]+$")) ? ? ? ? ?//八進(jìn)制轉(zhuǎn)換
? ? ? {
? ? ? ? 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]+$")) ? ? ? ? //十進(jìn)制轉(zhuǎn)換
? ? ? {
? ? ? ? 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]+$")) ?//十六機(jī)制轉(zhuǎn)換
? ? ? {
? ? ? ? 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;
? ? ? ? // 不為空 及 正整數(shù)或正小數(shù) ?(進(jìn)制另外進(jìn)一步匹配)
? ? ? ? if (text.Text == "")
? ? ? ? {
? ? ? ? ? return;
? ? ? ? }
? ? ? ? if (unitItem == "進(jìn)制")
? ? ? ? {
? ? ? ? ? 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)
? ? {
? ? ? // 清空 及 復(fù)位
? ? ? 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 "進(jìn)制":?
? ? ? ? ? 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 = "m2";
? ? ? ? ? label2.Text = "cm2";
? ? ? ? ? label3.Text = "m2";
? ? ? ? ? label4.Text = "km2";
? ? ? ? ? unitListBox.Items.Add("inch2");
? ? ? ? ? unitListBox.Items.Add("foot2");
? ? ? ? ? unitListBox.Items.Add("yard2");
? ? ? ? ? break;
? ? ? ? case "體積":
? ? ? ? ? label1.Text = "cm3";
? ? ? ? ? label2.Text = "mL";
? ? ? ? ? label3.Text = "L";
? ? ? ? ? label4.Text = "m3";
? ? ? ? ? unitListBox.Items.Add("inch3");
? ? ? ? ? unitListBox.Items.Add("foot3");
? ? ? ? ? unitListBox.Items.Add("yard3");
? ? ? ? ? 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 "inch2":
? ? ? ? ? ? customConversionBase = 0.001_55;
? ? ? ? ? ? break;
? ? ? ? ? case "foot2":
? ? ? ? ? ? customConversionBase = 0.000_011;
? ? ? ? ? ? break;
? ? ? ? ? case "yard2":
? ? ? ? ? ? customConversionBase = 0.000_001;
? ? ? ? ? ? break;
? ? ? ? ? case "inch3":
? ? ? ? ? ? customConversionBase = 0.061_024;
? ? ? ? ? ? break;
? ? ? ? ? case "foot3":
? ? ? ? ? ? customConversionBase = 0.000_035;
? ? ? ? ? ? break;
? ? ? ? ? case "yard3":
? ? ? ? ? ? customConversionBase = 0.000_001;
? ? ? ? ? ? break;
? ? ? ? }
? ? ? }
? ? }
? }
}

原文鏈接:https://blog.csdn.net/Link2Points/article/details/118691379

欄目分類
最近更新