網站首頁 編程語言 正文
本文實例為大家分享了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 = "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
- 上一篇:Go中變量命名規則與實例_Golang
- 下一篇:C#實現溫度轉換功能_C#教程
相關推薦
- 2022-12-26 React開發進階redux?saga使用原理詳解_React
- 2023-03-22 R語言基礎數據類型的具體使用_R語言
- 2023-02-15 React?中使用?react-i18next?國際化的過程(react-i18next?的基本用法
- 2023-05-22 PyTorch小功能之TensorDataset解讀_python
- 2022-08-12 如何實現python爬蟲爬取視頻時實現實時進度條顯示_python
- 2022-12-05 C?sharp?(#)?數據類型獲取方式_C#教程
- 2022-05-10 gin實現限流中間件
- 2023-01-07 基于Go語言實現選擇排序算法及優化_Golang
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支