C#實(shí)現(xiàn)簡(jiǎn)單加減乘除計(jì)算器
第一次學(xué)習(xí)C#,做了個(gè)簡(jiǎn)單的加減乘除計(jì)算器,只能實(shí)現(xiàn)兩個(gè)因數(shù)的運(yùn)算。
主要是練習(xí)下C#編程,和以前用過的VB差不多。與VB6不同的是,C#代碼區(qū)分大小寫。
Windows窗口程序主要也是由一些控件組成,響應(yīng)響應(yīng)的事件(event),實(shí)現(xiàn)具體的功能。
1.效果圖如下所示
2.代碼如下所示
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; namespace WindowsApplication1 { public partial class Main : Form { public Main() { InitializeComponent(); } private void Main_Load(object sender, EventArgs e) { } private void txtInshu1_TextChanged(object sender, EventArgs e) { } private void txtInshu1_KeyPress(object sender, KeyPressEventArgs e) { OnlyEnterNumber(sender, e); } //// <summary> /// 只能輸入數(shù)字(含負(fù)號(hào)小數(shù)點(diǎn)) /// </summary> /// <param name="sender"></param> /// <param name="e"></param> public static void OnlyEnterNumber(object sender, KeyPressEventArgs e) { if ((e.KeyChar < 48 || e.KeyChar > 57) && e.KeyChar != 8 && e.KeyChar != 13 && e.KeyChar != 45 && e.KeyChar != 46) { e.Handled = true; } // 輸入為負(fù)號(hào)時(shí),只能輸入一次且只能輸入一次 if (e.KeyChar == 45 && (((TextBox)sender).SelectionStart != 0 || ((TextBox)sender).Text.IndexOf("-") >= 0)) e.Handled = true; if (e.KeyChar == 46 && ((TextBox)sender).Text.IndexOf(".") >= 0) e.Handled = true; } /* * 參數(shù):d表示要四舍五入的數(shù);i表示要保留的小數(shù)點(diǎn)后位數(shù)。 * 正負(fù)數(shù)都四舍五入,適合數(shù)據(jù)統(tǒng)計(jì)的顯示 */ double Round(double d, int i) { if (d >= 0) { d += 5 * Math.Pow(10, -(i + 1)); } else { d += -5 * Math.Pow(10, -(i + 1)); } string str = d.ToString(); string[] strs = str.Split('.'); int idot = str.IndexOf('.'); string prestr = strs[0]; string poststr = strs[1]; if (poststr.Length > i) { poststr = str.Substring(idot + 1, i); } string strd = prestr + "." + poststr; d = Double.Parse(strd); return d; } private void txtInshu2_TextChanged(object sender, EventArgs e) { } private void txtInshu2_KeyPress_1(object sender, KeyPressEventArgs e) { OnlyEnterNumber(sender, e); } private void btnJisuan_Click(object sender, EventArgs e) { if (txtInshu1.Text == "") { MessageBox.Show("因數(shù)1不能為空!", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning); return; } if (txtInshu2.Text == "") { MessageBox.Show("因數(shù)2不能為空!", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning); return; } double inshu1 = Convert.ToDouble(txtInshu1.Text); double inshu2 = Convert.ToDouble(txtInshu2.Text); double result = 0.0; if (radioBtnJia.Checked) { result = inshu1 + inshu2; } if (radioBtnJian.Checked) { result = inshu1 - inshu2; } if (radioBtnCheng.Checked) { result = inshu1 * inshu2; } if (radioBtnChu.Checked) { if (0 == inshu2) { MessageBox.Show("因數(shù)2做除數(shù)不能為0!", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning); return; } result = inshu1 / inshu2; result = Round(result, 6); } txtResult.Text = Convert.ToString(result); } } }
因數(shù)輸入框只允許輸入數(shù)字和小數(shù)點(diǎn)負(fù)號(hào)的代碼是從網(wǎng)絡(luò)上引用的。
除法運(yùn)算時(shí)四舍五入的處理也是引用自網(wǎng)絡(luò)上的文章。
更多計(jì)算器功能實(shí)現(xiàn),請(qǐng)點(diǎn)擊專題: 計(jì)算器功能匯總 進(jìn)行學(xué)習(xí)
關(guān)于Android計(jì)算器功能的實(shí)現(xiàn),查看專題:Android計(jì)算器 進(jìn)行學(xué)習(xí)。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- C#計(jì)算器編寫代碼
- C#編寫的windows計(jì)算器的實(shí)例代碼
- C#開發(fā)簡(jiǎn)易winform計(jì)算器程序
- C#實(shí)現(xiàn)簡(jiǎn)單的計(jì)算器功能完整實(shí)例
- C#實(shí)現(xiàn)簡(jiǎn)單計(jì)算器功能
- C#實(shí)現(xiàn)Winform版計(jì)算器
- C#實(shí)現(xiàn)的簡(jiǎn)單整數(shù)四則運(yùn)算計(jì)算器功能示例
- c#入門之實(shí)現(xiàn)簡(jiǎn)易存款利息計(jì)算器示例
- C# WinForm程序設(shè)計(jì)簡(jiǎn)單計(jì)算器
- C#實(shí)現(xiàn)簡(jiǎn)單的計(jì)算器小程序
相關(guān)文章
在unity腳本中控制Inspector面板的參數(shù)操作
這篇文章主要介紹了在unity腳本中控制Inspector面板的參數(shù)操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2021-04-04c#實(shí)現(xiàn)萬(wàn)年歷示例分享 萬(wàn)年歷農(nóng)歷查詢
這篇文章主要介紹了c#實(shí)現(xiàn)萬(wàn)年歷的方法,可以顯示農(nóng)歷、節(jié)氣、節(jié)日、星座、星宿、屬相、生肖、閏年月、時(shí)辰,大家參考使用吧2014-01-01C#Process的OutputDataReceived事件不觸發(fā)問題及解決
這篇文章主要介紹了C#Process的OutputDataReceived事件不觸發(fā)問題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-02-02C#中三種Timer計(jì)時(shí)器的詳細(xì)用法
這篇文章介紹了C#中三種Timer計(jì)時(shí)器的詳細(xì)用法,文中通過示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-05-05c#圖片縮放圖片剪切功能實(shí)現(xiàn)(等比縮放)
c#圖片縮放剪切功能實(shí)現(xiàn),代碼中包含了c#圖片處理的一些基礎(chǔ)知識(shí),與大家分享2013-12-12