最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

C#實(shí)現(xiàn)計(jì)算器功能

 更新時(shí)間:2022年01月28日 14:42:06   作者:劇里局外  
這篇文章主要為大家詳細(xì)介紹了C#實(shí)現(xiàn)計(jì)算器功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

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

在剛剛接觸c#的時(shí)候,就想做一個(gè)簡(jiǎn)單加減乘除計(jì)算器。這就是目標(biāo),可惜一直沒有動(dòng)手去做,今天特意把它簡(jiǎn)單做了。很簡(jiǎn)單,很簡(jiǎn)單,了卻一個(gè)心愿了。代碼:

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 MyPictureDownloader
{
? ? //http://blog.sina.com.cn/s/blog_60d576800100tf5z.html
? ? //http://jingyan.baidu.com/article/380abd0a6b80701d90192cde.html
? ? public partial class JiSuanQi : Form
? ? {
? ? ? ? public JiSuanQi()
? ? ? ? {
? ? ? ? ? ? InitializeComponent();
? ? ? ? ? ? initButton();
? ? ? ? }

? ? ? ? public delegate double Operater(double num1, double num2);
? ? ? ? public void initButton()
? ? ? ? {
? ? ? ? ? ?var p = new Point(20, 80);
? ? ? ? ? ?Button[] listbtn = ?new Button[9];
? ? ? ? ? ? for (int i = 0; i < 9; i++)?
? ? ? ? ? ? {
? ? ? ? ? ? ? ? listbtn[i] = new Button();
? ? ? ? ? ? ? ? listbtn[i].Name = "btn" + (i + 1).ToString();
? ? ? ? ? ? ? ? listbtn[i].Text = (i+1).ToString();
? ? ? ? ? ? ? ? listbtn[i].SetBounds(p.X, p.Y, 50, 50);
? ? ? ? ? ? ? ? listbtn[i].Click+= new System.EventHandler(ClickHandler);
? ? ? ? ? ? ? ? this.Controls.Add(listbtn[i]);
? ? ? ? ? ? ? ? p.X += 80;
? ? ? ? ? ? ? ? if (p.X >= this.Width - 80)
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? p.X =20;
? ? ? ? ? ? ? ? ? ? p.Y += 60;
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? public void ClickHandler(Object sender, System.EventArgs e)
? ? ? ? {
? ? ? ? ? ? Button btn = (Button)sender; ??
? ? ? ? ? ? string temp=txtnum.Text.ToString()+btn.Text;//這樣解決了重復(fù)點(diǎn)擊賦值問題
? ? ? ? ? ? txtnum.Text = temp;
? ? ? ? }

? ? ? ? private void btnzero_Click(object sender, EventArgs e)
? ? ? ? {
? ? ? ? ? ? string temp = txtnum.Text.ToString() + btnzero.Text;//這樣解決了重復(fù)點(diǎn)擊賦值問題
? ? ? ? ? ? txtnum.Text = temp;
? ? ? ? }

? ? ? ? public double jisuan(string caozuofu, Operater fanga)
? ? ? ? {
? ? ? ? ? ? double num2 = double.Parse(txtnum.Text);
? ? ? ? ? ? double jieguo = 0;
? ? ? ? ? ? //switch(caozuofu){
? ? ? ? ? ? // ? ?case"+":
? ? ? ? ? ? // ? ? ? ?jieguo = fanga(tempnum, num2);
? ? ? ? ? ? // ? ? ? ?break;
? ? ? ? ? ? // ? ?case "-":
? ? ? ? ? ? // ? ? ? ?jieguo = fanga(tempnum, num2);
? ? ? ? ? ? // ? ? ? ?break;
? ? ? ? ? ? // ? ?case "*":
? ? ? ? ? ? // ? ? ? ?jieguo = fanga(tempnum, num2);
? ? ? ? ? ? // ? ? ? ?break;
? ? ? ? ? ? // ? ?case "/":
? ? ? ? ? ? // ? ? ? ?jieguo = fanga(tempnum, num2);
? ? ? ? ? ? // ? ? ? ?break;
? ? ? ? ? ? //}
? ? ? ? ? ? jieguo = fanga(tempnum, num2);
? ? ? ? ? ? return jieguo;
? ? ? ? }

? ? ? ? public double add(double num1, double num2)?
? ? ? ? {
? ? ? ? ? ? return num1 + num2;
? ? ? ? }

? ? ? ? public double jian(double num1, double num2)
? ? ? ? {
? ? ? ? ? ? return num1- num2;
? ? ? ? }

? ? ? ? public double cheng(double num1, double num2)
? ? ? ? {
? ? ? ? ? ? return num1 * num2;
? ? ? ? }

? ? ? ? public double chu(double num1, double num2)
? ? ? ? {
? ? ? ? ? ? double result = 0;
? ? ? ? ? ? if (num2!=0)
? ? ? ? ? ? {
? ? ? ? ? ? result= num1 / num2;
? ? ? ? ? ? }
? ? ? ? ? ? return result;
? ? ? ? }
? ? ? ? public double tempnum = 0;
? ? ? ? public string caozuofu = "";
? ? ? ? public event Operater fangfa;
? ? ? ? private void btnresult_Click(object sender, EventArgs e)
? ? ? ? {
? ? ? ? ? ? txtnum.Text = jisuan(caozuofu, fangfa).ToString();
? ? ? ? }

? ? ? ? private void btnadd_Click(object sender, EventArgs e)
? ? ? ? {
? ? ? ? ? ? tempnum = double.Parse(txtnum.Text);
? ? ? ? ? ? caozuofu = btnadd.Text;
? ? ? ? ? ? txtnum.Text = "";
? ? ? ? ? ? fangfa = add;
? ? ? ? }

? ? ? ? private void btnde_Click(object sender, EventArgs e)
? ? ? ? {
? ? ? ? ? ? tempnum = double.Parse(txtnum.Text);
? ? ? ? ? ? caozuofu = btnde.Text;
? ? ? ? ? ? txtnum.Text = "";
? ? ? ? ? ? fangfa = jian;
? ? ? ? }

? ? ? ? private void btncheng_Click(object sender, EventArgs e)
? ? ? ? {
? ? ? ? ? ? tempnum = double.Parse(txtnum.Text);
? ? ? ? ? ? caozuofu = btncheng.Text;
? ? ? ? ? ? txtnum.Text = "";
? ? ? ? ? ? fangfa = cheng;
? ? ? ? }

? ? ? ? private void btnchu_Click(object sender, EventArgs e)
? ? ? ? {
? ? ? ? ? ? tempnum = double.Parse(txtnum.Text);
? ? ? ? ? ? caozuofu = btnchu.Text;
? ? ? ? ? ? txtnum.Text = "";
? ? ? ? ? ? fangfa = chu;
? ? ? ? }

? ? ? ? private void btndian_Click(object sender, EventArgs e)
? ? ? ? {
? ? ? ? ? ? if (txtnum.Text.ToString()=="")?
? ? ? ? ? ? {
? ? ? ? ? ? ? ? txtnum.Text = "0";
? ? ? ? ? ? }
? ? ? ? ? ? string temp="";
? ? ? ? ? ? if (txtnum.Text.ToString().IndexOf(".") > 0)//解決只能包含一個(gè)小數(shù)點(diǎn)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? temp = txtnum.Text.ToString();
? ? ? ? ? ? }
? ? ? ? ? ? else?
? ? ? ? ? ? {
? ? ? ? ? ? ? ? temp = txtnum.Text.ToString() + btndian.Text;//這樣解決了重復(fù)點(diǎn)擊賦值問題
? ? ? ? ? ? }
? ? ? ? ? ? txtnum.Text = temp;
? ? ? ? }
? ? }
}

初始界面:

運(yùn)行后的界面:

幾個(gè)數(shù)字按鈕是動(dòng)態(tài)生成的,這就是我想要做的計(jì)算器。

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

相關(guān)文章

最新評(píng)論

宿州市| 连州市| 雷波县| 鄂尔多斯市| 滨州市| 普兰县| 浦江县| 山东| 麻城市| 北碚区| 永丰县| 河间市| 扎鲁特旗| 故城县| 安仁县| 崇仁县| 乐亭县| 游戏| 永福县| 穆棱市| 汤阴县| 潍坊市| 万山特区| 泽库县| 大石桥市| 天柱县| 肃南| 鲁山县| 通化县| 大关县| 湘潭市| 靖边县| 巴彦县| 马边| 社旗县| 汝城县| 莆田市| 武隆县| 合川市| 德惠市| 昌邑市|