C#實現(xiàn)簡單的計算器功能
本文實例為大家分享了C#實現(xiàn)簡單的計算器功能的具體代碼,供大家參考,具體內(nèi)容如下
環(huán)境:VS2010及以上版本
1、建立個Window窗體應用
2、在工具箱里拖出兩個TextBox,第一個放上面,第二個放下面 。主要這里的Name,上面是textBox1,下面是textBox2。這涉及到后面代碼的書寫
3、在工具欄里拖動Button,擺放好??衫蒙厦娴膶R工具輔助設計。
4、在屬性里改變各Button的Text,如下
注意這里的1~9,小數(shù)點,±*/ 的Text應只有一個字符,不要多輸。←
5、選中任意一個Button,右鍵,選擇查看代碼,轉到Form1.cs
6、開始寫代碼
AddNum 修改TextBox的Text,應用于1~9與小數(shù)點的Click事件
Reset 重置temp、myoperator,以及兩個TextBox的Text
Delete 刪除textBox2的Text最后一個字符
Calculate 把textBox2的Text轉為double給temp,修改myoperator
Equal 具體的計算
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(); ? ? ? ? } //----上面是自動生成的代碼,下面得我們手寫---- ? ? ? ? private double temp = 0; ?//存儲臨時數(shù)據(jù) ? ? ? ? private char myoperator = ' '; ?//判斷之前按的是+-*/中的哪個 ? ? ? ? private void AddNum(object sender, EventArgs e) ? ? ? ? { ? // 1~9與小數(shù)點的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事件 ? ? ? ? ? ? //移除最后一個字符 ? ? ? ? ? ? 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轉為double并賦值給temp ? ? ? ? ? ? { ? ? ? ? ? ? ? ? myoperator = button.Text[0]; //Text是string,取第一個字符 ? ? ? ? ? ? ? ? textBox1.Text = temp.ToString() + ' ' + myoperator; ? ? ? ? ? ? ? ? textBox2.Text = ""; ? ? ? ? ? ? } ? ? ? ? ? ? else ? ? ? ? ? ? { ? //轉換失敗,重置所有 ? ? ? ? ? ? ? ? Reset(sender, e); ? ? ? ? ? ? } ? ? ? ? } ? ? ? ? private void Equal(object sender, EventArgs e) ? ? ? ? { ? // = 的Click事件,計算并顯示 ? ? ? ? ? ? double temp2; ? ? ? ? ? ? //嘗試轉換,失敗則重置并返回 ? ? ? ? ? ? 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、設置各Button的Click事件
AddNum: 1~9與小數(shù)點的Click事件
Reset:CE的Click事件
Delete:←的Click事件
Calculate :±*/的Click事件
Equal:= 的Click事件
8、啟動(F5)
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
WinFrom中l(wèi)abel背景透明的實現(xiàn)方法
這篇文章主要介紹了WinFrom中l(wèi)abel背景透明的實現(xiàn)方法,方法簡單實用,是C#程序設計中非常實用的技巧,需要的朋友可以參考下2014-09-09c# OpenCvSharp實現(xiàn)常見檢測(斑點檢測,輪廓檢測,邊緣檢測)
這篇文章主要為大家詳細介紹了c#如何使用OpenCvSharp實現(xiàn)常見檢測(斑點檢測,輪廓檢測,邊緣檢測),文中的示例代碼講解詳細,需要的小伙伴可以參考下2023-12-12c#根據(jù)網(wǎng)址抓取網(wǎng)頁截屏生成圖片的示例
本文主要介紹了c#根據(jù)網(wǎng)址抓取網(wǎng)頁截屏生成圖片并保存的示例,代碼中使用了WebBrowser控件來完成這個功能,大家參考使用吧2014-01-01