Simple Calculator C # learning

Source: Internet
Author: User

C # source code

Https://git.oschina.net/363451039/Jisuanqi

Implement subtraction Square open reciprocal keyboard input
History Query and delete


Memory Plus/minus function not implemented

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 windowsformsapplication3{public partial class Mainform:form {public MainForm () {Initia        Lizecomponent (); }/////To store the most recently entered digital e_num and recent operations E_op///button CE save E_num, while Button C empties E_num and E_op Doub        Le e_num = 0;        Double e_res=0;        String e_op= "";        */String Op= "";//operator Double num1=0;        Double num2=0;        Double res=0;  int iOp = 0;//operator position bool Newop = true;                Start the new operation bool Addop = FALSE;                Type the number private void Button_click_num (object sender, EventArgs e) {if (NEWOP) {                Textnumbox.text = "";                Newop = false;                Addop = false;               /* E_num = num2;                E_res=res;                E_op=op;            */}///* Gets the button value assigned to Textnumbox.ttext///Multiple button's Click event named Button_click_num            Implement batch Button_click_num operation Button tempnum = (button) sender;            Textnumbox.text + = tempNum.Text.ToString (); */} private void Button_click_op (object sender, EventArgs e) {button TempO            p = (Button) sender;        Operater (TempOp.Text.ToString ()); private void Operater (string oper) {if (ADDOP) {textnumbox.text = C Onvert.                ToString (res) + oper;            Newop = false;            } else {textnumbox.text + = oper;            } Op=oper;            E_op=tempop.text.tostring ();            E_num1=textnumbox.text.substring (0, textnumbox.text.length-1); E_num2=textnumbox.text.skipwhIle<char> (char i,result<char,i== ' + ' | | i== '-' | | i== ' x ' | |            i== ' ÷ ' >predicate);//does not use this function} private void Operater (char oper) {if (ADDOP)            {Newop = false;            } op = convert.tostring (oper);            E_op=tempop.text.tostring ();            E_num1=textnumbox.text.substring (0, textnumbox.text.length-1); E_num2=textnumbox.text.skipwhile<char> (char i,result<char,i== ' + ' | | i== '-' | | i== ' x ' | |            i== ' ÷ ' >predicate);//does not use this function} private void Button_click_del (object sender, EventArgs e) { if (TextNumBox.Text.Length > 0) {textnumbox.text = textNumBox.Text.Substring (0, Textnumbo                X.TEXT.LENGTH-1);            Newop = false;            }} private void Button_click_ce (object sender, EventArgs e) {textnumbox.text = "";        Newop = true; } private void Button_click_c (object sender, EVentargs e) {textnumbox.text = "";            /* E_num = 0;            E_op = "";                */} private void Button_click_result (object sender, EventArgs e) {try {                    if (NEWOP) do not judge in result whether it is a restart calculation, but update textnumbox.text//{result ();                    res = Operate (OP);                    Newop = true;                    Addop = true;                COMMON.HISTORYLIST.ADD (Textnumbox.text);                }//else//{//result (RES);                 res = Operate (OP);                }} catch (Exception exc) {textnumbox.text= "Error";                Newop = false;            Addop = true;            }} private void result () {iOp = TextNumBox.Text.IndexOf (OP); NUM1 = convert.todouble (textNumBox.Text.SuBstring (0, iOp)); num2 = convert.todouble (textNumBox.Text.Substring (iOp + 1, textnumbox.text.length-iop-1));//-iop, The second parameter is the number of substring after the start of the//private void result (double oldnum)//{//IOP=TEXTNU        MBox.Text.LastIndexOf (OP);        NUM1 = Oldnum;        num2 = convert.todouble (textNumBox.Text.Substring (iOp + 1, textnumbox.text.length-iop-1));            } private double Operate (string opp) {double ress;                if (op = = "+") {ress = Num1 + num2;            Textnumbox.text + = "=" + ress;                } else if (op = = "-") {ress = num1-num2;            Textnumbox.text + = "=" + ress;                } else if (op = = "X") {ress = Num1 * NUM2;            Textnumbox.text + = "=" + ress; } else if (op = = "÷") {ress = num1/num2;            Textnumbox.text + = "=" + ress;            } else {ress = 0;        } return ress;            private void Button_click_fushu (object sender, EventArgs e) {textnumbox.text + = "-";            Newop = false;        COMMON.HISTORYLIST.ADD (Textnumbox.text);            } private void Button_click_pingfanggen (object sender, EventArgs e) {double numtemp;            try {numtemp = convert.todouble (Textnumbox.text);            } catch (System.FormatException)//cannot be converted to double under an exception with the res result calculated with {numtemp = res;            } res = math.sqrt (numtemp);            Textnumbox.text = "√ (" + numtemp + ") =" + res;            Newop = true;        COMMON.HISTORYLIST.ADD (Textnumbox.text);            } private void Button_click_daoshu (object sender, EventArgs e) {double numtemp;    Try        {numtemp = convert.todouble (Textnumbox.text);            } catch (System.FormatException)//cannot be converted to double under an exception with the res result calculated with {numtemp = res;            } res = 1/numtemp;            Textnumbox.text = "1/(" + numtemp + ") =" + convert.tostring (res);            Newop = true;        COMMON.HISTORYLIST.ADD (Textnumbox.text);   } private void Click_about (object sender, EventArgs e) {About aboutbox = new about (); To open a new window, first AboutBox.        ShowDialog ();   } private void Click_history (object sender, EventArgs e) {History historybox = new History (); To open a new window, first Historybox.        ShowDialog (); private void Click_pastenow (object sender, EventArgs e) {textnumbox.text + = Clipboard.gettext (        ); } private void Click_copynow (object sender, EventArgs e) {Clipboard.settext (textnumbox.teXT);        } bool flag = TRUE;            private void Keypress_equekey (object sender, KeyPressEventArgs e) {switch (E.keychar)                            {case ' = ': {if (flag) {                            Flag = false;                                 try {//if (NEWOP) do not judge in result whether it is a restart calculation, but rather update the Textnumbox.text                                    {//textnumbox.text + = "=";                                    Result (); if (TextNumBox.Text.Length > 0)//{//Textn                                    Umbox.text = textNumBox.Text.Substring (0, textnumbox.text.length-2);                                    Newop = false; } iOp = TextNumBox.Text.IndexOf (OP);                                    int op0 = textnumbox.text[0];//(0);                                    int OP1 = textnumbox.text[1];                                    NUM1 = convert.todouble (textNumBox.Text.Substring (0, iOp));                                    String temp = textNumBox.Text.Substring (iOp + 1, textnumbox.text.length-iop-1); num2 = convert.todouble (textNumBox.Text.Substring (iOp + 1, textnumbox.text.length-iop-1));//-iop,                                    The second parameter is the number of res = Operate (OP) after the start of the substring.                                    Newop = true;                                    Addop = true;                                COMMON.HISTORYLIST.ADD (Textnumbox.text); }//else//{//Result (res                                );                                 res = Operate (OP);           //}                 } catch (Exception exc) {                                Textnumbox.text = "Error";                                Newop = false;                            Addop = true;                            }} else {flag = true;                        TextNumBox.Text.Substring (1, textnumbox.text.length-1);                    } break;                        } case ' + ': {operater (' + ');                    Break                        } Case '-': {operater ('-');                    Break                        } case ' * ': {operater (' x ');                    Break               } case '/':     {operater (' ÷ ');                    Break  }}}}//History pass public class Common {public static list<string> historylist =        New List<string> ();    public static int a = 0; }}



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 windowsformsapplication3{public partial class History:form {public history () {Initiali        Zecomponent (); } private void Button_click_copy (object sender, EventArgs e) {Clipboard.settext (listbox1.items[l Istbox1.selectedindex].        ToString ());            } private void Button_click_delete (object sender, EventArgs e) {//listbox1.clearselected (); ListBox1.Items.Clear ();            Clear all int sel = Listbox1.selectedindex;        ListBox1.Items.RemoveAt (SEL); private void History_load (object sender, EventArgs e) {foreach (String A in common.historylist            ) {LISTBOX1.ITEMS.ADD (a); }        }    }}



Simple Calculator C # learning

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.