欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C#實現(xiàn)簡單的計算器小功能

 更新時間:2022年01月29日 09:14:29   作者:我自是年少韶華傾負(fù)  
這篇文章主要為大家詳細(xì)介紹了C#實現(xiàn)簡單的計算器小功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下

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

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

代碼:

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

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

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

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)//數(shù)字符所綁定的事件
? ? ? ? {
? ? ? ? ? ? btn = (Button)sender;
? ? ? ? ? ? textBox1.Text = textBox1.Text + btn.Text;//將所點擊的數(shù)字付呈現(xiàn)在textBox上面
?
? ? ? ? }
? ? ? ? private void buttonp_Click(object sender, EventArgs e)//運算符所綁定的事件
? ? ? ? {
? ? ? ? ? ? btn = (Button)sender;
? ? ? ? ? ? if (btn.Name != "button12")//如果不是"="
? ? ? ? ? ? {
? ? ? ? ? ? ? ? x = Convert.ToDouble(textBox1.Text);//將所所輸入的第一個字符保留下來
? ? ? ? ? ? ? ? textBox1.Text = "";//清空textBox的內(nèi)容
? ? ? ? ? ? ? ? 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;
? ? ? ? ? ? ? ? }
?
?
?
? ? ? ? ? ? }
? ? ? ? }
?
?
? ? }
}

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論