C#實(shí)現(xiàn)簡單加減乘除計(jì)算器
第一次學(xué)習(xí)C#,做了個簡單的加減乘除計(jì)算器,只能實(shí)現(xiàn)兩個因數(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ù)號小數(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ù)號時,只能輸入一次且只能輸入一次 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ù)號的代碼是從網(wǎng)絡(luò)上引用的。
除法運(yùn)算時四舍五入的處理也是引用自網(wǎng)絡(luò)上的文章。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持我們。
欄 目:C#教程
下一篇:詳解C# 不能用于文件名的字符
本文標(biāo)題:C#實(shí)現(xiàn)簡單加減乘除計(jì)算器
本文地址:http://mengdiqiu.com.cn/a1/C_jiaocheng/5272.html
您可能感興趣的文章
- 01-10C#實(shí)現(xiàn)txt定位指定行完整實(shí)例
- 01-10WinForm實(shí)現(xiàn)仿視頻 器左下角滾動新聞效果的方法
- 01-10C#實(shí)現(xiàn)清空回收站的方法
- 01-10C#實(shí)現(xiàn)讀取注冊表監(jiān)控當(dāng)前操作系統(tǒng)已安裝軟件變化的方法
- 01-10C#實(shí)現(xiàn)多線程下載文件的方法
- 01-10C#實(shí)現(xiàn)Winform中打開網(wǎng)頁頁面的方法
- 01-10C#實(shí)現(xiàn)遠(yuǎn)程關(guān)閉計(jì)算機(jī)或重啟計(jì)算機(jī)的方法
- 01-10C#自定義簽名章實(shí)現(xiàn)方法
- 01-10C#文件斷點(diǎn)續(xù)傳實(shí)現(xiàn)方法
- 01-10winform實(shí)現(xiàn)創(chuàng)建最前端窗體的方法


閱讀排行
本欄相關(guān)
- 01-10C#通過反射獲取當(dāng)前工程中所有窗體并
- 01-10關(guān)于ASP網(wǎng)頁無法打開的解決方案
- 01-10WinForm限制窗體不能移到屏幕外的方法
- 01-10WinForm繪制圓角的方法
- 01-10C#實(shí)現(xiàn)txt定位指定行完整實(shí)例
- 01-10WinForm實(shí)現(xiàn)仿視頻 器左下角滾動新
- 01-10C#停止線程的方法
- 01-10C#實(shí)現(xiàn)清空回收站的方法
- 01-10C#通過重寫Panel改變邊框顏色與寬度的
- 01-10C#實(shí)現(xiàn)讀取注冊表監(jiān)控當(dāng)前操作系統(tǒng)已
隨機(jī)閱讀
- 08-05dedecms(織夢)副欄目數(shù)量限制代碼修改
- 01-10delphi制作wav文件的方法
- 01-10使用C語言求解撲克牌的順子及n個骰子
- 08-05DEDE織夢data目錄下的sessions文件夾有什
- 01-11ajax實(shí)現(xiàn)頁面的局部加載
- 08-05織夢dedecms什么時候用欄目交叉功能?
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 01-10C#中split用法實(shí)例總結(jié)
- 04-02jquery與jsp,用jquery
- 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置