日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

C#實現簡單的計算器功能(窗體)_C#教程

作者:DC_prime ? 更新時間: 2022-04-11 編程語言

本文實例為大家分享了C#實現簡單的計算器功能的具體代碼,供大家參考,具體內容如下

1.界面設計

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;//計算器的操作數,成員變量
? ? ? ? private string opr;//操作符
? ? ? ? public Form1()
? ? ? ? {
? ? ? ? ? ? InitializeComponent();
? ? ? ? }
? ? ? ? //數字按鈕點擊事件的方法
? ? ? ? private void NumClick(object sender, EventArgs e)
? ? ? ? {
? ? ? ? ? ? Button button = (Button)sender;
? ? ? ? ? ? if (string.IsNullOrEmpty(opr))//如果還沒有輸入操作符
? ? ? ? ? ? {
? ? ? ? ? ? ? ? num1 = num1 + button.Text;//輸入第一個參與運算的數;字符串的鏈接個十百千
? ? ? ? ? ? }
? ? ? ? ? ? else
? ? ? ? ? ? {
? ? ? ? ? ? ? ? num2 = num2 + button.Text;//輸入第二個參與運算的數;字符串的鏈接個十百千
? ? ? ? ? ? }
? ? ? ? ? ? txtResult.Text = txtResult.Text + button.Text;
? ? ? ? }
? ? ? ? //操作符按鈕點擊事件的方法
? ? ? ? private void oprClick(object sender, EventArgs e)
? ? ? ? {
? ? ? ? ? ? Button button=(Button)sender;
? ? ? ? ? ? if (String.IsNullOrEmpty(num2))//如果還沒有輸入數字,則不允許按操作符
? ? ? ? ? ? {
? ? ? ? ? ? ? ? MessageBox.Show("此時不應該按入操作符!");
? ? ? ? ? ? ? ? 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("您輸入的內容有誤!");
? ? ? ? ? ? ? ? return;
? ? ? ? ? ? }
? ? ? ? ??
? ? ? ? ? ??
? ? ? ? ? ? ? ? txtResult.Text = txtResult.Text + "=";//將“=”拼接到框框里
? ? ? ? ? ? //進行兩個數的運算
? ? ? ? ? ? ? ? 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("除數不可以為零!");
? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? ? ? 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.總結分析

按鈕點擊事件:當多數按鈕的點擊效果一致時,可使用同一個Click事件(名字一致即可)

//僅作舉例使用
//關鍵代碼
Button button = (Button)sender;
//此時字符串的鏈接
num1 = num1 + button.Text;//輸入第一個參與運算的數;字符串的鏈接個十百千

代碼不足之處

僅供兩個操作數的運算使用,新加操作數比較麻煩

原文鏈接:https://blog.csdn.net/draumur_/article/details/115420129

欄目分類
最近更新