網站首頁 編程語言 正文
本文實例為大家分享了C#實現計算器功能的具體代碼,供大家參考,具體內容如下
代碼:
Random rad = new Random(); // 實例化隨機對象 ? ? ? ? private void Form1_Load(object sender, EventArgs e) ? ? ? ? { ? ? ? ? ? ? this.Left = Screen.PrimaryScreen.WorkingArea.Width / 2 - this.Width / 2; ? ? ? ? ? ? this.Top = Screen.PrimaryScreen.WorkingArea.Height / 2 - this.Height / 2; ? ? ? ? ? ? this.Text = "計算器"; ? ? ? ? ? ? textBox1.ReadOnly =true;// 文本框無法輸入字符 ? ? ? ? ? ? foreach (Control ctl in this.Controls) ? ? ? ? ? ? { ? // 獲取所有按鈕 ?改變背景顏色和數字和符號顏色 ? ? ? ? ? ? ? ? if (ctl is Button) ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? Button btn = ctl as Button; ? ? ? ? ? ? ? ? ? ? btn.BackColor = Color.FromArgb(rad.Next(256),rad.Next(256),rad.Next(256),rad.Next(256)); ? ? ? ? ? ? ? ? ? ? btn.ForeColor = Color.FromArgb(rad.Next(256), rad.Next(256), rad.Next(256)); ? ? ? ? ? ? ? ? } ? ? ? ? ? ? } ? ? ? ? } ? ? ? ? private void 關閉ToolStripMenuItem_Click(object sender, EventArgs e) ? ? ? ? { ? ? ? ? ? ? this.Close();// 關閉窗體 ? ? ? ? } ? ? ? ? char fuhao;// 接收符號 ? ? ? ? double temp, num; // temp第一個值,num為第二個值 ? ? ? ?// 1 ? ? ? ? private void button1_Click(object sender, EventArgs e) ? ? ? ? { ? ? ? ? ? ? textBox1.Text += "1"; ? ? ? ? } ? ? ? ? // 2 ? ? ? ? private void button2_Click(object sender, EventArgs e) ? ? ? ? { ? ? ? ? ? ? textBox1.Text += "2"; ? ? ? ? } ? ? ? ? // 3 ? ? ? ? private void button3_Click(object sender, EventArgs e) ? ? ? ? { ? ? ? ? ? ? textBox1.Text += "3"; ? ? ? ? } ? ? ? ? // 4 ? ? ? ? private void button5_Click(object sender, EventArgs e) ? ? ? ? { ? ? ? ? ? ? textBox1.Text += "4"; ? ? ? ? } ? ? ? ? // 5 ? ? ? ? private void button6_Click(object sender, EventArgs e) ? ? ? ? { ? ? ? ? ? ? textBox1.Text += "5"; ? ? ? ? } ? ? ? ? // 6 ? ? ? ? private void button7_Click(object sender, EventArgs e) ? ? ? ? { ? ? ? ? ? ? textBox1.Text += "6"; ? ? ? ? } ? ? ? ? // 7 ? ? ? ? private void button9_Click(object sender, EventArgs e) ? ? ? ? { ? ? ? ? ? ? textBox1.Text += "7"; ? ? ? ? } ? ? ? ? // 8 ? ? ? ? private void button10_Click(object sender, EventArgs e) ? ? ? ? { ? ? ? ? ? ? textBox1.Text += "8"; ? ? ? ? } ? ? ? ? // 9 ? ? ? ? private void button11_Click(object sender, EventArgs e) ? ? ? ? { ? ? ? ? ? ? textBox1.Text += "9"; ? ? ? ? } ? ? ? ? // 0 ? ? ? ? private void button15_Click(object sender, EventArgs e) ? ? ? ? { ? ? ? ? ? ? textBox1.Text += "0"; ? ? ? ? } ? ? ? ? //點 ? ? ? ? private void button16_Click(object sender, EventArgs e) ? ? ? ? { ? ? ? ? ? ? textBox1.Text += "."; ? ? ? ? } ? ? ? ? // + ? ? ? ? private void button4_Click(object sender, EventArgs e) ? ? ? ? { ? ? ? ? ? ? num = double.Parse(textBox1.Text); ? ? ? ? ? ? fuhao = '+'; ? ? ? ? ? ? textBox1.Text += "+"; ? ? ? ? ? ? //textBox1.Text = null; ? ? ? ? } ? ? ? ? // - ? ? ? ? private void button8_Click(object sender, EventArgs e) ? ? ? ? { ? ? ? ? ? ? num = double.Parse(textBox1.Text); ? ? ? ? ? ? fuhao = '-'; ? ? ? ? ? ? textBox1.Text = null; ? ? ? ? } ? ? ? ? // × ? ? ? ? private void button12_Click(object sender, EventArgs e) ? ? ? ? { ? ? ? ? ? ? num = double.Parse(textBox1.Text); ? ? ? ? ? ? fuhao = '×'; ? ? ? ? ? ? textBox1.Text = null; ? ? ? ? } ? ? ? ? // ÷ ? ? ? ? private void button20_Click(object sender, EventArgs e) ? ? ? ? { ? ? ? ? ? ? num = double.Parse(textBox1.Text); ? ? ? ? ? ? fuhao = '÷'; ? ? ? ? ? ? textBox1.Text = null; ? ? ? ? } ? ? ? ? // % ? ? ? ? private void button19_Click(object sender, EventArgs e) ? ? ? ? { ? ? ? ? ? ? num = double.Parse(textBox1.Text); ? ? ? ? ? ? fuhao = '%'; ? ? ? ? ? ? textBox1.Text = null; ? ? ? ? } ? ? ? ? // = ? ? ? ? private void button13_Click(object sender, EventArgs e) ? ? ? ? { ? ? ? ? ? ? temp = double.Parse(textBox1.Text); ? ? ? ? ? ? switch (fuhao) ? ? ? ? ? ? { ? ? ? ? ? ? ? ? case '+': ? ? ? ? ? ? ? ? ? ? num += temp; ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? ? ? case '-': ? ? ? ? ? ? ? ? ? ? num -= temp; ? ? ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? ? ? case '×': ? ? ? ? ? ? ? ? ? ? num *= temp; ? ? ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? ? ? case '÷': ? ? ? ? ? ? ? ? ? ? num /= temp; ? ? ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? ? ? case '%': ? ? ? ? ? ? ? ? ? ? num %= temp; ? ? ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? ? ? default: ? ? ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? } ? ? ? ? ? ? textBox1.Text = num.ToString(); ? ? ? ? ? ? fuhao = ' '; ? ? ? ? ? ? num = 0; ? ? ? ? ? ? temp = 0; ? ? ? ? } ? ? ? ? // 刪除 ? ? ? ? private void button18_Click(object sender, EventArgs e) ? ? ? ? { ? ? ? ? ? ? if (textBox1.Text.Length > 0) ? ? ? ? ? ? { ? ? ? ? ? ? ? ? textBox1.Text = textBox1.Text.Substring(0, textBox1.Text.Length - 1); ? ? ? ? ? ? } ? ? ? ? } ? ? ? ? //清空 ? ? ? ? private void button17_Click(object sender, EventArgs e) ? ? ? ? { ? ? ? ? ? ? textBox1.Text = ""; ? ? ? ? }
原文鏈接:https://blog.csdn.net/dust__/article/details/103076040
相關推薦
- 2022-07-15 Python打印數據類型的全過程_python
- 2022-04-11 MVVMLight項目Model?View結構及全局視圖模型注入器_Android
- 2022-09-16 Pandas數據連接pd.concat的實現_python
- 2022-08-23 C++簡明分析inline函數的使用_C 語言
- 2022-04-08 python?selenium保存圖片最好的兩種方法_python
- 2022-04-24 C語言時間函數之strftime()詳解_C 語言
- 2022-10-22 PyTorch中的CUDA的操作方法_python
- 2021-12-03 C++中signed?main和int?main的區別_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同步修改后的遠程分支