網(wǎng)站首頁 編程語言 正文
本文實(shí)例為大家分享了C#實(shí)現(xiàn)計(jì)算器精簡版的具體實(shí)現(xiàn)代碼,供大家參考,具體內(nèi)容如下
計(jì)算器需求分析
一、界面設(shè)計(jì)
1.做一個(gè)顯示屏
2.17個(gè)按鈕(0-9,±×÷%=,CE)
二、需要實(shí)現(xiàn)的功能
1.輸入第一個(gè)數(shù)字
2.選擇運(yùn)算類型
3.輸入第二個(gè)數(shù)字
4.按下等號(hào)計(jì)算出結(jié)果,結(jié)果顯示在顯示屏上
三、實(shí)現(xiàn)步驟
1.先做界面
a.顯示屏 textbox、listbox、label
b.使用17個(gè)button,button上的文本改成對(duì)應(yīng)的數(shù)字符號(hào)
2.補(bǔ)充:申請(qǐng)兩個(gè)int類型變量,第一個(gè)num1裝第一個(gè)數(shù)字
第二個(gè)num2裝第二個(gè)數(shù)字
(1).輸入第一個(gè)數(shù)字,當(dāng)點(diǎn)一個(gè)數(shù)字按鈕,屏幕上顯示一個(gè),之前顯示的數(shù)字在前面呆著
a1.添加按鈕的cilck事件
a2.事件觸發(fā),將按鈕代表的數(shù)字顯示textbox1的text
(2).當(dāng)輸入符號(hào)的時(shí)候,清除屏幕,但是后臺(tái)必須記錄好第一個(gè)數(shù)字
b1.添加符號(hào)按鈕的click事件
b2.當(dāng)點(diǎn)任何一個(gè)符號(hào)按鈕用一個(gè)變量num1裝剛才輸入的textbox1中的數(shù)字
(3).輸入第二個(gè)數(shù)字
c1. 當(dāng)點(diǎn)任何一個(gè)符號(hào)按鈕用一個(gè)變量num2裝剛才輸入的textbox1中的數(shù)字
(4).按下等號(hào)按鈕,顯示屏上面的文本改變成兩個(gè)數(shù)字的運(yùn)算結(jié)果
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 簡單的計(jì)算器制作 { ? ? public partial class Form1 : Form ? ? { ? ? ? ? public Form1() ? ? ? ? { ? ? ? ? ? ? InitializeComponent(); ? ? ? ? } ? ? ? ? private void Form1_Load(object sender, EventArgs e) ? ? ? ? { ? ? ? ? ? ? //計(jì)算窗口加載居中的位置 ? ? ? ? ? ? int left = Screen.PrimaryScreen.Bounds.Width / 2 - this.Width / 2; ? ? ? ? ? ? int top = Screen.PrimaryScreen.Bounds.Height / 2 - this.Height / 2; ? ? ? ? ? ? this.Location = new Point(left,top); ? ? ? ? ? ? //加載的時(shí)候獲取焦點(diǎn) ? ? ? ? ? ? button1.TabIndex = 0; ? ? ? ? } ? ? ? ? //當(dāng)我們輸入完第一個(gè)數(shù)字之后 ?在輸入運(yùn)算符的時(shí)候 我們要記下第一個(gè)數(shù)字num1 ? ? ? ? //當(dāng)我們輸入完第二個(gè)數(shù)字之后 ?在輸入等號(hào)的時(shí)候 我們要記下第二個(gè)數(shù)字num1 ? ? ? ? double num1 = 0; ? ? ? ? double num2 = 0; ? ? ? ? bool iskey = false; ? ? ? ? //ce ? ? ? ? private void button1_Click(object sender, EventArgs e) ? ? ? ? { ? ? ? ? ? ? //設(shè)置清空 ? ? ? ? ? ? textBox1.Text = "";? ? ? ? ? } ? ? ? ? ? ? ? ? //1 ? ? ? ? private void button4_Click(object sender, EventArgs e) ? ? ? ? { ? ? ? ? ? ? if (iskey) ? ? ? ? ? ? { ? ? ? ? ? ? ? ? textBox1.Text = ""; ? ? ? ? ? ? ? ? iskey = false; ? ? ? ? ? ? } ? ? ? ? ? ? textBox1.Text += "1"; ? ? ? ? } ? ? ? ? //2 ? ? ? ? private void button5_Click(object sender, EventArgs e) ? ? ? ? { ? ? ? ? ? ? if (iskey) ? ? ? ? ? ? { ? ? ? ? ? ? ? ? textBox1.Text = ""; ? ? ? ? ? ? ? ? iskey = false; ? ? ? ? ? ? } ? ? ? ? ? ? textBox1.Text += "2"; ? ? ? ? } ? ? ? ? //3 ? ? ? ? private void button6_Click(object sender, EventArgs e) ? ? ? ? { ? ? ? ? ? ? if (iskey) ? ? ? ? ? ? { ? ? ? ? ? ? ? ? textBox1.Text = ""; ? ? ? ? ? ? ? ? iskey = false; ? ? ? ? ? ? } ? ? ? ? ? ? textBox1.Text += "3"; ? ? ? ? } ? ? ? ? //4 ? ? ? ? private void button8_Click(object sender, EventArgs e) ? ? ? ? { ? ? ? ? ? ? if (iskey) ? ? ? ? ? ? { ? ? ? ? ? ? ? ? textBox1.Text = ""; ? ? ? ? ? ? ? ? iskey = false; ? ? ? ? ? ? } ? ? ? ? ? ? textBox1.Text += "4"; ? ? ? ? } ? ? ? ? //5 ? ? ? ? private void button9_Click(object sender, EventArgs e) ? ? ? ? { ? ? ? ? ? ? if (iskey) ? ? ? ? ? ? { ? ? ? ? ? ? ? ? textBox1.Text = ""; ? ? ? ? ? ? ? ? iskey = false; ? ? ? ? ? ? } ? ? ? ? ? ? textBox1.Text += "5"; ? ? ? ? } ? ? ? ? //6 ? ? ? ? private void button10_Click(object sender, EventArgs e) ? ? ? ? { ? ? ? ? ? ? if (iskey) ? ? ? ? ? ? { ? ? ? ? ? ? ? ? textBox1.Text = ""; ? ? ? ? ? ? ? ? iskey = false; ? ? ? ? ? ? } ? ? ? ? ? ? textBox1.Text += "6"; ? ? ? ? } ? ? ? ? //7 ? ? ? ? private void button12_Click(object sender, EventArgs e) ? ? ? ? { ? ? ? ? ? ? if (iskey) ? ? ? ? ? ? { ? ? ? ? ? ? ? ? textBox1.Text = ""; ? ? ? ? ? ? ? ? iskey = false; ? ? ? ? ? ? } ? ? ? ? ? ? textBox1.Text += "7"; ? ? ? ? } ? ? ? ? //8 ? ? ? ? private void button13_Click(object sender, EventArgs e) ? ? ? ? { ? ? ? ? ? ? if (iskey) ? ? ? ? ? ? { ? ? ? ? ? ? ? ? textBox1.Text = ""; ? ? ? ? ? ? ? ? iskey = false; ? ? ? ? ? ? } ? ? ? ? ? ? textBox1.Text += "8"; ? ? ? ? } ? ? ? ? //9 ? ? ? ? private void button14_Click(object sender, EventArgs e) ? ? ? ? { ? ? ? ? ? ? if (iskey) ? ? ? ? ? ? { ? ? ? ? ? ? ? ? textBox1.Text = ""; ? ? ? ? ? ? ? ? iskey = false; ? ? ? ? ? ? } ? ? ? ? ? ? textBox1.Text += "9"; ? ? ? ? } ? ? ? ? //0 ? ? ? ? private void button17_Click(object sender, EventArgs e) ? ? ? ? { ? ? ? ? ? ? if (iskey) ? ? ? ? ? ? { ? ? ? ? ? ? ? ? textBox1.Text = ""; ? ? ? ? ? ? ? ? iskey = false; ? ? ? ? ? ? } ? ? ? ? ? ? textBox1.Text += "9"; ? ? ? ? } ? ? ? ? //. ? ? ? ? private void button16_Click(object sender, EventArgs e) ? ? ? ? { ? ? ? ? ? ? if (iskey) ? ? ? ? ? ? { ? ? ? ? ? ? ? ? textBox1.Text = ""; ? ? ? ? ? ? ? ? iskey = false; ? ? ? ? ? ? } ? ? ? ? ? ? textBox1.Text += "."; ? ? ? ? } ? ? ? ? //定義一個(gè)空的來接收符號(hào) ? ? ? ? string type=" "; ? ? ? ? //+ ? ? ? ? private void button15_Click(object sender, EventArgs e) ? ? ? ? { ? ? ? ? ? ? if(textBox1.Text != "") ? ? ? ? ? ? { ? ? ? ? ? ? //獲取運(yùn)算的第一個(gè)數(shù)字(前一個(gè)數(shù)字);將字符串類型轉(zhuǎn)換為int類型(int.parse()) ? ? ? ? ? ? ?// num1 = int.Parse(textBox1.Text); ? ? ? ? ? ? // num1 = Convert.ToInt32(textBox1.Text); ? ? ? ? ? // ?第二種轉(zhuǎn)換方式convert ? ? ? ? num1 = Convert.ToDouble(textBox1.Text); ? ? ? ? ? ? } ? ? ? ? ? ? type = "+"; ? ? ? ? ? ? // ?textBox1.Text = ""; ? ? ? ? ? ? iskey = true; ? ? ? ? } ? ? ? ? //- ? ? ? ? private void button3_Click(object sender, EventArgs e) ? ? ? ? { ? ? ? ? ? ? if(textBox1.Text != ""){ ? ? ? ? ? ? ? ? num1 = Convert.ToDouble(textBox1.Text); ? ? ? ? ? ? } ? ? ? ? ? ? type = "-"; ? ? ? ? ? ? // textBox1.Text = ""; ? ? ? ? ? ? iskey = true; ? ? ? ? } ? ? ? ? //* ? ? ? ? private void button7_Click(object sender, EventArgs e) ? ? ? ? { ? ? ? ? ? ? if(textBox1.Text != "") ? ? ? ? ? ? { ? ? ? ? ? ? ? ? num1 = Convert.ToDouble(textBox1.Text); ? ? ? ? ? ? } ? ? ? ? ? ? type = "*"; ? ? ? ? ? ? // ?textBox1.Text = ""; ? ? ? ? ? ? iskey = true; ? ? ? ? } ? ? ? ? //÷ ? ? ? ? private void button11_Click(object sender, EventArgs e) ? ? ? ? { ? ? ? ? ? ? if(textBox1.Text != "") ? ? ? ? ? ? { ? ? ? ? ? ? ? ? num1 = Convert.ToDouble(textBox1.Text); ? ? ? ? ? ? } ? ? ? ? ? ? type = "/"; ? ? ? ? ? ? //textBox1.Text = ""; ? ? ? ? ? ? iskey = true; ? ? ? ? } ? ? ? ? //% ? ? ? ? private void button18_Click(object sender, EventArgs e) ? ? ? ? { ? ? ? ? ? ? iskey = true; ? ? ? ? ? ? if (textBox1.Text != "") ? ? ? ? ? ? { ? ? ? ? ? ? ? ? num1 = Convert.ToDouble(textBox1.Text); ? ? ? ? ? ? } ? ? ? ? ? ? type = "%"; ? ? ? ? ? ? //textBox1.Text = ""; ? ? ? ? } ? ? ? ? //= ? ? ? ? private void button2_Click(object sender, EventArgs e) ? ? ? ? { ? ? ? ? ? ? if (iskey) ? ? ? ? ? ? { ? ? ? ? ? ? ? ? return; ? ? ? ? ? ? } ? ? ? ? ? ? iskey = true; ? ? ? ? ? ? if(textBox1.Text != "") ? ? ? ? ? ? { ? ? ? ? ? ? ? ? num2 = Convert.ToDouble(textBox1.Text); ? ? ? ? ? ? } ? ? ? ? ? ? switch (type) ? ? ? ? ? ? { ? ? ? ? ? ? ? ? case ?"+": ? ? ? ? ? ? ? ? //括號(hào)里進(jìn)行計(jì)算,計(jì)算的結(jié)果轉(zhuǎn)化為string類型,并顯示在屏幕(textbox1)里; ? ? ? ? ? ? ? ? ? ? textBox1.Text = (num1 + num2).ToString(); ? ? ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? ? ? case "-": ? ? ? ? ? ? ? ? ? ? textBox1.Text = (num1 - num2).ToString(); ? ? ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? ? ? case "*": ? ? ? ? ? ? ? ? ? ? textBox1.Text = (num1 * num2).ToString(); ? ? ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? ? ? case "/": ? ? ? ? ? ? ? ? ? ? textBox1.Text = (num1 / num2).ToString(); ? ? ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? ? ? case "%": ? ? ? ? ? ? ? ? ? ? textBox1.Text = (num1 % num2).ToString(); ? ? ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? } ? ? ? ? } ? ? } }
原文鏈接:https://blog.csdn.net/weixin_44870681/article/details/90760613
相關(guān)推薦
- 2022-04-17 Spring Security前后端分離實(shí)現(xiàn)
- 2022-11-16 Python中Pygame模塊的詳細(xì)安裝過程_python
- 2022-05-28 python非單一.py文件用Pyinstaller打包發(fā)布成exe_python
- 2023-08-30 Git忽略已經(jīng)提交過一次文件Git忽略文件
- 2022-07-09 python如何獲取Prometheus監(jiān)控?cái)?shù)據(jù)_python
- 2022-04-19 C#中的類繼承詳解_C#教程
- 2023-01-14 Go語言Http調(diào)用之Post請(qǐng)求詳解_Golang
- 2022-04-14 Python之OptionParser模塊使用詳解_python
- 最近更新
-
- 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)程分支