網站首頁 編程語言 正文
本文實例為大家分享了C#實現計算器功能的具體代碼,供大家參考,具體內容如下
在剛剛接觸c#的時候,就想做一個簡單加減乘除計算器。這就是目標,可惜一直沒有動手去做,今天特意把它簡單做了。很簡單,很簡單,了卻一個心愿了。代碼:
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 MyPictureDownloader { ? ? //http://blog.sina.com.cn/s/blog_60d576800100tf5z.html ? ? //http://jingyan.baidu.com/article/380abd0a6b80701d90192cde.html ? ? public partial class JiSuanQi : Form ? ? { ? ? ? ? public JiSuanQi() ? ? ? ? { ? ? ? ? ? ? InitializeComponent(); ? ? ? ? ? ? initButton(); ? ? ? ? } ? ? ? ? public delegate double Operater(double num1, double num2); ? ? ? ? public void initButton() ? ? ? ? { ? ? ? ? ? ?var p = new Point(20, 80); ? ? ? ? ? ?Button[] listbtn = ?new Button[9]; ? ? ? ? ? ? for (int i = 0; i < 9; i++)? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? listbtn[i] = new Button(); ? ? ? ? ? ? ? ? listbtn[i].Name = "btn" + (i + 1).ToString(); ? ? ? ? ? ? ? ? listbtn[i].Text = (i+1).ToString(); ? ? ? ? ? ? ? ? listbtn[i].SetBounds(p.X, p.Y, 50, 50); ? ? ? ? ? ? ? ? listbtn[i].Click+= new System.EventHandler(ClickHandler); ? ? ? ? ? ? ? ? this.Controls.Add(listbtn[i]); ? ? ? ? ? ? ? ? p.X += 80; ? ? ? ? ? ? ? ? if (p.X >= this.Width - 80) ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? p.X =20; ? ? ? ? ? ? ? ? ? ? p.Y += 60; ? ? ? ? ? ? ? ? } ? ? ? ? ? ? } ? ? ? ? } ? ? ? ? public void ClickHandler(Object sender, System.EventArgs e) ? ? ? ? { ? ? ? ? ? ? Button btn = (Button)sender; ?? ? ? ? ? ? ? string temp=txtnum.Text.ToString()+btn.Text;//這樣解決了重復點擊賦值問題 ? ? ? ? ? ? txtnum.Text = temp; ? ? ? ? } ? ? ? ? private void btnzero_Click(object sender, EventArgs e) ? ? ? ? { ? ? ? ? ? ? string temp = txtnum.Text.ToString() + btnzero.Text;//這樣解決了重復點擊賦值問題 ? ? ? ? ? ? txtnum.Text = temp; ? ? ? ? } ? ? ? ? public double jisuan(string caozuofu, Operater fanga) ? ? ? ? { ? ? ? ? ? ? double num2 = double.Parse(txtnum.Text); ? ? ? ? ? ? double jieguo = 0; ? ? ? ? ? ? //switch(caozuofu){ ? ? ? ? ? ? // ? ?case"+": ? ? ? ? ? ? // ? ? ? ?jieguo = fanga(tempnum, num2); ? ? ? ? ? ? // ? ? ? ?break; ? ? ? ? ? ? // ? ?case "-": ? ? ? ? ? ? // ? ? ? ?jieguo = fanga(tempnum, num2); ? ? ? ? ? ? // ? ? ? ?break; ? ? ? ? ? ? // ? ?case "*": ? ? ? ? ? ? // ? ? ? ?jieguo = fanga(tempnum, num2); ? ? ? ? ? ? // ? ? ? ?break; ? ? ? ? ? ? // ? ?case "/": ? ? ? ? ? ? // ? ? ? ?jieguo = fanga(tempnum, num2); ? ? ? ? ? ? // ? ? ? ?break; ? ? ? ? ? ? //} ? ? ? ? ? ? jieguo = fanga(tempnum, num2); ? ? ? ? ? ? return jieguo; ? ? ? ? } ? ? ? ? public double add(double num1, double num2)? ? ? ? ? { ? ? ? ? ? ? return num1 + num2; ? ? ? ? } ? ? ? ? public double jian(double num1, double num2) ? ? ? ? { ? ? ? ? ? ? return num1- num2; ? ? ? ? } ? ? ? ? public double cheng(double num1, double num2) ? ? ? ? { ? ? ? ? ? ? return num1 * num2; ? ? ? ? } ? ? ? ? public double chu(double num1, double num2) ? ? ? ? { ? ? ? ? ? ? double result = 0; ? ? ? ? ? ? if (num2!=0) ? ? ? ? ? ? { ? ? ? ? ? ? result= num1 / num2; ? ? ? ? ? ? } ? ? ? ? ? ? return result; ? ? ? ? } ? ? ? ? public double tempnum = 0; ? ? ? ? public string caozuofu = ""; ? ? ? ? public event Operater fangfa; ? ? ? ? private void btnresult_Click(object sender, EventArgs e) ? ? ? ? { ? ? ? ? ? ? txtnum.Text = jisuan(caozuofu, fangfa).ToString(); ? ? ? ? } ? ? ? ? private void btnadd_Click(object sender, EventArgs e) ? ? ? ? { ? ? ? ? ? ? tempnum = double.Parse(txtnum.Text); ? ? ? ? ? ? caozuofu = btnadd.Text; ? ? ? ? ? ? txtnum.Text = ""; ? ? ? ? ? ? fangfa = add; ? ? ? ? } ? ? ? ? private void btnde_Click(object sender, EventArgs e) ? ? ? ? { ? ? ? ? ? ? tempnum = double.Parse(txtnum.Text); ? ? ? ? ? ? caozuofu = btnde.Text; ? ? ? ? ? ? txtnum.Text = ""; ? ? ? ? ? ? fangfa = jian; ? ? ? ? } ? ? ? ? private void btncheng_Click(object sender, EventArgs e) ? ? ? ? { ? ? ? ? ? ? tempnum = double.Parse(txtnum.Text); ? ? ? ? ? ? caozuofu = btncheng.Text; ? ? ? ? ? ? txtnum.Text = ""; ? ? ? ? ? ? fangfa = cheng; ? ? ? ? } ? ? ? ? private void btnchu_Click(object sender, EventArgs e) ? ? ? ? { ? ? ? ? ? ? tempnum = double.Parse(txtnum.Text); ? ? ? ? ? ? caozuofu = btnchu.Text; ? ? ? ? ? ? txtnum.Text = ""; ? ? ? ? ? ? fangfa = chu; ? ? ? ? } ? ? ? ? private void btndian_Click(object sender, EventArgs e) ? ? ? ? { ? ? ? ? ? ? if (txtnum.Text.ToString()=="")? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? txtnum.Text = "0"; ? ? ? ? ? ? } ? ? ? ? ? ? string temp=""; ? ? ? ? ? ? if (txtnum.Text.ToString().IndexOf(".") > 0)//解決只能包含一個小數點 ? ? ? ? ? ? { ? ? ? ? ? ? ? ? temp = txtnum.Text.ToString(); ? ? ? ? ? ? } ? ? ? ? ? ? else? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? temp = txtnum.Text.ToString() + btndian.Text;//這樣解決了重復點擊賦值問題 ? ? ? ? ? ? } ? ? ? ? ? ? txtnum.Text = temp; ? ? ? ? } ? ? } }
初始界面:
運行后的界面:
幾個數字按鈕是動態生成的,這就是我想要做的計算器。
原文鏈接:https://www.cnblogs.com/xiaohuasan/p/5624426.html
相關推薦
- 2022-02-26 docker 分布式 lnmp 鏡像制作
- 2022-04-23 通過自定義指令實現 element-ui的tooltip組件 文本長度超出顯示不超出不顯示
- 2023-01-14 python與matlab一些常用函數互轉問題_python
- 2024-03-17 樹莓派無桌面配置WiFi連接
- 2021-11-22 C++?STL中五個常用算法使用教程及實例講解_C 語言
- 2022-06-24 Android實現按鈕點擊事件的三種方法總結_Android
- 2022-04-02 C#實現NPOI的Excel導出詳解_C#教程
- 2022-02-11 Android之Compose頁面切換動畫介紹_Android
- 最近更新
-
- 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同步修改后的遠程分支