C#實(shí)現(xiàn)簡單的計(jì)算器功能(窗體)
本文實(shí)例為大家分享了C#實(shí)現(xiàn)簡單的計(jì)算器功能的具體代碼,供大家參考,具體內(nèi)容如下
1.界面設(shè)計(jì)
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;//計(jì)算器的操作數(shù),成員變量 ? ? ? ? private string opr;//操作符 ? ? ? ? public Form1() ? ? ? ? { ? ? ? ? ? ? InitializeComponent(); ? ? ? ? } ? ? ? ? //數(shù)字按鈕點(diǎn)擊事件的方法 ? ? ? ? private void NumClick(object sender, EventArgs e) ? ? ? ? { ? ? ? ? ? ? Button button = (Button)sender; ? ? ? ? ? ? if (string.IsNullOrEmpty(opr))//如果還沒有輸入操作符 ? ? ? ? ? ? { ? ? ? ? ? ? ? ? num1 = num1 + button.Text;//輸入第一個參與運(yùn)算的數(shù);字符串的鏈接個十百千 ? ? ? ? ? ? } ? ? ? ? ? ? else ? ? ? ? ? ? { ? ? ? ? ? ? ? ? num2 = num2 + button.Text;//輸入第二個參與運(yùn)算的數(shù);字符串的鏈接個十百千 ? ? ? ? ? ? } ? ? ? ? ? ? txtResult.Text = txtResult.Text + button.Text; ? ? ? ? } ? ? ? ? //操作符按鈕點(diǎn)擊事件的方法 ? ? ? ? private void oprClick(object sender, EventArgs e) ? ? ? ? { ? ? ? ? ? ? Button button=(Button)sender; ? ? ? ? ? ? if (String.IsNullOrEmpty(num2))//如果還沒有輸入數(shù)字,則不允許按操作符 ? ? ? ? ? ? { ? ? ? ? ? ? ? ? MessageBox.Show("此時不應(yīng)該按入操作符!"); ? ? ? ? ? ? ? ? return; ? ? ? ? ? ? } ? ? ? ? ? ? opr = button.Text; ? ? ? ? ? ? txtResult.Text = txtResult.Text + button.Text; ? ? ? ? } ? ? ? ? //“=”事件,即計(jì)算 ? ? ? ? private void btnGet_Click(object sender, EventArgs e) ? ? ? ? { ? ? ? ? ? ? if (String.IsNullOrEmpty(opr) ? ? ? ? ? ? ? ? || String.IsNullOrEmpty(num1) ? ? ? ? ? ? ? ? || String.IsNullOrEmpty(num2)) ? ? ? ? ? ? { ? ? ? ? ? ? ? ? MessageBox.Show("您輸入的內(nèi)容有誤!"); ? ? ? ? ? ? ? ? return; ? ? ? ? ? ? } ? ? ? ? ?? ? ? ? ? ? ?? ? ? ? ? ? ? ? ? txtResult.Text = txtResult.Text + "=";//將“=”拼接到框框里 ? ? ? ? ? ? //進(jìn)行兩個數(shù)的運(yùn)算 ? ? ? ? ? ? ? ? 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("除數(shù)不可以為零!"); ? ? ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? ? ? 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.總結(jié)分析
按鈕點(diǎn)擊事件:當(dāng)多數(shù)按鈕的點(diǎn)擊效果一致時,可使用同一個Click事件(名字一致即可)
//僅作舉例使用 //關(guān)鍵代碼 Button button = (Button)sender; //此時字符串的鏈接 num1 = num1 + button.Text;//輸入第一個參與運(yùn)算的數(shù);字符串的鏈接個十百千
代碼不足之處
僅供兩個操作數(shù)的運(yùn)算使用,新加操作數(shù)比較麻煩
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
C#使用SqlConnection連接到SQL Server的代碼示例
這篇文章主要介紹了C#使用SqlConnection連接到SQL Server的代碼示例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-03-03C# string格式的日期時間字符串轉(zhuǎn)為DateTime類型的方法
這篇文章主要介紹了C# string格式的日期時間字符串轉(zhuǎn)為DateTime類型的方法,需要的朋友可以參考下2017-02-02分享WCF聊天程序--WCFChat實(shí)現(xiàn)代碼
無意中在一個國外的站點(diǎn)下到了一個利用WCF實(shí)現(xiàn)聊天的程序,作者是:Nikola Paljetak。研究了一下,自己做了測試和部分修改,感覺還不錯,分享給大家2015-11-11C#實(shí)現(xiàn)隨鼠標(biāo)移動窗體實(shí)例
這篇文章主要介紹了C#實(shí)現(xiàn)隨鼠標(biāo)移動窗體實(shí)例,主要通過簡單的窗體事件代碼即可實(shí)現(xiàn)鼠標(biāo)隨窗體移動的功能,非常簡單實(shí)用,需要的朋友可以參考下2014-10-10C#實(shí)現(xiàn)HTTP訪問類HttpHelper的示例詳解
在項(xiàng)目開發(fā)過程中,我們經(jīng)常會訪問第三方接口,如我們需要接入的第三方接口是Web API,這時候我們就需要使用HttpHelper調(diào)用遠(yuǎn)程接口了。本文為大家介紹了C#實(shí)現(xiàn)HTTP訪問類HttpHelper的示例代碼,需要的可以參考一下2022-09-09