C # Calculator writing code _c# tutorial

Source: Internet
Author: User

Use C # to write a calculator. The following figure, can complete the basic arithmetic.

Of course, this program is not even a self-contained calculator with Windows accessories.
However, the logic of this program is still very worthwhile to think about, first of all, you should consider the user by +-*/= and other operational symbols, digital keys after the status of the calculator problem.
Then you want to prevent the problem of pressing a key multiple times. Like a decimal point. You should not let the user type two times when entering a number.
Finally, there are two arrays, one that holds the user's input and another that holds the user's input.
The production process is as follows,
1, the layout is as follows, at the same time can refer to the "Simple implementation of C # form procedures to determine whether leap year" inside the method provided, the button inside the position set up, prohibit this window resize, change the name of each component.

2, Form1.cs specific code as follows:

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 Calculator {public partial class Form1:form {private list<double> value_list = new List<dou Ble> ()//The number entered by the user private list<int> operator_list = new list<int> ();//The operator entered by the user, defined + 0,-is 1,x to 3/ /status Record private bool Add_flag = false;//+ Press private bool Minus_flag = false;//-Press private bool Multi_flag = Fals E;//X Press private bool Div_flag = False;//÷ Press private bool Result_flag = false;//= Press private bool CAN_OPERATE_FL
    AG = false;//pressed = Whether to respond to public Form1 () {InitializeComponent (); }//Number key pressed, containing 0 and., similar to 000001223 such cases are allowed here, because C # can speak 000001223 itself into 1223 private void Num_down (String num) {if (AD D_flag | | Minus_flag | | Multi_flag | | Div_flag | |
Result_flag) {if (Result_flag)//By the equal sign, just finished the state of an operation {          Label1.
        Text = "";
        Textbox1.clear ()//If the user has just entered an operator Add_flag = false;
        Minus_flag = false;
        Multi_flag = false;
        Div_flag = false;
      Result_flag = false; The IF (Num. Equals (".") && textBox1.Text.IndexOf (".") < 0) | | !num.
      Equals ("."))
        {//If the user has entered a decimal point, you can determine whether the currently entered number contains decimal points. To allow input textbox1.text + = num; Label1.
        Text + num;
      Can_operate_flag = true;
    } private void Bt0_click (object sender, EventArgs e) {Num_down ("0");
    } private void Bt1_click (object sender, EventArgs e) {num_down ("1");
    } private void Bt2_click (object sender, EventArgs e) {Num_down ("2");
    } private void Bt3_click (object sender, EventArgs e) {Num_down ("3");
    } private void Bt4_click (object sender, EventArgs e) {Num_down ("4"); } private void Bt5_click (object sender, EventArgs e) {num_Down ("5");
    } private void Bt6_click (object sender, EventArgs e) {Num_down ("6");
    } private void Bt7_click (object sender, EventArgs e) {Num_down ("7");
    } private void Bt8_click (object sender, EventArgs e) {Num_down ("8");
    } private void Bt9_click (object sender, EventArgs e) {num_down ("9");

    } private void Bt_point_click (object sender, EventArgs e) {Num_down ("."); }//symbol key input private void Bt_plus_click (object sender, EventArgs e) {if (!add_flag)//Prevents users from entering a symbol key more than once, the symbol key is only allowed
        Allow input once {Result_flag = false; Value_list. ADD (double. Parse (TextBox1.Text));//Put the currently entered number into the Value_list operator_list.
        ADD (0); Label1.
        Text + = "+";
        Add_flag = true; Can_operate_flag = false;//has just entered the symbol and cannot form a normal expression, such as 111+, set to not run state} private void Bt_minus_click (object send Er, EventArgs e) {if (!minus_flag) {Result_flag = FalsE Value_list. ADD (double.
        Parse (TextBox1.Text)); Operator_list.
        ADD (1); Label1.
        Text + + "-";
        Minus_flag = true;
      Can_operate_flag = false; }} private void Bt_multi_click (object sender, EventArgs e) {if (!multi_flag) {Result_f
        lag = false; Value_list. ADD (double.
        Parse (TextBox1.Text)); Operator_list.
        ADD (2); Label1. Text = "(" + Label1. Text + ")" + "X";//Add parentheses to the previous item that has been entered.
        (operator stack problem is a very complex data structure problem, not done here: P) Multi_flag = true;
      Can_operate_flag = false; }} private void Bt_div_click (object sender, EventArgs e) {if (!div_flag) {Result_flag
        = false; Value_list. ADD (double.
        Parse (TextBox1.Text)); Operator_list.
        ADD (3); Label1. Text = "(" + Label1.
        Text + ")" + "";
        Div_flag = true;
      Can_operate_flag = false; }} private void Bt_result_click (object sender, EventArgs e) {if (value_list). CounT > 0 && operator_list.
        Count > 0 && can_operate_flag) {//need to prevent users from not entering a number, or just enter a number, click =. Value_list. ADD (double.
        Parse (TextBox1.Text));
        Double total = value_list[0]; for (int i = 0; i < operator_list. Count;
          i++) {int _operator = Operator_list[i];//operator is a keyword of C # operator overloading, preceded by _ to differentiate switch (_operator)
              {case 0:total + = value_list[i + 1];
            Break
              Case 1:total-= value_list[i + 1];
            Break
              Case 2:total *= value_list[i + 1];
            Break
              Case 3:total/= value_list[i + 1];
          Break
        } TextBox1.Text = Total + ""; Label1.
        Text = Total + ""; Operator_list. Clear (),//finish, empty the cumulative number and the operation array Value_list.
        Clear (); Result_flag = true;//= Press}}//CE key to initialize everything to the private void Btce_click (object sender, EVentargs e) {operator_list.
      Clear (); Value_list.
      Clear ();
      Add_flag = false;
      Minus_flag = false;
      Multi_flag = false;
      Div_flag = false;
      Result_flag = false;
      Can_operate_flag = false;
      Textbox1.clear (); Label1.
    Text = "";
 }

  }
}

The basic idea here is that the user presses any symbol key to record a currently entered number and put it in the array value_list.

When the equal sign is pressed, such as when the user enters a 100+222+33, the total is initialized to 100, then the operator + with the target number 222 is total=total+222, and so on.

After the operation completes, the result is hit to the textbox, and if the user presses the remaining symbol keys, the result is also entered into the array of record input value_list

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.