網站首頁 編程語言 正文
本文實例為大家分享了C#實現簡單的計算器功能的具體代碼,供大家參考,具體內容如下
1.界面設計
2.代碼
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 calculator3 { ? ? public partial class Form1 : Form ? ? { ? ? ? ? private string num1, num2;//計算器的操作數,成員變量 ? ? ? ? private string opr;//操作符 ? ? ? ? public Form1() ? ? ? ? { ? ? ? ? ? ? InitializeComponent(); ? ? ? ? } ? ? ? ? //數字按鈕點擊事件的方法 ? ? ? ? private void NumClick(object sender, EventArgs e) ? ? ? ? { ? ? ? ? ? ? Button button = (Button)sender; ? ? ? ? ? ? if (string.IsNullOrEmpty(opr))//如果還沒有輸入操作符 ? ? ? ? ? ? { ? ? ? ? ? ? ? ? num1 = num1 + button.Text;//輸入第一個參與運算的數;字符串的鏈接個十百千 ? ? ? ? ? ? } ? ? ? ? ? ? else ? ? ? ? ? ? { ? ? ? ? ? ? ? ? num2 = num2 + button.Text;//輸入第二個參與運算的數;字符串的鏈接個十百千 ? ? ? ? ? ? } ? ? ? ? ? ? txtResult.Text = txtResult.Text + button.Text; ? ? ? ? } ? ? ? ? //操作符按鈕點擊事件的方法 ? ? ? ? private void oprClick(object sender, EventArgs e) ? ? ? ? { ? ? ? ? ? ? Button button=(Button)sender; ? ? ? ? ? ? if (String.IsNullOrEmpty(num2))//如果還沒有輸入數字,則不允許按操作符 ? ? ? ? ? ? { ? ? ? ? ? ? ? ? MessageBox.Show("此時不應該按入操作符!"); ? ? ? ? ? ? ? ? return; ? ? ? ? ? ? } ? ? ? ? ? ? opr = button.Text; ? ? ? ? ? ? txtResult.Text = txtResult.Text + button.Text; ? ? ? ? } ? ? ? ? //“=”事件,即計算 ? ? ? ? private void btnGet_Click(object sender, EventArgs e) ? ? ? ? { ? ? ? ? ? ? if (String.IsNullOrEmpty(opr) ? ? ? ? ? ? ? ? || String.IsNullOrEmpty(num1) ? ? ? ? ? ? ? ? || String.IsNullOrEmpty(num2)) ? ? ? ? ? ? { ? ? ? ? ? ? ? ? MessageBox.Show("您輸入的內容有誤!"); ? ? ? ? ? ? ? ? return; ? ? ? ? ? ? } ? ? ? ? ?? ? ? ? ? ? ?? ? ? ? ? ? ? ? ? txtResult.Text = txtResult.Text + "=";//將“=”拼接到框框里 ? ? ? ? ? ? //進行兩個數的運算 ? ? ? ? ? ? ? ? switch (opr) ? ? ? ? ? ? ? ? {? ? ? ? ? ? ? ? ? ? ? case "+": ? ? ? ? ? ? ? ? ? ? ? ? txtResult.Text = txtResult.Text + (Int32.Parse(num1) + Int32.Parse(num2)); ? ? ? ? ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? ? ? ? ? case "-": ? ? ? ? ? ? ? ? ? ? ? ? txtResult.Text = txtResult.Text + (Int32.Parse(num1) - Int32.Parse(num2)); ? ? ? ? ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? ? ? ? ? case "*": ? ? ? ? ? ? ? ? ? ? ? ? txtResult.Text = txtResult.Text + (Int32.Parse(num1) * Int32.Parse(num2)); ? ? ? ? ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? ? ? ? ? case "/": ? ? ? ? ? ? ? ? ? ? ? ? if (num2 == "0") ? ? ? ? ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? ? ? ? ? MessageBox.Show("除數不可以為零!"); ? ? ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? ? ? txtResult.Text = txtResult.Text + (Int32.Parse(num1) / Int32.Parse(num2)); ? ? ? ? ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? ? ? } ? ? ? ? ? ?? ? ? ? ? } ? ? ? ? //清除事件 ? ? ? ? private void btnClear_Click(object sender, EventArgs e) ? ? ? ? { ? ? ? ? ? ? txtResult.Text = ""; ? ? ? ? ? ? num1 = ""; ? ? ? ? ? ? num2 = ""; ? ? ? ? ? ? opr = ""; ? ? ? ? } ?? ? ? } }
3.總結分析
按鈕點擊事件:當多數按鈕的點擊效果一致時,可使用同一個Click事件(名字一致即可)
//僅作舉例使用 //關鍵代碼 Button button = (Button)sender; //此時字符串的鏈接 num1 = num1 + button.Text;//輸入第一個參與運算的數;字符串的鏈接個十百千
代碼不足之處
僅供兩個操作數的運算使用,新加操作數比較麻煩
原文鏈接:https://blog.csdn.net/draumur_/article/details/115420129
相關推薦
- 2022-05-09 python中pip安裝庫時出現Read?timed?out解決辦法_python
- 2022-09-21 Redis緩存更新策略詳解_Redis
- 2023-02-06 Python利用Pytorch實現繪制ROC與PR曲線圖_python
- 2022-02-25 Oracle工具PL/SQL的基本語法_oracle
- 2024-07-13 Spring AOP 基于注解的方式實現切面遍程
- 2022-04-19 jQuery下實現等待指定元素加載完畢
- 2022-07-07 python?如何求N的階乘_python
- 2022-06-17 C#中Parallel類For、ForEach和Invoke使用介紹_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同步修改后的遠程分支