Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use C # to implement a simple calculator Mini Program

Shulou Source: shulou.com Published: 2022-06-01 19:29:35 10月03日 Update

This article mainly introduces "how to use C # to achieve a simple calculator Mini Program". In daily operation, I believe many people have doubts about how to use C # to achieve a simple calculator Mini Program. I have consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the doubts of "how to use C # to achieve a simple calculator Mini Program". Next, please follow the editor to study!

The following is a screenshot of Mini Program after production

Code snippet in which a function of dataTable is used for calculation

Dt.Compute (CurStr, string.Empty)

Using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Windows.Forms;namespace Calculate {public partial class CalculateFrom: Form {public CalculateFrom () {InitializeComponent ();} private string FhStr = string.Empty; private string CurStr = string.Empty / / calculate the string / / equal to the equal button processing private void Click_DENG_Click (object sender, EventArgs e) {DataTable dt = new DataTable (); if (string.IsNullOrEmpty (CurStr)) {return } List KeyList = new List {"+", "-", "*", "/"}; if (KeyList.Contains (CurStr.Substring (CurStr.Length-1) / / determines that the last bit is four operators, then remove {CurStr = CurStr.Substring (CurStr.Length-1) } if (string.IsNullOrEmpty (CurStr)) / / if left empty, the result is assigned a zero value {this.Text_JM.Text = "0"; return;} else {CurStr = Convert.ToString (dt.Compute (CurStr, string.Empty)) / / calculate the form of calculation using DataTable this.Text_JM.Text = CurStr if the condition is met / / the calculation result is assigned to Text}} / / the value is added / / the data is only calculated by private void ValueClick (string ClickV) {/ / to judge. Determine whether the click is a non-empty number or decimal place if (! string.IsNullOrEmpty (ClickV)) {if (ClickV = = "." & & string.IsNullOrEmpty (CurStr)) / / the first place is the decimal place {CurStr = "0." / / if you click on the decimal place first, you will automatically increase the zero value before it} else if (ClickV = = ".") / / if you already have a decimal place, you can no longer enter {if (! CurStr.Contains (".")) {CurStr = CurStr + "." }} else CurStr = CurStr + ClickV; this.Text_JM.Text = CurStr / / copy the string to the text displayed in the foreground}} / / four operational symbol processing private void ValueCalculate (string Key) {if (string.IsNullOrEmpty (CurStr)) {return } if (CurStr.Substring (CurStr.Length-1,1) = ".") {CurStr = CurStr + "0"; / / if the last decimal place automatically completes a zero value} List KeyList=new List {"+", "*", "/"} If (KeyList.Contains (CurStr.Substring (CurStr.Length-1, 1)) / / if the last bit is already four operation symbols, then no operation {return;// last bit is currently the symbol} CurStr = CurStr + Key; this.Text_JM.Text = CurStr } # region numeric private void Click_1_Click (object sender, EventArgs e) {ValueClick ("1");} private void Click_2_Click (object sender, EventArgs e) {ValueClick ("2");} private void Click_3_Click (object sender, EventArgs e) {ValueClick ("3") } private void Click_4_Click (object sender, EventArgs e) {ValueClick ("4");} private void Click_5_Click (object sender, EventArgs e) {ValueClick ("5");} private void Click_6_Click (object sender, EventArgs e) {ValueClick ("6") } private void Click_7_Click (object sender, EventArgs e) {ValueClick ("7");} private void Click_8_Click (object sender, EventArgs e) {ValueClick ("8");} private void Click_9_Click (object sender, EventArgs e) {ValueClick ("9") } private void Click_0_Click (object sender, EventArgs e) {ValueClick ("0");} / / decimal point private void Click_C_Click (object sender, EventArgs e) {ValueClick (".") } # endregion # region add, subtract, multiply and divide private void Click_A_Click (object sender, EventArgs e) {ValueCalculate ("+");} private void Click_R_Click (object sender, EventArgs e) {ValueCalculate ("-") } private void Click_X_Click (object sender, EventArgs e) {ValueCalculate ("*");} private void Click_L_Click (object sender, EventArgs e) {ValueCalculate ("/") } # endregion}} at this point, the study on "how to implement a simple calculator Mini Program with C #" is over. I hope I can solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Tags: Decimals programs calculators digits digits learning four numeric values symbols operations four operations characters strings more results processing help utility addition subtraction multiplication and division next Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno OPPO Reno Shulou Information Xiaomi Redmi Huawei