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

學無先后,達者為師

網站首頁 編程語言 正文

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

作者:我自是年少韶華傾負 ? 更新時間: 2022-04-10 編程語言

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

先來張效果圖吧(5分鐘寫好,莫怪)

代碼:

數字按鈕綁定的是button_Clickd()方法

運算符按鈕綁的是Button_Clickp()方法

思想:按下數字按鈕,將數字按鈕的值連接到textbox上,然后按下運算符判斷是否為等于,并記錄運算符的內容,以便后面做處理。

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 WindowsFormsApplication4
{
? ? public partial class Form2 : Form
? ? {
? ? ? ? private string s;
? ? ? ? private double x, y;
? ? ? ? private Button btn;
? ? ? ?
? ? ? ? public Form2()
? ? ? ? {
? ? ? ? ? ? InitializeComponent();
? ? ? ? }
?
?
? ? ? ? private void Form2_Load(object sender, EventArgs e)
? ? ? ? {
? ? ? ? ? ? textBox1.Text = "";
? ? ? ? ? ? label1.Text="";
?
? ? ? ? }
? ? ? ? private void buttond_Click(object sender, EventArgs e)//數字符所綁定的事件
? ? ? ? {
? ? ? ? ? ? btn = (Button)sender;
? ? ? ? ? ? textBox1.Text = textBox1.Text + btn.Text;//將所點擊的數字付呈現在textBox上面
?
? ? ? ? }
? ? ? ? private void buttonp_Click(object sender, EventArgs e)//運算符所綁定的事件
? ? ? ? {
? ? ? ? ? ? btn = (Button)sender;
? ? ? ? ? ? if (btn.Name != "button12")//如果不是"="
? ? ? ? ? ? {
? ? ? ? ? ? ? ? x = Convert.ToDouble(textBox1.Text);//將所所輸入的第一個字符保留下來
? ? ? ? ? ? ? ? textBox1.Text = "";//清空textBox的內容
? ? ? ? ? ? ? ? s = btn.Name;//獲取運算符的種類
? ? ? ? ? ? ? ? label1.Text = x.ToString();//將第一個所按的字符輸出來
?
?
? ? ? ? ? ? }
? ? ? ? ? ? else
? ? ? ? ? ? {
? ? ? ? ? ? ? ? if (label1.Text == "")
? ? ? ? ? ? ? ? ? ? MessageBox.Show("輸入不正確!!", "信息提示", MessageBoxButtons.OK);
? ? ? ? ? ? ? ? else
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? y = Convert.ToDouble(textBox1.Text);
? ? ? ? ? ? ? ? ? ? switch (s)//使用s來判讀所按的按鈕
? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? case "button13":
? ? ? ? ? ? ? ? ? ? ? ? ? ? textBox1.Text = (x + y).ToString();
? ? ? ? ? ? ? ? ? ? ? ? ? ? break;
? ? ? ? ? ? ? ? ? ? ? ? case "button14":
? ? ? ? ? ? ? ? ? ? ? ? ? ? textBox1.Text = (x - y).ToString();
? ? ? ? ? ? ? ? ? ? ? ? ? ? break;
? ? ? ? ? ? ? ? ? ? ? ? case "button15":
? ? ? ? ? ? ? ? ? ? ? ? ? ? textBox1.Text = (x * y).ToString();
? ? ? ? ? ? ? ? ? ? ? ? ? ? break;
?
? ? ? ? ? ? ? ? ? ? ? ? case "button16":
? ? ? ? ? ? ? ? ? ? ? ? ? ? if (y == 0)
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? MessageBox.Show("除零錯誤!!!", "信息提示", MessageBoxButtons.OK);
? ? ? ? ? ? ? ? ? ? ? ? ? ? else
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? textBox1.Text = (x / y).ToString();
?
?
? ? ? ? ? ? ? ? ? ? ? ? ? ? break;
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? label1.Text = textBox1.Text;
? ? ? ? ? ? ? ? }
?
?
?
? ? ? ? ? ? }
? ? ? ? }
?
?
? ? }
}

原文鏈接:https://blog.csdn.net/qq_38345598/article/details/79561013

欄目分類
最近更新