網站首頁 編程語言 正文
本文實例為大家分享了C#實現簡單的計算器小功能的具體代碼,供大家參考,具體內容如下
先來張效果圖吧(5分鐘寫好,莫怪)
代碼:
數字按鈕綁定的是button_Clickd()方法
運算符按鈕綁的是Button_Clickp()方法
思想:按下數字按鈕,將數字按鈕的值連接到textbox上,然后按下運算符判斷是否為等于,并記錄運算符的內容,以便后面做處理。
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; ? namespace WindowsFormsApplication4 { ? ? public partial class Form2 : Form ? ? { ? ? ? ? private string s; ? ? ? ? private double x, y; ? ? ? ? private Button btn; ? ? ? ? ? ? ? ? public Form2() ? ? ? ? { ? ? ? ? ? ? InitializeComponent(); ? ? ? ? } ? ? ? ? ? ? private void Form2_Load(object sender, EventArgs e) ? ? ? ? { ? ? ? ? ? ? textBox1.Text = ""; ? ? ? ? ? ? label1.Text=""; ? ? ? ? ? } ? ? ? ? private void buttond_Click(object sender, EventArgs e)//數字符所綁定的事件 ? ? ? ? { ? ? ? ? ? ? btn = (Button)sender; ? ? ? ? ? ? textBox1.Text = textBox1.Text + btn.Text;//將所點擊的數字付呈現在textBox上面 ? ? ? ? ? } ? ? ? ? private void buttonp_Click(object sender, EventArgs e)//運算符所綁定的事件 ? ? ? ? { ? ? ? ? ? ? btn = (Button)sender; ? ? ? ? ? ? if (btn.Name != "button12")//如果不是"=" ? ? ? ? ? ? { ? ? ? ? ? ? ? ? x = Convert.ToDouble(textBox1.Text);//將所所輸入的第一個字符保留下來 ? ? ? ? ? ? ? ? textBox1.Text = "";//清空textBox的內容 ? ? ? ? ? ? ? ? s = btn.Name;//獲取運算符的種類 ? ? ? ? ? ? ? ? label1.Text = x.ToString();//將第一個所按的字符輸出來 ? ? ? ? ? ? ? ? } ? ? ? ? ? ? else ? ? ? ? ? ? { ? ? ? ? ? ? ? ? if (label1.Text == "") ? ? ? ? ? ? ? ? ? ? MessageBox.Show("輸入不正確!!", "信息提示", MessageBoxButtons.OK); ? ? ? ? ? ? ? ? else ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? y = Convert.ToDouble(textBox1.Text); ? ? ? ? ? ? ? ? ? ? switch (s)//使用s來判讀所按的按鈕 ? ? ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? ? ? case "button13": ? ? ? ? ? ? ? ? ? ? ? ? ? ? textBox1.Text = (x + y).ToString(); ? ? ? ? ? ? ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? ? ? ? ? ? ? case "button14": ? ? ? ? ? ? ? ? ? ? ? ? ? ? textBox1.Text = (x - y).ToString(); ? ? ? ? ? ? ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? ? ? ? ? ? ? case "button15": ? ? ? ? ? ? ? ? ? ? ? ? ? ? textBox1.Text = (x * y).ToString(); ? ? ? ? ? ? ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? ? ? ? ? ? ? ? case "button16": ? ? ? ? ? ? ? ? ? ? ? ? ? ? if (y == 0) ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? MessageBox.Show("除零錯誤!!!", "信息提示", MessageBoxButtons.OK); ? ? ? ? ? ? ? ? ? ? ? ? ? ? else ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? textBox1.Text = (x / y).ToString(); ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? label1.Text = textBox1.Text; ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? } ? ? ? ? } ? ? ? ? } }
原文鏈接:https://blog.csdn.net/qq_38345598/article/details/79561013
相關推薦
- 2022-12-10 c語言如何設置隨機數及逐行解析_C 語言
- 2022-04-09 數據庫連接池Driud使用JbbcUtils工具包
- 2022-03-16 VS2022?安裝.NET4.5目標包的方法_實用技巧
- 2021-12-15 go?gin+token(JWT)驗證實現登陸驗證_Golang
- 2022-07-11 Mongodb分片技術理論
- 2023-06-17 Flask中特殊裝飾器的使用_python
- 2022-07-04 一文搞懂???????python可迭代對象,迭代器,生成器,協程_python
- 2022-10-14 C語言數據結構之棧和隊列的實現及應用_C 語言
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支