C#實(shí)現(xiàn)計(jì)算器窗體程序
本文實(shí)例為大家分享了C#實(shí)現(xiàn)計(jì)算器窗體程序的具體代碼,供大家參考,具體內(nèi)容如下
功能設(shè)計(jì)
1、計(jì)算器中,添加 0-9 共十個(gè)數(shù)字鍵。
2、計(jì)算器中,增添 加、減、乘、除、等于五個(gè)功能鍵。
3、計(jì)算器中,增加四個(gè)功能鍵:x2,sqrt,log, ln 四個(gè)鍵,分別計(jì)算求平方,開(kāi)方。
實(shí)現(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; ? ? ? ? ? ? ?//存儲(chǔ)計(jì)算結(jié)果
? ? ? ? double number = 0; ? ? ? ? ? ? ?//存儲(chǔ)輸入的數(shù)字
? ? ? ? bool exist_value = false; ? ? ? //判斷文本框中是否有值
? ? ? ? string operation; ? ? ? ? ? ? ? //存儲(chǔ)輸入的運(yùn)算符
? ? ? ? /*
? ? ? ? ?* 初始化
? ? ? ? ?*/
? ? ? ? public Form1()
? ? ? ? {
? ? ? ? ? ? InitializeComponent();
? ? ? ? }
? ? ? ? /*
? ? ? ? ?* 數(shù)字鍵觸發(fā)事件實(shí)現(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("請(qǐng)先輸入值再計(jì)算!", "錯(cuò)誤提示", 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("請(qǐng)先輸入值再計(jì)算!", "錯(cuò)誤提示", 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("請(qǐng)先輸入值再計(jì)算!", "錯(cuò)誤提示", 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("請(qǐng)先輸入值再計(jì)算!", "錯(cuò)誤提示", 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("請(qǐng)先輸入值再計(jì)算!", "錯(cuò)誤提示", 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("請(qǐng)先輸入值再計(jì)算!", "錯(cuò)誤提示", 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("請(qǐng)先輸入值再計(jì)算!", "錯(cuò)誤提示", 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("請(qǐng)先輸入值再計(jì)算!", "錯(cuò)誤提示", 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ù)不能為零", "錯(cuò)誤提示", 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è)計(jì)

運(yùn)行結(jié)果

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- C#計(jì)算器編寫(xiě)代碼
- C#編寫(xiě)的windows計(jì)算器的實(shí)例代碼
- C#開(kāi)發(fā)簡(jiǎn)易winform計(jì)算器程序
- C#實(shí)現(xiàn)簡(jiǎn)單的計(jì)算器功能完整實(shí)例
- C#實(shí)現(xiàn)簡(jiǎn)單計(jì)算器功能
- C#實(shí)現(xiàn)簡(jiǎn)單加減乘除計(jì)算器
- C#實(shí)現(xiàn)Winform版計(jì)算器
- C#實(shí)現(xiàn)的簡(jiǎn)單整數(shù)四則運(yùn)算計(jì)算器功能示例
- c#入門(mén)之實(shí)現(xiàn)簡(jiǎn)易存款利息計(jì)算器示例
- C# WinForm程序設(shè)計(jì)簡(jiǎn)單計(jì)算器
相關(guān)文章
C# 如何使用OpcUaHelper讀寫(xiě)OPC服務(wù)器
這篇文章給大家介紹C# 如何使用OpcUaHelper讀寫(xiě)OPC服務(wù)器,本文通過(guò)圖文實(shí)例代碼相結(jié)合給大家介紹的非常詳細(xì),需要的朋友參考下吧2023-12-12
c#實(shí)現(xiàn)隱藏與顯示任務(wù)欄的方法詳解
本篇文章是對(duì)c#中任務(wù)欄隱藏與顯示的實(shí)現(xiàn)方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06
C#結(jié)合AForge實(shí)現(xiàn)攝像頭錄像
最近由于興趣學(xué)習(xí)了下在C#上使用AForge錄制攝像頭視頻并壓縮編碼??傮w上來(lái)說(shuō)這個(gè)第三方.net視覺(jué)開(kāi)發(fā)庫(kù)還是比較穩(wěn)定的2017-09-09
C#中DataTable 轉(zhuǎn)實(shí)體實(shí)例詳解
這篇文章主要介紹了C#中DataTable 轉(zhuǎn)實(shí)體實(shí)例詳解,需要的朋友可以參考下2017-04-04
C# Winform中如何繪制動(dòng)畫(huà)示例詳解
這篇文章主要給大家介紹了關(guān)于C# Winform中如何繪制動(dòng)畫(huà)的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用C# Winform具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-05-05
WPF中使用WebView2控件的方法及常見(jiàn)問(wèn)題
WebView2為WPF網(wǎng)頁(yè)瀏覽工具,具有簡(jiǎn)單易用,頁(yè)面顯示清晰的優(yōu)點(diǎn),下面這篇文章主要給大家介紹了關(guān)于WPF中使用WebView2控件的方法及常見(jiàn)問(wèn)題,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-02-02
C#與js實(shí)現(xiàn)去除textbox文本框里面重復(fù)記錄的方法
這篇文章主要介紹了C#與js實(shí)現(xiàn)去除textbox文本框里面重復(fù)記錄的方法,很實(shí)用的功能,需要的朋友可以參考下2014-08-08

