C#開發(fā)簡易winform計(jì)算器程序
臨近年關(guān),今日在學(xué)習(xí)的過程中感覺甚是無聊,便想用C#來開發(fā)一個(gè)簡易的計(jì)算器程序,這里記錄下今日下午的實(shí)現(xiàn)過程,同時(shí)也記錄下自己的第一遍博客。
一、需求
首先我們先來決定我們的計(jì)算器要實(shí)現(xiàn)什么功能
功能需求:1、能夠?qū)崿F(xiàn)加、減、乘、除、求余等兩個(gè)操作數(shù)的運(yùn)算,以及開方、平方單個(gè)操作數(shù)的運(yùn)算
2、能夠清除錯(cuò)誤的輸入,能夠?qū)崿F(xiàn)清零操作
顯示需求:能夠顯示操作數(shù)與運(yùn)算內(nèi)容,顯示結(jié)果
二、設(shè)計(jì)界面
1、在明白我們的功能需求后,我們來設(shè)計(jì)界面,界面主要包括三個(gè)部分,用于顯示的兩個(gè)textBox,以及數(shù)字鍵Button組,以及功能鍵Button組,對于具體的界面排列方法,以及Button的命名方式讀者可根據(jù)自己的喜好來設(shè)置,在這里我實(shí)現(xiàn)的界面如圖:
2、控件命名,主要是命名控件的Name屬性(表示控件的名字),以及Text屬性(表示控件上的顯示字體)
對于顯示區(qū)域命名為:textBox_top(顯示運(yùn)算過程),textBox_show(顯示結(jié)果)
對于功能區(qū)域命名為btn_name,name依次為(back,clear)表示清楚一位,和全部清零
對于操作數(shù)區(qū)域命名為btn_name,name依次為(9,8,7,6,5,4,3,2,1,0,point,PI),表式10個(gè)數(shù)字、小數(shù)點(diǎn)和常數(shù)PI
對于運(yùn)算符區(qū)域命名為btn_name,name依次為(add,sub,mult,div,square,involution,remain,equal)表示加、減、乘、除、平方、開方、求余和等號
三、實(shí)現(xiàn)邏輯
自己在沒做這個(gè)計(jì)算器程序之前,覺得這個(gè)程序很簡單,可是等做起來的時(shí)候發(fā)現(xiàn)不那么簡單,特別是這個(gè)邏輯,感覺很是繞,這里也提醒能夠看到本文的初學(xué)者一定不要眼高手低。其實(shí)這里的實(shí)現(xiàn)邏輯也并沒有那么復(fù)雜:首先,這里的運(yùn)算包括,兩個(gè)操作數(shù)的運(yùn)算,和一個(gè)操作數(shù)的運(yùn)算,明白這個(gè)后,我們實(shí)現(xiàn)起來就容易多了。
1、首先我們定義幾個(gè)變量來存儲操作數(shù)、運(yùn)算符和結(jié)果,firstValue(第一個(gè)操作數(shù)), secondValue(第二個(gè)操作數(shù)), result(結(jié)果);operation,存儲操作運(yùn)算符。
//定義操作數(shù)和結(jié)果 string firstValue, secondValue, result; char operation; //存儲上次點(diǎn)擊了什么按鈕,0代表什么都沒點(diǎn)擊,1代表了數(shù)字按鈕 private int lastButtonStatus = 0;
2、此處為了方便我們將點(diǎn)擊操作數(shù)的事件集中處理代碼如下:
private void btnVaL_Click(object sender, EventArgs e) { Button btn = (Button)sender; if (lastButtonStatus == 0 || textBox_show.Text == "0") { textBox_show.Text = btn.Text; } else { textBox_show.Text += btn.Text; } lastButtonStatus = 1; } //將數(shù)字按鈕的事件集中處理 private void Form1_Load(object sender, EventArgs e) { this.textBox_show.Text = "0"; this.textBox_top.Text = "0"; btn_0.Click += new EventHandler(btnVaL_Click); btn_1.Click += new EventHandler(btnVaL_Click); btn_2.Click += new EventHandler(btnVaL_Click); btn_3.Click += new EventHandler(btnVaL_Click); btn_4.Click += new EventHandler(btnVaL_Click); btn_5.Click += new EventHandler(btnVaL_Click); btn_6.Click += new EventHandler(btnVaL_Click); btn_7.Click += new EventHandler(btnVaL_Click); btn_8.Click += new EventHandler(btnVaL_Click); btn_9.Click += new EventHandler(btnVaL_Click); btn_point.Click += new EventHandler(btnVaL_Click); }
對于此方法,private void Form1_Load(object sender, EventArgs e)我們在窗體設(shè)計(jì)界面上雙擊,即可自動生成此方法,我們在里面添加相應(yīng)代碼即可,注意是窗口而不是具體的控件。
3、對于兩個(gè)操作數(shù),我們以運(yùn)算符是否按下后來劃分兩個(gè)操作數(shù),當(dāng)發(fā)生點(diǎn)擊運(yùn)算符(加、減、乘、除、求余)事件時(shí),我們將輸入框(textBox_show)中的數(shù)字存儲在firstValue變量中,并將“+”運(yùn)算符存儲在operation變量中,同時(shí)清空textBox_show中的內(nèi)容,并更新textBox_top中的內(nèi)容,然后繼續(xù)輸入第二個(gè)操作數(shù),當(dāng)按下等號運(yùn)算符時(shí),將輸入框中的數(shù)字存儲在secondValue變量中,并根據(jù)operation中的運(yùn)算符類型來計(jì)算。
4、對于一個(gè)操作數(shù)的運(yùn)算,在我們點(diǎn)擊運(yùn)算符(平方、開方)事件時(shí),直接根據(jù)運(yùn)算進(jìn)行計(jì)算,代碼如下:
//加 private void btn_add_Click(object sender, EventArgs e) { firstValue = textBox_show.Text; operation = '+'; textBox_top.Text = firstValue + '+'; textBox_show.Text = string.Empty; } //減 private void btn_sub_Click(object sender, EventArgs e) { firstValue = textBox_show.Text; operation = '-'; textBox_top.Text = firstValue + '-'; textBox_show.Text = string.Empty; } //乘 private void btn_mult_Click(object sender, EventArgs e) { firstValue = textBox_show.Text; operation = '*'; textBox_top.Text = firstValue + '*'; textBox_show.Text = string.Empty; } //除 private void btn_div_Click(object sender, EventArgs e) { firstValue = textBox_show.Text; operation = '/'; textBox_top.Text = firstValue + '/'; textBox_show.Text = string.Empty; } //求余 private void btn_remain_Click(object sender, EventArgs e) { firstValue = textBox_show.Text; operation = '%'; textBox_top.Text = firstValue + '%'; textBox_show.Text = string.Empty; } //求平方和 private void btn_square_Click(object sender, EventArgs e) { firstValue = textBox_show.Text; textBox_top.Text = firstValue + "的平方是:"; double outFirst; double.TryParse(firstValue, out outFirst); textBox_show.Text = (outFirst * outFirst).ToString(); } //求平方根 private void btn_involution_Click(object sender, EventArgs e) { firstValue = textBox_show.Text; textBox_top.Text = firstValue + "的平方根是:"; double outFirst; double.TryParse(firstValue, out outFirst); textBox_show.Text = (Math.Sqrt(outFirst)).ToString(); } //等號,處理加、減、乘、除、求余、運(yùn)算 private void btn_equal_Click(object sender, EventArgs e) { secondValue = textBox_show.Text; textBox_top.Text += secondValue + '='; double outFirst, outSecond; double.TryParse(firstValue, out outFirst); double.TryParse(secondValue,out outSecond); switch (operation) { case '+': result = (outFirst + outSecond).ToString(); break; case '-': result = (outFirst - outSecond).ToString(); break; case '*': result = (outFirst * outSecond).ToString(); break; case '/': if (outSecond != 0) { result = (outFirst / outSecond).ToString(); } else { MessageBox.Show("被除數(shù)不能為0"); } break; case '%': result = (outFirst % outSecond).ToString(); break; } textBox_show.Text = result; }
5、對于退格功能和清零功能,比較容易實(shí)現(xiàn),這里直接貼上代碼
//退格功能 private void btn_back_Click(object sender, EventArgs e) { if (textBox_show.Text.Length > 0) { textBox_show.Text = textBox_show.Text.Substring(0, textBox_show.Text.Length - 1); } } //清空 private void btn_clear_Click(object sender, EventArgs e) { textBox_top.Text = string.Empty; textBox_show.Text = string.Empty; firstValue = string.Empty; secondValue = string.Empty; }
四、實(shí)現(xiàn)結(jié)果
這樣我們就實(shí)現(xiàn)了一個(gè)簡易的計(jì)算器器程序,趕快來運(yùn)行以下來看看效果吧:
最后放上全部的實(shí)現(xiàn)代碼:CalculatorTest ,同時(shí)也感謝各位大神批評指正。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持我們。
上一篇:詳解C# 不能用于文件名的字符
欄 目:C#教程
本文標(biāo)題:C#開發(fā)簡易winform計(jì)算器程序
本文地址:http://mengdiqiu.com.cn/a1/C_jiaocheng/5274.html
您可能感興趣的文章
- 01-10WinForm限制窗體不能移到屏幕外的方法
- 01-10WinForm繪制圓角的方法
- 01-10WinForm實(shí)現(xiàn)仿視頻 器左下角滾動新聞效果的方法
- 01-10C#實(shí)現(xiàn)Winform中打開網(wǎng)頁頁面的方法
- 01-10winform實(shí)現(xiàn)創(chuàng)建最前端窗體的方法
- 01-10WinForm實(shí)現(xiàn)自定義右下角提示效果的方法
- 01-10.net2.0+ Winform項(xiàng)目實(shí)現(xiàn)彈出容器層
- 01-10winform 實(shí)現(xiàn)控制輸入法
- 01-10WinForm實(shí)現(xiàn)程序一段時(shí)間不運(yùn)行自動關(guān)閉的方法
- 01-10Winform消除button按下出現(xiàn)的虛線簡單實(shí)現(xiàn)方法


閱讀排行
本欄相關(guān)
- 01-10C#通過反射獲取當(dāng)前工程中所有窗體并
- 01-10關(guān)于ASP網(wǎng)頁無法打開的解決方案
- 01-10WinForm限制窗體不能移到屏幕外的方法
- 01-10WinForm繪制圓角的方法
- 01-10C#實(shí)現(xiàn)txt定位指定行完整實(shí)例
- 01-10WinForm實(shí)現(xiàn)仿視頻 器左下角滾動新
- 01-10C#停止線程的方法
- 01-10C#實(shí)現(xiàn)清空回收站的方法
- 01-10C#通過重寫Panel改變邊框顏色與寬度的
- 01-10C#實(shí)現(xiàn)讀取注冊表監(jiān)控當(dāng)前操作系統(tǒng)已
隨機(jī)閱讀
- 01-10使用C語言求解撲克牌的順子及n個(gè)骰子
- 01-11ajax實(shí)現(xiàn)頁面的局部加載
- 08-05織夢dedecms什么時(shí)候用欄目交叉功能?
- 08-05DEDE織夢data目錄下的sessions文件夾有什
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 04-02jquery與jsp,用jquery
- 01-10delphi制作wav文件的方法
- 01-10C#中split用法實(shí)例總結(jié)
- 08-05dedecms(織夢)副欄目數(shù)量限制代碼修改
- 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置