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

學無先后,達者為師

網站首頁 編程語言 正文

C#實現簡單的計算器功能_C#教程

作者:夏灣 ? 更新時間: 2022-04-09 編程語言

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

環境:VS2010及以上版本

1、建立個Window窗體應用

2、在工具箱里拖出兩個TextBox,第一個放上面,第二個放下面 。主要這里的Name,上面是textBox1,下面是textBox2。這涉及到后面代碼的書寫

3、在工具欄里拖動Button,擺放好??衫蒙厦娴膶R工具輔助設計。

4、在屬性里改變各Button的Text,如下

注意這里的1~9,小數點,±*/ 的Text應只有一個字符,不要多輸?!?/p>

5、選中任意一個Button,右鍵,選擇查看代碼,轉到Form1.cs

6、開始寫代碼

AddNum 修改TextBox的Text,應用于1~9與小數點的Click事件
Reset 重置temp、myoperator,以及兩個TextBox的Text
Delete 刪除textBox2的Text最后一個字符
Calculate 把textBox2的Text轉為double給temp,修改myoperator
Equal 具體的計算

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 WindowsFormsApp1
{
? ? public partial class Form1 : Form
? ? {
? ? ? ? public Form1()
? ? ? ? {
? ? ? ? ? ? InitializeComponent();
? ? ? ? }
//----上面是自動生成的代碼,下面得我們手寫----
? ? ? ? private double temp = 0; ?//存儲臨時數據
? ? ? ? private char myoperator = ' '; ?//判斷之前按的是+-*/中的哪個

? ? ? ? private void AddNum(object sender, EventArgs e)
? ? ? ? { ? // 1~9與小數點的Click事件
? ? ? ? ? ? //sender是引發該事件的控件,這里我們拆箱為Button
? ? ? ? ? ? Button button = (Button)sender;
? ? ? ? ? ? textBox2.Text += button.Text;
? ? ? ? }

? ? ? ? private void Reset(object sender, EventArgs e)
? ? ? ? { ? // CE的Click事件
? ? ? ? ? ? temp = 0;
? ? ? ? ? ? myoperator = ' ';
? ? ? ? ? ? textBox1.Text = textBox2.Text = "";
? ? ? ? }

? ? ? ? private void Delete(object sender, EventArgs e)
? ? ? ? { ? // ←的Click事件
? ? ? ? ? ? //移除最后一個字符
? ? ? ? ? ? if (textBox2.TextLength > 0)
? ? ? ? ? ? ? ? textBox2.Text = textBox2.Text.Remove(textBox2.TextLength - 1);
? ? ? ? }

? ? ? ? private void Calculate(object sender, EventArgs e)
? ? ? ? { ? // +-*/的Click事件
? ? ? ? ? ? Button button = (Button)sender;
? ? ? ? ? ? if (double.TryParse(textBox2.Text, out temp)) ?//嘗試把textBox2的Text轉為double并賦值給temp
? ? ? ? ? ? {
? ? ? ? ? ? ? ? myoperator = button.Text[0]; //Text是string,取第一個字符
? ? ? ? ? ? ? ? textBox1.Text = temp.ToString() + ' ' + myoperator;
? ? ? ? ? ? ? ? textBox2.Text = "";
? ? ? ? ? ? }
? ? ? ? ? ? else
? ? ? ? ? ? { ? //轉換失敗,重置所有
? ? ? ? ? ? ? ? Reset(sender, e);
? ? ? ? ? ? }
? ? ? ? }

? ? ? ? private void Equal(object sender, EventArgs e)
? ? ? ? { ? // = 的Click事件,計算并顯示
? ? ? ? ? ? double temp2;
? ? ? ? ? ? //嘗試轉換,失敗則重置并返回
? ? ? ? ? ? if (!double.TryParse(textBox2.Text, out temp2)) { Reset(sender, e); return; }
? ? ? ? ? ? switch (myoperator)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? case '+':
? ? ? ? ? ? ? ? ? ? temp += temp2;
? ? ? ? ? ? ? ? ? ? break;

? ? ? ? ? ? ? ? case '-':
? ? ? ? ? ? ? ? ? ? temp -= temp2;
? ? ? ? ? ? ? ? ? ? break;

? ? ? ? ? ? ? ? case '*':
? ? ? ? ? ? ? ? ? ? temp *= temp2;
? ? ? ? ? ? ? ? ? ? break;

? ? ? ? ? ? ? ? case '/':
? ? ? ? ? ? ? ? ? ? temp /= temp2;
? ? ? ? ? ? ? ? ? ? break;

? ? ? ? ? ? ? ? default:
? ? ? ? ? ? ? ? ? ? break;
? ? ? ? ? ? }
? ? ? ? ? ? textBox1.Text = "";
? ? ? ? ? ? textBox2.Text = temp.ToString();
? ? ? ? }
? ? }
}

7、設置各Button的Click事件

AddNum: 1~9與小數點的Click事件
Reset:CE的Click事件
Delete:←的Click事件
Calculate :±*/的Click事件
Equal:= 的Click事件

8、啟動(F5)

原文鏈接:https://blog.csdn.net/weixin_46068322/article/details/114920031

欄目分類
最近更新