• c#计算器


      1 using System;
      2 using System.Collections.Generic;
      3 using System.ComponentModel;
      4 using System.Data;
      5 using System.Drawing;
      6 using System.Linq;
      7 using System.Text;
      8 using System.Windows.Forms;
      9 
     10 namespace 帅的计算器
     11 {
     12     public partial class Form1 : Form
     13     {
     14         //类字段变量声明
     15         //类字段:第一个运算数、第二个运算数、运算结果
     16         private decimal _num1 = 0, _num2 = 0, _result = 0;
     17         //类字段:运算符号
     18         private string _operator = "";
     19         //类字段:运算符号的数量
     20         private int _operatorCount = 0;
     21         //类字段:首数字标志位
     22         private bool _firstNumberFlag = true;
     23         //类字段:进制标志位
     24         private string _NumberSystem = "dec";
     25 
     26 
     27         //类方法:把十进制转化为二进制
     28         public string DecToBin(int dec)
     29         {
     30             string bin = "";
     31             while (dec > 1)
     32             {
     33                 bin = Convert.ToChar(dec % 2 + '0') + bin;
     34                 dec = dec / 2;
     35             }
     36             bin = Convert.ToChar(dec + '0') + bin;
     37 
     38             return bin;
     39         }
     40 
     41         //类方法:把二进制转化为十进制
     42         public int BinToDec(string bin)
     43         {
     44             int dec = 0;
     45             for (int i = bin.Length; i >= 1; i--)
     46             {
     47                 dec = dec + Convert.ToInt32(bin[i - 1] - '0') * (int)(Math.Pow(2, bin.Length - i));
     48             }
     49 
     50             return dec;
     51         }
     52 
     53         //类方法:把十进制转化为八进制
     54         public string DecToOct(int dec)
     55         {
     56             string oct = "";
     57             while (dec > 7)
     58             {
     59                 oct = Convert.ToChar(dec % 8 + '0') + oct;
     60                 dec = dec / 8;
     61             }
     62             oct = Convert.ToChar(dec + '0') + oct;
     63 
     64             return oct;
     65         }
     66 
     67         //类方法:把八进制转化为十进制
     68         public int OctToDec(string oct)
     69         {
     70             int dec = 0;
     71             for (int i = oct.Length; i >= 1; i--)
     72             {
     73                 dec = dec + Convert.ToInt32(oct[i - 1] - '0') * (int)(Math.Pow(8, oct.Length - i));
     74             }
     75 
     76             return dec;
     77         }
     78 
     79         //构造方法
     80         public Form1()
     81         {
     82             InitializeComponent();
     83         }
     84 
     85         //按钮"0"的Click事件响应方法
     86         private void buttonNum0_Click(object sender, EventArgs e)
     87         {
     88             //判断是否为首数字。如果是,则替换;如果否,则追加。
     89             if (_firstNumberFlag == true)
     90             {
     91                 //编辑框文字替换为"0"
     92                 textBoxResult.Text = "0";
     93                 //首数字标志位设为false
     94                 _firstNumberFlag = false;
     95             }
     96             else
     97                 //编辑框文字中追加一个"0"
     98                 textBoxResult.Text = textBoxResult.Text + "0";
     99 
    100             //使输入焦点落到编辑框textBoxResult中
    101             textBoxResult.Focus();
    102             //让光标落在编辑框textBoxResult的最后面
    103             textBoxResult.Select(textBoxResult.Text.Length, 0);
    104         }
    105 
    106         //按钮"1"的Click事件响应方法
    107         private void buttonNum1_Click(object sender, EventArgs e)
    108         {
    109             //判断是否为首数字。如果是,则替换;如果否,则追加。
    110             if (_firstNumberFlag == true)
    111             {
    112                 //编辑框文字替换为"1"
    113                 textBoxResult.Text = "1";
    114                 //首数字标志位设为false
    115                 _firstNumberFlag = false;
    116             }
    117             else
    118                 //编辑框文字中追加一个"1"
    119                 textBoxResult.Text = textBoxResult.Text + "1";
    120 
    121             //使输入焦点落到编辑框textBoxResult中
    122             textBoxResult.Focus();
    123             //让光标落在编辑框textBoxResult的最后面
    124             textBoxResult.Select(textBoxResult.Text.Length, 0);
    125         }
    126 
    127         //按钮"2"的Click事件响应方法
    128         private void buttonNum2_Click(object sender, EventArgs e)
    129         {
    130             //判断是否为首数字。如果是,则替换;如果否,则追加。
    131             if (_firstNumberFlag == true)
    132             {
    133                 //编辑框文字替换为"2"
    134                 textBoxResult.Text = "2";
    135                 //首数字标志位设为false
    136                 _firstNumberFlag = false;
    137             }
    138             else
    139                 //编辑框文字中追加一个"2"
    140                 textBoxResult.Text = textBoxResult.Text + "2";
    141 
    142             //使输入焦点落到编辑框textBoxResult中
    143             textBoxResult.Focus();
    144             //让光标落在编辑框textBoxResult的最后面
    145             textBoxResult.Select(textBoxResult.Text.Length, 0);
    146         }
    147 
    148         //按钮"3"的Click事件响应方法
    149         private void buttonNum3_Click(object sender, EventArgs e)
    150         {
    151             //判断是否为首数字。如果是,则替换;如果否,则追加。
    152             if (_firstNumberFlag == true)
    153             {
    154                 //编辑框文字替换为"3"
    155                 textBoxResult.Text = "3";
    156                 //首数字标志位设为false
    157                 _firstNumberFlag = false;
    158             }
    159             else
    160                 //编辑框文字中追加一个"3"
    161                 textBoxResult.Text = textBoxResult.Text + "3";
    162 
    163             //使输入焦点落到编辑框textBoxResult中
    164             textBoxResult.Focus();
    165             //让光标落在编辑框textBoxResult的最后面
    166             textBoxResult.Select(textBoxResult.Text.Length, 0);
    167         }
    168 
    169         //按钮"4"的Click事件响应方法
    170         private void buttonNum4_Click(object sender, EventArgs e)
    171         {
    172             //判断是否为首数字。如果是,则替换;如果否,则追加。
    173             if (_firstNumberFlag == true)
    174             {
    175                 //编辑框文字替换为"4"
    176                 textBoxResult.Text = "4";
    177                 //首数字标志位设为false
    178                 _firstNumberFlag = false;
    179             }
    180             else
    181                 //编辑框文字中追加一个"4"
    182                 textBoxResult.Text = textBoxResult.Text + "4";
    183 
    184             //使输入焦点落到编辑框textBoxResult中
    185             textBoxResult.Focus();
    186             //让光标落在编辑框textBoxResult的最后面
    187             textBoxResult.Select(textBoxResult.Text.Length, 0);
    188         }
    189 
    190         //按钮"5"的Click事件响应方法
    191         private void buttonNum5_Click(object sender, EventArgs e)
    192         {
    193             //判断是否为首数字。如果是,则替换;如果否,则追加。
    194             if (_firstNumberFlag == true)
    195             {
    196                 //编辑框文字替换为"5"
    197                 textBoxResult.Text = "5";
    198                 //首数字标志位设为false
    199                 _firstNumberFlag = false;
    200             }
    201             else
    202                 //编辑框文字中追加一个"5"
    203                 textBoxResult.Text = textBoxResult.Text + "5";
    204 
    205             //使输入焦点落到编辑框textBoxResult中
    206             textBoxResult.Focus();
    207             //让光标落在编辑框textBoxResult的最后面
    208             textBoxResult.Select(textBoxResult.Text.Length, 0);
    209         }
    210 
    211         //按钮"6"的Click事件响应方法
    212         private void buttonNum6_Click(object sender, EventArgs e)
    213         {
    214             //判断是否为首数字。如果是,则替换;如果否,则追加。
    215             if (_firstNumberFlag == true)
    216             {
    217                 //编辑框文字替换为"6"
    218                 textBoxResult.Text = "6";
    219                 //首数字标志位设为false
    220                 _firstNumberFlag = false;
    221             }
    222             else
    223                 //编辑框文字中追加一个"6"
    224                 textBoxResult.Text = textBoxResult.Text + "6";
    225 
    226             //使输入焦点落到编辑框textBoxResult中
    227             textBoxResult.Focus();
    228             //让光标落在编辑框textBoxResult的最后面
    229             textBoxResult.Select(textBoxResult.Text.Length, 0);
    230         }
    231 
    232         //按钮"7"的Click事件响应方法
    233         private void buttonNum7_Click(object sender, EventArgs e)
    234         {
    235             //判断是否为首数字。如果是,则替换;如果否,则追加。
    236             if (_firstNumberFlag == true)
    237             {
    238                 //编辑框文字替换为"7"
    239                 textBoxResult.Text = "7";
    240                 //首数字标志位设为false
    241                 _firstNumberFlag = false;
    242             }
    243             else
    244                 //编辑框文字中追加一个"7"
    245                 textBoxResult.Text = textBoxResult.Text + "7";
    246 
    247             //使输入焦点落到编辑框textBoxResult中
    248             textBoxResult.Focus();
    249             //让光标落在编辑框textBoxResult的最后面
    250             textBoxResult.Select(textBoxResult.Text.Length, 0);
    251         }
    252 
    253         //按钮"8"的Click事件响应方法
    254         private void buttonNum8_Click(object sender, EventArgs e)
    255         {
    256             //判断是否为首数字。如果是,则替换;如果否,则追加。
    257             if (_firstNumberFlag == true)
    258             {
    259                 //编辑框文字替换为"8"
    260                 textBoxResult.Text = "8";
    261                 //首数字标志位设为false
    262                 _firstNumberFlag = false;
    263             }
    264             else
    265                 //编辑框文字中追加一个"8"
    266                 textBoxResult.Text = textBoxResult.Text + "8";
    267 
    268             //使输入焦点落到编辑框textBoxResult中
    269             textBoxResult.Focus();
    270             //让光标落在编辑框textBoxResult的最后面
    271             textBoxResult.Select(textBoxResult.Text.Length, 0);
    272         }
    273 
    274         //按钮"9"的Click事件响应方法
    275         private void buttonNum9_Click(object sender, EventArgs e)
    276         {
    277             //判断是否为首数字。如果是,则替换;如果否,则追加。
    278             if (_firstNumberFlag == true)
    279             {
    280                 //编辑框文字替换为"9"
    281                 textBoxResult.Text = "9";
    282                 //首数字标志位设为false
    283                 _firstNumberFlag = false;
    284             }
    285             else
    286                 //编辑框文字中追加一个"9"
    287                 textBoxResult.Text = textBoxResult.Text + "9";
    288 
    289             //使输入焦点落到编辑框textBoxResult中
    290             textBoxResult.Focus();
    291             //让光标落在编辑框textBoxResult的最后面
    292             textBoxResult.Select(textBoxResult.Text.Length, 0);
    293         }
    294 
    295         //按钮"."的Click事件响应方法
    296         private void buttonDot_Click(object sender, EventArgs e)
    297         {
    298             //判断是否为首数字。如果是,则替换;如果否,则追加。
    299             if (_firstNumberFlag == true)
    300             {
    301                 //编辑框文字替换为"."
    302                 textBoxResult.Text = ".";
    303                 //首数字标志位设为false
    304                 _firstNumberFlag = false;
    305             }
    306             else
    307             {
    308                 //判断现有数字中是否含有小数点
    309                 if (textBoxResult.Text.IndexOf('.') == -1)
    310                     //编辑框文字中追加一个"."
    311                     textBoxResult.Text = textBoxResult.Text + ".";
    312             }
    313 
    314             //使输入焦点落到编辑框textBoxResult中
    315             textBoxResult.Focus();
    316             //让光标落在编辑框textBoxResult的最后面
    317             textBoxResult.Select(textBoxResult.Text.Length, 0);
    318         }
    319 
    320 
    321         //按钮"+"的Click事件响应方法
    322         private void buttonAdd_Click(object sender, EventArgs e)
    323         {
    324             //首数字标志位设为true
    325             _firstNumberFlag = true;
    326 
    327             //运算符号数量加1
    328             _operatorCount = _operatorCount + 1;
    329             //判断是否为第一个运算符
    330             if (_operatorCount == 1)
    331             {
    332                 //保存第一个运算数到类字段变量_num1中(注意进制转化问题)
    333                 if (_NumberSystem == "dec")
    334                     _num1 = Convert.ToDecimal(textBoxResult.Text);
    335                 else if (_NumberSystem == "bin")
    336                     _num1 = BinToDec(textBoxResult.Text);
    337                 else if (_NumberSystem == "oct")
    338                     _num1 = OctToDec(textBoxResult.Text);
    339                 //保存运算符号到类字段变量_operator中
    340                 _operator = "+";
    341                 //清空编辑框 
    342                 textBoxResult.Text = "";
    343             }
    344             else
    345             {
    346                 //保存第二个运算数到类字段变量_num2中(注意进制转化问题)
    347                 if (_NumberSystem == "dec")
    348                     _num2 = Convert.ToDecimal(textBoxResult.Text);
    349                 else if (_NumberSystem == "bin")
    350                     _num2 = BinToDec(textBoxResult.Text);
    351                 else if (_NumberSystem == "oct")
    352                     _num2 = OctToDec(textBoxResult.Text);
    353                 //进行运算
    354                 if (_operator == "+")
    355                     _num1 = _num1 + _num2;
    356                 else if (_operator == "-")
    357                     _num1 = _num1 - _num2;
    358                 else if (_operator == "*")
    359                     _num1 = _num1 * _num2;
    360                 else if (_operator == "/")
    361                     _num1 = _num1 / _num2;
    362                 //保存运算符号到类字段变量_operator中
    363                 _operator = "+";
    364                 //在编辑框中显示前面数字的运算结果(注意进制转化问题)
    365                 if (_NumberSystem == "dec")
    366                     textBoxResult.Text = _num1.ToString();
    367                 else if (_NumberSystem == "bin")
    368                     textBoxResult.Text = DecToBin((int)_num1);
    369                 else if (_NumberSystem == "oct")
    370                     textBoxResult.Text = DecToOct((int)_num1);
    371             }
    372 
    373             //使输入焦点落到编辑框textBoxResult中
    374             textBoxResult.Focus();
    375             //让光标落在编辑框textBoxResult的最后面
    376             textBoxResult.Select(textBoxResult.Text.Length, 0);
    377         }
    378 
    379         //按钮"-"的Click事件响应方法
    380         private void buttonSubtract_Click(object sender, EventArgs e)
    381         {
    382             //首数字标志位设为true
    383             _firstNumberFlag = true;
    384 
    385             //运算符号数量加1
    386             _operatorCount = _operatorCount + 1;
    387             //判断是否为第一个运算符
    388             if (_operatorCount == 1)
    389             {
    390                 //保存第一个运算数到类字段变量_num1中(注意进制转化问题)
    391                 if (_NumberSystem == "dec")
    392                     _num1 = Convert.ToDecimal(textBoxResult.Text);
    393                 else if (_NumberSystem == "bin")
    394                     _num1 = BinToDec(textBoxResult.Text);
    395                 else if (_NumberSystem == "oct")
    396                     _num1 = OctToDec(textBoxResult.Text);
    397                 //保存运算符号到类字段变量_operator中
    398                 _operator = "-";
    399                 //清空编辑框 
    400                 textBoxResult.Text = "";
    401             }
    402             else
    403             {
    404                 //保存第二个运算数到类字段变量_num2中(注意进制转化问题)
    405                 if (_NumberSystem == "dec")
    406                     _num2 = Convert.ToDecimal(textBoxResult.Text);
    407                 else if (_NumberSystem == "bin")
    408                     _num2 = BinToDec(textBoxResult.Text);
    409                 else if (_NumberSystem == "oct")
    410                     _num2 = OctToDec(textBoxResult.Text);
    411                 //进行运算
    412                 if (_operator == "+")
    413                     _num1 = _num1 + _num2;
    414                 else if (_operator == "-")
    415                     _num1 = _num1 - _num2;
    416                 else if (_operator == "*")
    417                     _num1 = _num1 * _num2;
    418                 else if (_operator == "/")
    419                     _num1 = _num1 / _num2;
    420                 //保存运算符号到类字段变量_operator中
    421                 _operator = "-";
    422                 //在编辑框中显示前面数字的运算结果(注意进制转化问题) 
    423                 if (_NumberSystem == "dec")
    424                     textBoxResult.Text = _num1.ToString();
    425                 else if (_NumberSystem == "bin")
    426                     textBoxResult.Text = DecToBin((int)_num1);
    427                 else if (_NumberSystem == "oct")
    428                     textBoxResult.Text = DecToOct((int)_num1);
    429             }
    430 
    431             //使输入焦点落到编辑框textBoxResult中
    432             textBoxResult.Focus();
    433             //让光标落在编辑框textBoxResult的最后面
    434             textBoxResult.Select(textBoxResult.Text.Length, 0);
    435         }
    436 
    437         //按钮"*"的Click事件响应方法
    438         private void buttonMultiply_Click(object sender, EventArgs e)
    439         {
    440             //首数字标志位设为true
    441             _firstNumberFlag = true;
    442 
    443             //运算符号数量加1
    444             _operatorCount = _operatorCount + 1;
    445             //判断是否为第一个运算符
    446             if (_operatorCount == 1)
    447             {
    448                 //保存第一个运算数到类字段变量_num1中(注意进制转化问题)
    449                 if (_NumberSystem == "dec")
    450                     _num1 = Convert.ToDecimal(textBoxResult.Text);
    451                 else if (_NumberSystem == "bin")
    452                     _num1 = BinToDec(textBoxResult.Text);
    453                 else if (_NumberSystem == "oct")
    454                     _num1 = OctToDec(textBoxResult.Text);
    455                 //保存运算符号到类字段变量_operator中
    456                 _operator = "*";
    457                 //清空编辑框 
    458                 textBoxResult.Text = "";
    459             }
    460             else
    461             {
    462                 //保存第二个运算数到类字段变量_num2中(注意进制转化问题)
    463                 if (_NumberSystem == "dec")
    464                     _num2 = Convert.ToDecimal(textBoxResult.Text);
    465                 else if (_NumberSystem == "bin")
    466                     _num2 = BinToDec(textBoxResult.Text);
    467                 else if (_NumberSystem == "oct")
    468                     _num2 = OctToDec(textBoxResult.Text);
    469                 //进行运算
    470                 if (_operator == "+")
    471                     _num1 = _num1 + _num2;
    472                 else if (_operator == "-")
    473                     _num1 = _num1 - _num2;
    474                 else if (_operator == "*")
    475                     _num1 = _num1 * _num2;
    476                 else if (_operator == "/")
    477                     _num1 = _num1 / _num2;
    478                 //保存运算符号到类字段变量_operator中
    479                 _operator = "*";
    480                 //在编辑框中显示前面数字的运算结果(注意进制转化问题) 
    481                 if (_NumberSystem == "dec")
    482                     textBoxResult.Text = _num1.ToString();
    483                 else if (_NumberSystem == "bin")
    484                     textBoxResult.Text = DecToBin((int)_num1);
    485                 else if (_NumberSystem == "oct")
    486                     textBoxResult.Text = DecToOct((int)_num1);
    487             }
    488 
    489             //使输入焦点落到编辑框textBoxResult中
    490             textBoxResult.Focus();
    491             //让光标落在编辑框textBoxResult的最后面
    492             textBoxResult.Select(textBoxResult.Text.Length, 0);
    493         }
    494 
    495         //按钮"/"的Click事件响应方法
    496         private void buttonDivide_Click(object sender, EventArgs e)
    497         {
    498             //首数字标志位设为true
    499             _firstNumberFlag = true;
    500 
    501             //运算符号数量加1
    502             _operatorCount = _operatorCount + 1;
    503             //判断是否为第一个运算符
    504             if (_operatorCount == 1)
    505             {
    506                 //保存第一个运算数到类字段变量_num1中(注意进制转化问题)
    507                 if (_NumberSystem == "dec")
    508                     _num1 = Convert.ToDecimal(textBoxResult.Text);
    509                 else if (_NumberSystem == "bin")
    510                     _num1 = BinToDec(textBoxResult.Text);
    511                 else if (_NumberSystem == "oct")
    512                     _num1 = OctToDec(textBoxResult.Text);
    513                 //保存运算符号到类字段变量_operator中
    514                 _operator = "/";
    515                 //清空编辑框 
    516                 textBoxResult.Text = "";
    517             }
    518             else
    519             {
    520                 //保存第二个运算数到类字段变量_num2中(注意进制转化问题)
    521                 if (_NumberSystem == "dec")
    522                     _num2 = Convert.ToDecimal(textBoxResult.Text);
    523                 else if (_NumberSystem == "bin")
    524                     _num2 = BinToDec(textBoxResult.Text);
    525                 else if (_NumberSystem == "oct")
    526                     _num2 = OctToDec(textBoxResult.Text);
    527                 //进行运算
    528                 if (_operator == "+")
    529                     _num1 = _num1 + _num2;
    530                 else if (_operator == "-")
    531                     _num1 = _num1 - _num2;
    532                 else if (_operator == "*")
    533                     _num1 = _num1 * _num2;
    534                 else if (_operator == "/")
    535                     _num1 = _num1 / _num2;
    536                 //保存运算符号到类字段变量_operator中
    537                 _operator = "/";
    538                 //在编辑框中显示前面数字的运算结果(注意进制转化问题) 
    539                 if (_NumberSystem == "dec")
    540                     textBoxResult.Text = _num1.ToString();
    541                 else if (_NumberSystem == "bin")
    542                     textBoxResult.Text = DecToBin((int)_num1);
    543                 else if (_NumberSystem == "oct")
    544                     textBoxResult.Text = DecToOct((int)_num1);
    545             }
    546 
    547             //使输入焦点落到编辑框textBoxResult中
    548             textBoxResult.Focus();
    549             //让光标落在编辑框textBoxResult的最后面
    550             textBoxResult.Select(textBoxResult.Text.Length, 0);
    551         }
    552 
    553         //按钮"="的Click事件响应方法
    554         private void buttonEqual_Click(object sender, EventArgs e)
    555         {
    556             //保存第二个运算数到类字段变量_num2中(注意进制转化问题)
    557             if (_NumberSystem == "dec")
    558                 _num2 = Convert.ToDecimal(textBoxResult.Text);
    559             else if (_NumberSystem == "bin")
    560                 _num2 = BinToDec(textBoxResult.Text);
    561             else if (_NumberSystem == "oct")
    562                 _num2 = OctToDec(textBoxResult.Text);
    563             //进行运算
    564             if (_operator == "+")
    565                 _result = _num1 + _num2;
    566             else if (_operator == "-")
    567                 _result = _num1 - _num2;
    568             else if (_operator == "*")
    569                 _result = _num1 * _num2;
    570             else if (_operator == "/")
    571                 _result = _num1 / _num2;
    572             //在编辑框中显示运算结果(注意进制转化问题)
    573             if (_NumberSystem == "dec")
    574                 textBoxResult.Text = _result.ToString();
    575             else if (_NumberSystem == "bin")
    576                 textBoxResult.Text = DecToBin((int)_result);
    577             else if (_NumberSystem == "oct")
    578                 textBoxResult.Text = DecToOct((int)_result);
    579 
    580             //运算符号数量清零
    581             _operatorCount = 0;
    582 
    583             //使输入焦点落到编辑框textBoxResult中
    584             textBoxResult.Focus();
    585             //让光标落在编辑框textBoxResult的最后面
    586             textBoxResult.Select(textBoxResult.Text.Length, 0);
    587         }
    588 
    589         //按钮"<-"的Click事件响应方法
    590         private void buttonBackSpace_Click(object sender, EventArgs e)
    591         {
    592             //如果textBoxResult.Text不为空
    593             if (textBoxResult.Text.Length > 0)
    594                 //去除textBoxResult.Text中的最后一个字符
    595                 textBoxResult.Text = textBoxResult.Text.Substring(0, textBoxResult.Text.Length - 1);
    596 
    597             //使输入焦点落到编辑框textBoxResult中
    598             textBoxResult.Focus();
    599         }
    600 
    601         //按钮"C"的Click事件响应方法
    602         private void buttonClear_Click(object sender, EventArgs e)
    603         {
    604             //初始化有关类字段变量
    605             _num1 = 0;
    606             _num2 = 0;
    607             _result = 0;
    608             _operator = "";
    609             _operatorCount = 0;
    610             _firstNumberFlag = false;
    611             //清空编辑框
    612             textBoxResult.Text = "";
    613 
    614             //使输入焦点落到编辑框textBoxResult中
    615             textBoxResult.Focus();
    616             //让光标落在编辑框textBoxResult的最后面
    617             textBoxResult.Select(textBoxResult.Text.Length, 0);
    618         }
    619 
    620         //按钮"+/-"的Click事件响应方法
    621        
    622 
    623         //编辑框的KeyPress(键盘下压)事件的响应方法
    624         private void textBoxResult_KeyPress(object sender, KeyPressEventArgs e)
    625         {
    626             //根据用户按下的键的值,调用相应的响应方法
    627             if (e.KeyChar == '0')
    628                 buttonNum0_Click(sender, e);
    629             else if (e.KeyChar == '1')
    630                 buttonNum1_Click(sender, e);
    631             else if (e.KeyChar == '2')
    632                 buttonNum2_Click(sender, e);
    633             else if (e.KeyChar == '3')
    634                 buttonNum3_Click(sender, e);
    635             else if (e.KeyChar == '4')
    636                 buttonNum4_Click(sender, e);
    637             else if (e.KeyChar == '5')
    638                 buttonNum5_Click(sender, e);
    639             else if (e.KeyChar == '6')
    640                 buttonNum6_Click(sender, e);
    641             else if (e.KeyChar == '7')
    642                 buttonNum7_Click(sender, e);
    643             else if (e.KeyChar == '8')
    644                 buttonNum8_Click(sender, e);
    645             else if (e.KeyChar == '9')
    646                 buttonNum9_Click(sender, e);
    647 
    648             else if (e.KeyChar == '+')
    649                 buttonAdd_Click(sender, e);
    650             else if (e.KeyChar == '-')
    651                 buttonSubtract_Click(sender, e);
    652             else if (e.KeyChar == '*')
    653                 buttonMultiply_Click(sender, e);
    654             else if (e.KeyChar == '/')
    655                 buttonDivide_Click(sender, e);
    656             else if (e.KeyChar == '=')
    657                 buttonEqual_Click(sender, e);
    658 
    659             else if (e.KeyChar == 'c')
    660                 buttonClear_Click(sender, e);
    661             else if (e.KeyChar == '.')
    662                 buttonDot_Click(sender, e);
    663 
    664             else if (e.KeyChar == 8)             //或者 (e.KeyChar == '')   或者 (e.KeyChar == (char)Keys.Back)
    665                 buttonBackSpace_Click(sender, e);
    666 
    667             //让光标落在编辑框textBoxResult的最后面
    668             textBoxResult.Select(textBoxResult.Text.Length, 0);
    669         }
    670 
    671 
    672 
    673 
    674 
    675         private void radioButtonOctal_CheckedChanged(object sender, EventArgs e)
    676         {
    677 
    678         }
    679 
    680         private void Form1_Load(object sender, EventArgs e)
    681         {
    682 
    683         }
    684 
    685         private void radioButtonDecimal_CheckedChanged(object sender, EventArgs e)
    686         {
    687 
    688         }
    689 
    690         private void radioButtonBinary_CheckedChanged(object sender, EventArgs e)
    691         {
    692 
    693             //进行进制转换
    694             if (textBoxResult.Text != "")
    695             {
    696                 if (_NumberSystem == "dec")
    697                     textBoxResult.Text = DecToBin((int)Math.Round(Convert.ToDecimal(textBoxResult.Text)));
    698                 else if (_NumberSystem == "oct")
    699                 {
    700                     int dec = OctToDec(textBoxResult.Text);
    701                     textBoxResult.Text = DecToBin(dec);
    702                 }
    703             }
    704 
    705             //启用或禁用有关按钮
    706             buttonNum2.Enabled = false;
    707             buttonNum3.Enabled = false;
    708             buttonNum4.Enabled = false;
    709             buttonNum5.Enabled = false;
    710             buttonNum6.Enabled = false;
    711             buttonNum7.Enabled = false;
    712             buttonNum8.Enabled = false;
    713             buttonNum9.Enabled = false;
    714             buttonDot.Enabled = false;
    715 
    716             //保存进制标志
    717             _NumberSystem = "bin";
    718         }
    719 
    720         private void radioButtonDecimal_Click(object sender, EventArgs e)
    721         {
    722             //进行进制转换
    723             if (textBoxResult.Text != "")
    724             {
    725                 if (_NumberSystem == "bin")
    726                     textBoxResult.Text = BinToDec(textBoxResult.Text).ToString();
    727                 else if (_NumberSystem == "oct")
    728                     textBoxResult.Text = OctToDec(textBoxResult.Text).ToString();
    729             }
    730 
    731             //启用或禁用有关按钮
    732             buttonNum2.Enabled = true;
    733             buttonNum3.Enabled = true;
    734             buttonNum4.Enabled = true;
    735             buttonNum5.Enabled = true;
    736             buttonNum6.Enabled = true;
    737             buttonNum7.Enabled = true;
    738             buttonNum8.Enabled = true;
    739             buttonNum9.Enabled = true;
    740             buttonDot.Enabled = true;
    741 
    742             //保存进制标志
    743             _NumberSystem = "dec";
    744         }
    745 
    746         private void radioButtonOctal_Click(object sender, EventArgs e)
    747         {
    748             //进行进制转换
    749             if (textBoxResult.Text != "")
    750             {
    751                 if (_NumberSystem == "dec")
    752                     textBoxResult.Text = DecToOct((int)Math.Round(Convert.ToDecimal(textBoxResult.Text)));
    753                 else if (_NumberSystem == "bin")
    754                 {
    755                     int dec = BinToDec(textBoxResult.Text);
    756                     textBoxResult.Text = DecToOct(dec);
    757                 }
    758             }
    759 
    760             //启用或禁用有关按钮
    761             buttonNum2.Enabled = true;
    762             buttonNum3.Enabled = true;
    763             buttonNum4.Enabled = true;
    764             buttonNum5.Enabled = true;
    765             buttonNum6.Enabled = true;
    766             buttonNum7.Enabled = true;
    767             buttonNum8.Enabled = false;
    768             buttonNum9.Enabled = false;
    769             buttonDot.Enabled = false;
    770 
    771             //保存进制标志
    772             _NumberSystem = "oct";
    773         }
    774 
    775 
    776     }
    777 }
    View Code
  • 相关阅读:
    2.0 C++远征:隐藏
    1.0 C++远征:为什么继承
    15.0 C++远征:常指针和常引用
    14.0 C++远征:常对象成员和常成员函数
    13.0 C++远征:this指针
    12.0 C++远征:对象成员指针
    Sql语句
    斐波那契 递归 求某一个数
    WebAPI 和 WebService的区别
    获取用户IP
  • 原文地址:https://www.cnblogs.com/caiyishuai/p/13271195.html
Copyright © 2020-2023  润新知