網(wǎng)站首頁 編程語言 正文
本文實(shí)例為大家分享了C#實(shí)現(xià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)
原文鏈接:https://blog.csdn.net/weixin_46068322/article/details/114920031
相關(guān)推薦
- 2023-05-31 Pandas多個(gè)條件(AND,OR,NOT)中提取行_python
- 2022-11-01 Go語言框架快速集成限流中間件詳解_Golang
- 2022-07-10 移動(dòng)文件夾ubuntu
- 2022-03-27 詳解OpenCV中簡單的鼠標(biāo)事件處理_python
- 2022-09-08 python筆記之使用fillna()填充缺失值_python
- 2023-07-14 如何限制請(qǐng)求的并發(fā)數(shù)量
- 2022-05-15 Python?中的集合和字典_python
- 2022-04-05 如何定義多個(gè)context:property-placeholder配置
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支