How to realize Calculator function in C #
Today, the editor will share with you the relevant knowledge points about how to realize the calculator function in C#. The content is detailed and the logic is clear. I believe most people still know too much about this knowledge, so share this article for your reference. I hope you can get something after reading this article, let's take a look at it.
Code:
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 / / this solves the problem of repeated click assignment txtnum.Text = temp;} private void btnzero_Click (object sender, EventArgs e) {string temp = txtnum.Text.ToString () + btnzero.Text;//, which solves the problem of repeated click assignment 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 (num2room0) {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) / solution can contain only one decimal point {temp = txtnum.Text.ToString ();} else {temp = txtnum.Text.ToString () + btndian.Text / / this solves the problem of repeated click assignment} txtnum.Text = temp;}
Initial interface:
The interface after running:
Several digital buttons are generated dynamically, and this is the calculator I want to do.
These are all the contents of the article "how to achieve Calculator function in C#". Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to the industry information channel.