C#實(shí)現(xiàn)簡(jiǎn)單的計(jì)算器功能(窗體)
更新時(shí)間:2022年01月31日 13:03:16 作者:DC_prime
這篇文章主要為大家詳細(xì)介紹了C#實(shí)現(xiàn)簡(jiǎn)單的計(jì)算器功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
本文實(shí)例為大家分享了C#實(shí)現(xiàn)簡(jiǎ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;//輸入第一個(gè)參與運(yùn)算的數(shù);字符串的鏈接個(gè)十百千
? ? ? ? ? ? }
? ? ? ? ? ? else
? ? ? ? ? ? {
? ? ? ? ? ? ? ? num2 = num2 + button.Text;//輸入第二個(gè)參與運(yùn)算的數(shù);字符串的鏈接個(gè)十百千
? ? ? ? ? ? }
? ? ? ? ? ? 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("此時(shí)不應(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)行兩個(gè)數(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)擊效果一致時(shí),可使用同一個(gè)Click事件(名字一致即可)
//僅作舉例使用 //關(guān)鍵代碼 Button button = (Button)sender; //此時(shí)字符串的鏈接 num1 = num1 + button.Text;//輸入第一個(gè)參與運(yùn)算的數(shù);字符串的鏈接個(gè)十百千
代碼不足之處
僅供兩個(gè)操作數(shù)的運(yùn)算使用,新加操作數(shù)比較麻煩
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
您可能感興趣的文章:
- C#計(jì)算器編寫代碼
- C#編寫的windows計(jì)算器的實(shí)例代碼
- C#開發(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#入門之實(shí)現(xiàn)簡(jiǎn)易存款利息計(jì)算器示例
- C# WinForm程序設(shè)計(jì)簡(jiǎn)單計(jì)算器
相關(guān)文章
Dynamic和Var的區(qū)別及dynamic使用詳解
C#中的很多關(guān)鍵詞用法比較容易混淆,var和dynamic就是其中一組,他們都可以申明動(dòng)態(tài)類型的變量,但是本質(zhì)上他們還是有不少區(qū)別的,下面通過本文給大家介紹Dynamic和Var的區(qū)別及如何正確使用dynamic,需要的朋友參考下2016-01-01
C#使用委托實(shí)現(xiàn)的快速排序算法實(shí)例
這篇文章主要介紹了C#使用委托實(shí)現(xiàn)的快速排序算法,實(shí)例分析了C#委托機(jī)制與快速排序算法的實(shí)現(xiàn)技巧,需要的朋友可以參考下2015-07-07

