C#實現(xiàn)簡單的計算器功能(窗體)
更新時間:2022年01月31日 13:03:16 作者:DC_prime
這篇文章主要為大家詳細(xì)介紹了C#實現(xiàn)簡單的計算器功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了C#實現(xiàn)簡單的計算器功能的具體代碼,供大家參考,具體內(nèi)容如下
1.界面設(shè)計

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;//計算器的操作數(shù),成員變量
? ? ? ? private string opr;//操作符
? ? ? ? public Form1()
? ? ? ? {
? ? ? ? ? ? InitializeComponent();
? ? ? ? }
? ? ? ? //數(shù)字按鈕點擊事件的方法
? ? ? ? private void NumClick(object sender, EventArgs e)
? ? ? ? {
? ? ? ? ? ? Button button = (Button)sender;
? ? ? ? ? ? if (string.IsNullOrEmpty(opr))//如果還沒有輸入操作符
? ? ? ? ? ? {
? ? ? ? ? ? ? ? num1 = num1 + button.Text;//輸入第一個參與運算的數(shù);字符串的鏈接個十百千
? ? ? ? ? ? }
? ? ? ? ? ? else
? ? ? ? ? ? {
? ? ? ? ? ? ? ? num2 = num2 + button.Text;//輸入第二個參與運算的數(shù);字符串的鏈接個十百千
? ? ? ? ? ? }
? ? ? ? ? ? txtResult.Text = txtResult.Text + button.Text;
? ? ? ? }
? ? ? ? //操作符按鈕點擊事件的方法
? ? ? ? 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;
? ? ? ? }
? ? ? ? //“=”事件,即計算
? ? ? ? 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ù)的運算
? ? ? ? ? ? ? ? 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é)分析
按鈕點擊事件:當(dāng)多數(shù)按鈕的點擊效果一致時,可使用同一個Click事件(名字一致即可)
//僅作舉例使用 //關(guān)鍵代碼 Button button = (Button)sender; //此時字符串的鏈接 num1 = num1 + button.Text;//輸入第一個參與運算的數(shù);字符串的鏈接個十百千
代碼不足之處
僅供兩個操作數(shù)的運算使用,新加操作數(shù)比較麻煩
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Dynamic和Var的區(qū)別及dynamic使用詳解
C#中的很多關(guān)鍵詞用法比較容易混淆,var和dynamic就是其中一組,他們都可以申明動態(tài)類型的變量,但是本質(zhì)上他們還是有不少區(qū)別的,下面通過本文給大家介紹Dynamic和Var的區(qū)別及如何正確使用dynamic,需要的朋友參考下2016-01-01

