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

學(xué)無先后,達(dá)者為師

網(wǎng)站首頁 編程語言 正文

C#實(shí)現(xiàn)簡單的計(jì)算器小功能_C#教程

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

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

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

代碼:

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

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

思想:按下數(shù)字按鈕,將數(shù)字按鈕的值連接到textbox上,然后按下運(yùn)算符判斷是否為等于,并記錄運(yùn)算符的內(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;//將所點(diǎn)擊的數(shù)字付呈現(xiàn)在textBox上面
?
? ? ? ? }
? ? ? ? private void buttonp_Click(object sender, EventArgs e)//運(yùn)算符所綁定的事件
? ? ? ? {
? ? ? ? ? ? btn = (Button)sender;
? ? ? ? ? ? if (btn.Name != "button12")//如果不是"="
? ? ? ? ? ? {
? ? ? ? ? ? ? ? x = Convert.ToDouble(textBox1.Text);//將所所輸入的第一個(gè)字符保留下來
? ? ? ? ? ? ? ? textBox1.Text = "";//清空textBox的內(nèi)容
? ? ? ? ? ? ? ? s = btn.Name;//獲取運(yùn)算符的種類
? ? ? ? ? ? ? ? label1.Text = x.ToString();//將第一個(gè)所按的字符輸出來
?
?
? ? ? ? ? ? }
? ? ? ? ? ? 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("除零錯(cuò)誤!!!", "信息提示", MessageBoxButtons.OK);
? ? ? ? ? ? ? ? ? ? ? ? ? ? else
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? textBox1.Text = (x / y).ToString();
?
?
? ? ? ? ? ? ? ? ? ? ? ? ? ? break;
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? label1.Text = textBox1.Text;
? ? ? ? ? ? ? ? }
?
?
?
? ? ? ? ? ? }
? ? ? ? }
?
?
? ? }
}

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

欄目分類
最近更新