C#實現(xiàn)計算器窗體程序
本文實例為大家分享了C#實現(xiàn)計算器窗體程序的具體代碼,供大家參考,具體內(nèi)容如下
功能設(shè)計
1、計算器中,添加 0-9 共十個數(shù)字鍵。
2、計算器中,增添 加、減、乘、除、等于五個功能鍵。
3、計算器中,增加四個功能鍵:x2,sqrt,log, ln 四個鍵,分別計算求平方,開方。
實現(xiàn)代碼
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 test3_1 { ? ? public partial class Form1 : Form ? ? { ? ? ? ? double result = 0; ? ? ? ? ? ? ?//存儲計算結(jié)果 ? ? ? ? double number = 0; ? ? ? ? ? ? ?//存儲輸入的數(shù)字 ? ? ? ? bool exist_value = false; ? ? ? //判斷文本框中是否有值 ? ? ? ? string operation; ? ? ? ? ? ? ? //存儲輸入的運算符 ? ? ? ? /* ? ? ? ? ?* 初始化 ? ? ? ? ?*/ ? ? ? ? public Form1() ? ? ? ? { ? ? ? ? ? ? InitializeComponent(); ? ? ? ? } ? ? ? ? /* ? ? ? ? ?* 數(shù)字鍵觸發(fā)事件實現(xiàn) ? ? ? ? ?*/ ? ? ? ? private void Seven_Click(object sender, EventArgs e) ? ? ? ? { ? ? ? ? ? ? if (exist_value == true) ? ? ? ? ? ? { ? ? ? ? ? ? ? ? textBox1.Text = ""; ? ? ? ? ? ? ? ? exist_value = false; ? ? ? ? ? ? } ? ? ? ? ? ? textBox1.Text += "7"; ? ? ? ? } ? ? ? ? private void Eight_Click(object sender, EventArgs e) ? ? ? ? { ? ? ? ? ? ? if (exist_value == true) ? ? ? ? ? ? { ? ? ? ? ? ? ? ? textBox1.Text = ""; ? ? ? ? ? ? ? ? exist_value = false; ? ? ? ? ? ? } ? ? ? ? ? ? textBox1.Text += "8"; ? ? ? ? } ? ? ? ? private void Nine_Click(object sender, EventArgs e) ? ? ? ? { ? ? ? ? ? ? if (exist_value == true) ? ? ? ? ? ? { ? ? ? ? ? ? ? ? textBox1.Text = ""; ? ? ? ? ? ? ? ? exist_value = false; ? ? ? ? ? ? } ? ? ? ? ? ? textBox1.Text += "9"; ? ? ? ? } ? ? ? ? private void Four_Click(object sender, EventArgs e) ? ? ? ? { ? ? ? ? ? ? if (exist_value == true) ? ? ? ? ? ? { ? ? ? ? ? ? ? ? textBox1.Text = ""; ? ? ? ? ? ? ? ? exist_value = false; ? ? ? ? ? ? } ? ? ? ? ? ? textBox1.Text += "4"; ? ? ? ? } ? ? ? ? private void Five_Click(object sender, EventArgs e) ? ? ? ? { ? ? ? ? ? ? if (exist_value == true) ? ? ? ? ? ? { ? ? ? ? ? ? ? ? textBox1.Text = ""; ? ? ? ? ? ? ? ? exist_value = false; ? ? ? ? ? ? } ? ? ? ? ? ? textBox1.Text += "5"; ? ? ? ? } ? ? ? ? private void Six_Click(object sender, EventArgs e) ? ? ? ? { ? ? ? ? ? ? if (exist_value == true) ? ? ? ? ? ? { ? ? ? ? ? ? ? ? textBox1.Text = ""; ? ? ? ? ? ? ? ? exist_value = false; ? ? ? ? ? ? } ? ? ? ? ? ? textBox1.Text += "6"; ? ? ? ? } ? ? ? ? private void One_Click(object sender, EventArgs e) ? ? ? ? { ? ? ? ? ? ? if (exist_value == true) ? ? ? ? ? ? { ? ? ? ? ? ? ? ? textBox1.Text = ""; ? ? ? ? ? ? ? ? exist_value = false; ? ? ? ? ? ? } ? ? ? ? ? ? textBox1.Text += "1"; ? ? ? ? } ? ? ? ? private void Two_Click(object sender, EventArgs e) ? ? ? ? { ? ? ? ? ? ? if (exist_value == true) ? ? ? ? ? ? { ? ? ? ? ? ? ? ? textBox1.Text = ""; ? ? ? ? ? ? ? ? exist_value = false; ? ? ? ? ? ? } ? ? ? ? ? ? textBox1.Text += "2"; ? ? ? ? } ? ? ? ? private void Three_Click(object sender, EventArgs e) ? ? ? ? { ? ? ? ? ? ? if (exist_value == true) ? ? ? ? ? ? { ? ? ? ? ? ? ? ? textBox1.Text = ""; ? ? ? ? ? ? ? ? exist_value = false; ? ? ? ? ? ? } ? ? ? ? ? ? textBox1.Text += "3"; ? ? ? ? } ? ? ? ? private void Zero_Click(object sender, EventArgs e) ? ? ? ? { ? ? ? ? ? ? if (exist_value == true) ? ? ? ? ? ? { ? ? ? ? ? ? ? ? textBox1.Text = ""; ? ? ? ? ? ? ? ? exist_value = false; ? ? ? ? ? ? } ? ? ? ? ? ? textBox1.Text += "0"; ? ? ? ? } ? ? ? ? /* ? ? ? ? ?* 功能鍵觸發(fā)事件 ? ? ? ? ?*/ ? ? ? ? private void Add_Click(object sender, EventArgs e) ? ? ? ? { ? ? ? ? ? ? if (textBox1.Text == "") ? ? ? ? ? ? { ? ? ? ? ? ? ? ? MessageBox.Show("請先輸入值再計算!", "錯誤提示", MessageBoxButtons.OK, MessageBoxIcon.Warning); ? ? ? ? ? ? } ? ? ? ? ? ? else ? ? ? ? ? ? { ? ? ? ? ? ? ? ? exist_value = true; ? ? ? ? ? ? ? ? number = double.Parse(textBox1.Text); ? ? ? ? ? ? ? ? operation = "+"; ? ? ? ? ? ? } ? ? ? ? } ? ? ? ? private void Sub_Click(object sender, EventArgs e) ? ? ? ? { ? ? ? ? ? ? if (textBox1.Text == "") ? ? ? ? ? ? { ? ? ? ? ? ? ? ? MessageBox.Show("請先輸入值再計算!", "錯誤提示", MessageBoxButtons.OK, MessageBoxIcon.Warning); ? ? ? ? ? ? } ? ? ? ? ? ? else ? ? ? ? ? ? { ? ? ? ? ? ? ? ? exist_value = true; ? ? ? ? ? ? ? ? number = double.Parse(textBox1.Text); ? ? ? ? ? ? ? ? operation = "-"; ? ? ? ? ? ? } ? ? ? ? } ? ? ? ? private void Mul_Click(object sender, EventArgs e) ? ? ? ? { ? ? ? ? ? ? if (textBox1.Text == "") ? ? ? ? ? ? { ? ? ? ? ? ? ? ? MessageBox.Show("請先輸入值再計算!", "錯誤提示", MessageBoxButtons.OK, MessageBoxIcon.Warning); ? ? ? ? ? ? } ? ? ? ? ? ? else ? ? ? ? ? ? { ? ? ? ? ? ? ? ? exist_value = true; ? ? ? ? ? ? ? ? number = double.Parse(textBox1.Text); ? ? ? ? ? ? ? ? operation = "*"; ? ? ? ? ? ? } ? ? ? ? } ? ? ? ? private void Div_Click(object sender, EventArgs e) ? ? ? ? { ? ? ? ? ? ? if (textBox1.Text == "") ? ? ? ? ? ? { ? ? ? ? ? ? ? ? MessageBox.Show("請先輸入值再計算!", "錯誤提示", MessageBoxButtons.OK, MessageBoxIcon.Warning); ? ? ? ? ? ? } ? ? ? ? ? ? else ? ? ? ? ? ? { ? ? ? ? ? ? ? ? exist_value = true; ? ? ? ? ? ? ? ? number = double.Parse(textBox1.Text); ? ? ? ? ? ? ? ? operation = "/"; ? ? ? ? ? ? } ? ? ? ? } ? ? ? ? private void Squ_Click(object sender, EventArgs e) ? ? ? ? { ? ? ? ? ? ? if (textBox1.Text == "") ? ? ? ? ? ? { ? ? ? ? ? ? ? ? MessageBox.Show("請先輸入值再計算!", "錯誤提示", MessageBoxButtons.OK, MessageBoxIcon.Warning); ? ? ? ? ? ? } ? ? ? ? ? ? else ? ? ? ? ? ? { ? ? ? ? ? ? ? ? exist_value = true; ? ? ? ? ? ? ? ? number = double.Parse(textBox1.Text); ? ? ? ? ? ? ? ? operation = "x^2"; ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? ? ? } ? ? ? ? private void Sqrt_Click(object sender, EventArgs e) ? ? ? ? { ? ? ? ? ? ? if (textBox1.Text == "") ? ? ? ? ? ? { ? ? ? ? ? ? ? ? MessageBox.Show("請先輸入值再計算!", "錯誤提示", MessageBoxButtons.OK, MessageBoxIcon.Warning); ? ? ? ? ? ? } ? ? ? ? ? ? else ? ? ? ? ? ? { ? ? ? ? ? ? ? ? exist_value = true; ? ? ? ? ? ? ? ? number = double.Parse(textBox1.Text); ? ? ? ? ? ? ? ? operation = "sqrt"; ? ? ? ? ? ? } ? ? ? ? } ? ? ? ? private void Log_Click(object sender, EventArgs e) ? ? ? ? { ? ? ? ? ? ? if (textBox1.Text == "") ? ? ? ? ? ? { ? ? ? ? ? ? ? ? MessageBox.Show("請先輸入值再計算!", "錯誤提示", MessageBoxButtons.OK, MessageBoxIcon.Warning); ? ? ? ? ? ? } ? ? ? ? ? ? else ? ? ? ? ? ? { ? ? ? ? ? ? ? ? exist_value = true; ? ? ? ? ? ? ? ? number = double.Parse(textBox1.Text); ? ? ? ? ? ? ? ? operation = "log"; ? ? ? ? ? ? } ? ? ? ? } ? ? ? ? private void Ln_Click(object sender, EventArgs e) ? ? ? ? { ? ? ? ? ? ? if (textBox1.Text == "") ? ? ? ? ? ? { ? ? ? ? ? ? ? ? MessageBox.Show("請先輸入值再計算!", "錯誤提示", MessageBoxButtons.OK, MessageBoxIcon.Warning); ? ? ? ? ? ? } ? ? ? ? ? ? else ? ? ? ? ? ? { ? ? ? ? ? ? ? ? exist_value = true; ? ? ? ? ? ? ? ? number = double.Parse(textBox1.Text); ? ? ? ? ? ? ? ? operation = "ln"; ? ? ? ? ? ? } ? ? ? ? } ? ? ? ? private void Del_Click(object sender, EventArgs e) ? ? ? ? { ? ? ? ? ? ? textBox1.Text = ""; ? ? ? ? } ? ? ? ? private void Equ_Click(object sender, EventArgs e) ? ? ? ? { ? ? ? ? ? ? switch (operation) ? ? ? ? ? ? { ? ? ? ? ? ? ? ? case "+": result = number + double.Parse(textBox1.Text); break; ? ? ? ? ? ? ? ? case "-": result = number - double.Parse(textBox1.Text); break; ? ? ? ? ? ? ? ? case "*": result = number * double.Parse(textBox1.Text); break; ? ? ? ? ? ? ? ? case "/": ? ? ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? ? ? double temp=double.Parse(textBox1.Text); ? ? ? ? ? ? ? ? ? ? ? ? if (temp != 0) ? ? ? ? ? ? ? ? ? ? ? ? ? ? result = number / temp; ? ? ? ? ? ? ? ? ? ? ? ? else ? ? ? ? ? ? ? ? ? ? ? ? ? ? MessageBox.Show("除數(shù)不能為零", "錯誤提示", MessageBoxButtons.OK, MessageBoxIcon.Warning); ? ? ? ? ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? case "x^2": result = number * number; break; ? ? ? ? ? ? ? ? case "sqrt": result = Math.Sqrt(number); break; ? ? ? ? ? ? ? ? case "log": result = Math.Log10(number); break; ? ? ? ? ? ? ? ? case "ln": result = Math.Log(number); break; ? ? ? ? ? ? } ? ? ? ? ? ? textBox1.Text = result + ""; ? ? ? ? ? ? exist_value = true; ? ? ? ? } ? ? } }
界面設(shè)計
運行結(jié)果
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
C#使用Windows Service的簡單教程(創(chuàng)建、安裝、卸載、調(diào)試)
這篇文章主要為大家詳細介紹了C#創(chuàng)建、安裝、卸載、調(diào)試Windows Service(Windows 服務(wù))的簡單教程,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-01-01C#對WPF數(shù)據(jù)綁定的菜單插入Seperator分隔
這篇文章介紹了C#對WPF數(shù)據(jù)綁定的菜單插入Seperator分隔的方法,文中通過示例代碼介紹的非常詳細。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-06-06C#根據(jù)http和ftp圖片地址獲取對應(yīng)圖片
這篇文章主要為大家詳細介紹了C#根據(jù)http和ftp圖片地址獲取對應(yīng)圖片,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-06-06