namespaceCalculator 2._0{ Public Partial classForm1:form { PublicForm1 () {InitializeComponent (); } Private BOOLIsok =true;//record whether the operator has just been ordered, or whether to enter the interface for the first time Private stringBiaodashi;//expression before the record operator (in the first column) Private decimalSum;//record the results of previous calculations (in the second column) Private stringPreyunsuanfu;//the operator before logging Private voidButton32_click (Objectsender, EventArgs e)//other digital Buttons{Button btn= (Button) sender;//sender for each corresponding class, here for each button if(Isok)//If the first time you enter the interface or before you click the operator{TextBox2.Text= btn. Text;//The contents of the text box in the following column are changed to the contents of the buttonIsok =false;//change isOK to false means: not just finishing the operator, or just entering the interface, is to get into the else } Else{TextBox2.Text= TextBox2.Text + btn. Text;//if it is not just entered, then the number will be added to the content of the button you just clicked } } Private voidButton36_click (Objectsender, EventArgs e)//0 Button{Button btn= (Button) sender;//sender for each corresponding class, here for each button if(Isok)//If the first time you enter the interface or before you click the operator{TextBox2.Text= btn. Text;//The contents of the text box in the following column are changed to the contents of the button } Else{TextBox2.Text= TextBox2.Text + btn. Text;//if it is not just entered, then the number will be added to the content of the button you just clicked } } Private voidButton44_click (Objectsender, EventArgs e)//operator{Button btn= (Button) sender;//sender for each corresponding class, here for each button stringYunsuanfu = btn. Text;//assigns the contents of the button to the Yunsuanfu; if(Preyunsuanfu = =NULL) {Biaodashi= Biaodashi +TextBox2.Text; Sum=decimal. Parse (TextBox2.Text); } Else { if(! Isok)//If you have just ordered the operator, this piece will not need to be calculated. Conversely, the operation is required.{// ! Isok represents an operator that has just not been ordered, so that the following code is calculated. if(Preyunsuanfu = ="+")//If the operator you just had is a plus{Sum= Sum +decimal. Parse (TextBox2.Text);//The results are calculated and added to the first point. } if(Preyunsuanfu = ="-") {Sum= Sum-decimal. Parse (TextBox2.Text); } if(Preyunsuanfu = ="*") {Sum= Sum *decimal. Parse (TextBox2.Text); } if(Preyunsuanfu = ="/") {Sum= Sum/decimal. Parse (TextBox2.Text); } if(Preyunsuanfu = ="%") {Sum= Sum%decimal. Parse (TextBox2.Text); } Biaodashi= Biaodashi + Preyunsuanfu + textbox2.text;//before an operator, the value of the first column changes to the previous expression + the operator you just entered.TextBox2.Text = Sum.tostring ();//the value of the second column becomes the result}} TextBox1.Text= Biaodashi + Yunsuanfu;//The first column displays the text as an expression + the currently entered operatorPreyunsuanfu = Yunsuanfu;//assigns the value of the Yunsuanfu to the last operator of the record;Isok =true;//as long as isOK is ture, it will empty the value of the current button and re-enter the value } Private voidButton8_click (Objectsender, EventArgs e)//Clear Key{//Initialize all StatesTextBox1.Text =""; TextBox2.Text="0"; Isok=true; Preyunsuanfu=NULL; Sum=0; Biaodashi=""; } Private voidButton6_click (Objectsender, EventArgs e)//Undo Key { if(! Isok)//If you've just not ordered an operator to perform this step { if(TextBox2.Text.Length = =1)//if the text length of the second column is 1{TextBox2.Text="0";//Clicking Undo will turn into 0 .Isok =true;//as soon as 0 is removed, the isOK is changed to true. So 0 will not accumulate, equivalent to re-enter the interface. } Else{TextBox2.Text= TextBox2.Text.Substring (0, TextBox2.Text.Length-1); } } } Private voidButton28_click (Objectsender, EventArgs e)//decimal point { if(TextBox2.Text = ="0")//if it's 0.{TextBox2.Text="0.";//it becomes 0.Isok =false;//change the isOK to false in order to be 0, then you can continue to enter a different number after the decimal point, instead of emptying } if(! Isok&&!textbox2.text.contains ("."))//If the result box is displayed (that is, the point-to-point operator is not allowed), the next step is not performed, and it has just been ordered.{TextBox2.Text= TextBox2.Text +"."; } } Private voidButton30_click (Objectsender, EventArgs e)//equals sign{TextBox1.Text ="";//The first column is empty//the results before the calculation if(Preyunsuanfu = ="+")//If the operator you just had is a plus{Sum= Sum +decimal. Parse (TextBox2.Text);//The results are calculated and added to the first point. } if(Preyunsuanfu = ="-") {Sum= Sum-decimal. Parse (TextBox2.Text); } if(Preyunsuanfu = ="*") {Sum= Sum *decimal. Parse (TextBox2.Text); } if(Preyunsuanfu = ="/") {Sum= Sum/decimal. Parse (TextBox2.Text); } if(Preyunsuanfu = ="%") {Sum= Sum%decimal. Parse (TextBox2.Text); } TextBox2.Text= Sum.tostring ();//The second column appears as the previous result//Initializing ObjectsIsok =true; Biaodashi=""; Preyunsuanfu=NULL; Sum=0; //It's not going to happen. } Private voidButton9_click (Objectsender, EventArgs e)//plus sign { if(! Isok)//It's not just the dot operator, it's not just the interface, that is, if it's a result, the following code is not executed { if(decimal. Parse (TextBox2.Text) >0) {TextBox2.Text="-"+ TextBox2.Text;//add minus sign if greater than 0 } Else{TextBox2.Text= TextBox2.Text.Substring (1);//if it's less than 0, remove the minus . } } } }}
winform--Calculator (most features)