C#實(shí)現(xiàn)簡(jiǎn)單的計(jì)算器功能
本文實(shí)例為大家分享了C#實(shí)現(xiàn)簡(jiǎn)單的計(jì)算器功能的具體代碼,供大家參考,具體內(nèi)容如下
環(huán)境:VS2010及以上版本
1、建立個(gè)Window窗體應(yīng)用
2、在工具箱里拖出兩個(gè)TextBox,第一個(gè)放上面,第二個(gè)放下面 。主要這里的Name,上面是textBox1,下面是textBox2。這涉及到后面代碼的書寫
3、在工具欄里拖動(dòng)Button,擺放好??衫蒙厦娴膶?duì)齊工具輔助設(shè)計(jì)。
4、在屬性里改變各Button的Text,如下
注意這里的1~9,小數(shù)點(diǎn),±*/ 的Text應(yīng)只有一個(gè)字符,不要多輸。←
5、選中任意一個(gè)Button,右鍵,選擇查看代碼,轉(zhuǎn)到Form1.cs
6、開始寫代碼
AddNum 修改TextBox的Text,應(yīng)用于1~9與小數(shù)點(diǎn)的Click事件
Reset 重置temp、myoperator,以及兩個(gè)TextBox的Text
Delete 刪除textBox2的Text最后一個(gè)字符
Calculate 把textBox2的Text轉(zhuǎn)為double給temp,修改myoperator
Equal 具體的計(jì)算
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace WindowsFormsApp1 { ? ? public partial class Form1 : Form ? ? { ? ? ? ? public Form1() ? ? ? ? { ? ? ? ? ? ? InitializeComponent(); ? ? ? ? } //----上面是自動(dòng)生成的代碼,下面得我們手寫---- ? ? ? ? private double temp = 0; ?//存儲(chǔ)臨時(shí)數(shù)據(jù) ? ? ? ? private char myoperator = ' '; ?//判斷之前按的是+-*/中的哪個(gè) ? ? ? ? private void AddNum(object sender, EventArgs e) ? ? ? ? { ? // 1~9與小數(shù)點(diǎn)的Click事件 ? ? ? ? ? ? //sender是引發(fā)該事件的控件,這里我們拆箱為Button ? ? ? ? ? ? Button button = (Button)sender; ? ? ? ? ? ? textBox2.Text += button.Text; ? ? ? ? } ? ? ? ? private void Reset(object sender, EventArgs e) ? ? ? ? { ? // CE的Click事件 ? ? ? ? ? ? temp = 0; ? ? ? ? ? ? myoperator = ' '; ? ? ? ? ? ? textBox1.Text = textBox2.Text = ""; ? ? ? ? } ? ? ? ? private void Delete(object sender, EventArgs e) ? ? ? ? { ? // ←的Click事件 ? ? ? ? ? ? //移除最后一個(gè)字符 ? ? ? ? ? ? if (textBox2.TextLength > 0) ? ? ? ? ? ? ? ? textBox2.Text = textBox2.Text.Remove(textBox2.TextLength - 1); ? ? ? ? } ? ? ? ? private void Calculate(object sender, EventArgs e) ? ? ? ? { ? // +-*/的Click事件 ? ? ? ? ? ? Button button = (Button)sender; ? ? ? ? ? ? if (double.TryParse(textBox2.Text, out temp)) ?//嘗試把textBox2的Text轉(zhuǎn)為double并賦值給temp ? ? ? ? ? ? { ? ? ? ? ? ? ? ? myoperator = button.Text[0]; //Text是string,取第一個(gè)字符 ? ? ? ? ? ? ? ? textBox1.Text = temp.ToString() + ' ' + myoperator; ? ? ? ? ? ? ? ? textBox2.Text = ""; ? ? ? ? ? ? } ? ? ? ? ? ? else ? ? ? ? ? ? { ? //轉(zhuǎn)換失敗,重置所有 ? ? ? ? ? ? ? ? Reset(sender, e); ? ? ? ? ? ? } ? ? ? ? } ? ? ? ? private void Equal(object sender, EventArgs e) ? ? ? ? { ? // = 的Click事件,計(jì)算并顯示 ? ? ? ? ? ? double temp2; ? ? ? ? ? ? //嘗試轉(zhuǎn)換,失敗則重置并返回 ? ? ? ? ? ? if (!double.TryParse(textBox2.Text, out temp2)) { Reset(sender, e); return; } ? ? ? ? ? ? switch (myoperator) ? ? ? ? ? ? { ? ? ? ? ? ? ? ? case '+': ? ? ? ? ? ? ? ? ? ? temp += temp2; ? ? ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? ? ? case '-': ? ? ? ? ? ? ? ? ? ? temp -= temp2; ? ? ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? ? ? case '*': ? ? ? ? ? ? ? ? ? ? temp *= temp2; ? ? ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? ? ? case '/': ? ? ? ? ? ? ? ? ? ? temp /= temp2; ? ? ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? ? ? default: ? ? ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? } ? ? ? ? ? ? textBox1.Text = ""; ? ? ? ? ? ? textBox2.Text = temp.ToString(); ? ? ? ? } ? ? } }
7、設(shè)置各Button的Click事件
AddNum: 1~9與小數(shù)點(diǎn)的Click事件
Reset:CE的Click事件
Delete:←的Click事件
Calculate :±*/的Click事件
Equal:= 的Click事件
8、啟動(dòng)(F5)
以上就是本文的全部?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)簡(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ì)算器
相關(guān)文章
WinFrom中l(wèi)abel背景透明的實(shí)現(xiàn)方法
這篇文章主要介紹了WinFrom中l(wèi)abel背景透明的實(shí)現(xiàn)方法,方法簡(jiǎn)單實(shí)用,是C#程序設(shè)計(jì)中非常實(shí)用的技巧,需要的朋友可以參考下2014-09-09winform關(guān)閉窗體FormClosing事件用法介紹
這篇文章介紹了winform關(guān)閉窗體FormClosing事件的用法,文中通過示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-03-03c# OpenCvSharp實(shí)現(xiàn)常見檢測(cè)(斑點(diǎn)檢測(cè),輪廓檢測(cè),邊緣檢測(cè))
這篇文章主要為大家詳細(xì)介紹了c#如何使用OpenCvSharp實(shí)現(xiàn)常見檢測(cè)(斑點(diǎn)檢測(cè),輪廓檢測(cè),邊緣檢測(cè)),文中的示例代碼講解詳細(xì),需要的小伙伴可以參考下2023-12-12c#中WebService的介紹及調(diào)用方式小結(jié)
這篇文章主要給大家介紹了關(guān)于c#中的WebService及其調(diào)用方式的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-11-11c#根據(jù)網(wǎng)址抓取網(wǎng)頁(yè)截屏生成圖片的示例
本文主要介紹了c#根據(jù)網(wǎng)址抓取網(wǎng)頁(yè)截屏生成圖片并保存的示例,代碼中使用了WebBrowser控件來完成這個(gè)功能,大家參考使用吧2014-01-01C#使用foreach語(yǔ)句遍歷隊(duì)列(Queue)的方法
這篇文章主要介紹了C#使用foreach語(yǔ)句遍歷隊(duì)列(Queue)的方法,涉及foreach語(yǔ)句的使用技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-04-04