• 简单计算器


    编写工具:c#

    主要cs文件

      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.Threading.Tasks;
      9 using System.Windows.Forms;
     10 
     11 namespace calculator
     12 {
     13     public partial class Form1 : Form
     14     {
     15         public Form1()
     16         {
     17             InitializeComponent();
     18         }
     19         
     20         int numberxiabiao_int = 0;//数字集合下标
     21         int operatorxiabiao_int = 0;//运算符下标
     22         double[] number_double = new double[10];//数字集合
     23         string[] operator_string = new string[10];//运算符集合
     24 
     25         public void numclik(int num)
     26         {//输入数字
     27                 if (textBox1.Text == "0")
     28                 {
     29                     //输入的是整数第一个数字
     30                     textBox1.Text = num + "";
     31                 }
     32                 else
     33                 {
     34                     //输入的不是整数的第一个数字
     35                     textBox1.Text = textBox1.Text+ num + "";
     36                 }
     37         }
     38         private void button3_Click(object sender, EventArgs e)
     39         {
     40             //按钮 1
     41             numclik(1);
     42         }
     43 
     44         private void button6_Click(object sender, EventArgs e)
     45         {
     46             //按钮 2
     47             numclik(2);
     48         }
     49 
     50         private void button9_Click(object sender, EventArgs e)
     51         {
     52             //按钮 3
     53             numclik(3);
     54         }
     55 
     56         private void button2_Click(object sender, EventArgs e)
     57         {
     58             //按钮 4
     59             numclik(4);
     60         }
     61 
     62         private void button5_Click(object sender, EventArgs e)
     63         {
     64             //按钮 5
     65             numclik(5);
     66         }
     67 
     68         private void button8_Click(object sender, EventArgs e)
     69         {
     70             //按钮 6
     71             numclik(6);
     72         }
     73 
     74         private void button1_Click(object sender, EventArgs e)
     75         {
     76             //按钮 7
     77             numclik(7);
     78         }
     79 
     80         private void button4_Click(object sender, EventArgs e)
     81         {
     82             //按钮 8
     83             numclik(8);
     84         }
     85 
     86         private void button7_Click(object sender, EventArgs e)
     87         {
     88             //按钮 9
     89             numclik(9);
     90         }
     91 
     92         private void button10_Click(object sender, EventArgs e)
     93         {
     94             //按钮 0
     95             numclik(0);
     96         }
     97 
     98         private void button11_Click(object sender, EventArgs e)
     99         {
    100             //按钮 点
    101             string a = textBox1.Text;
    102             if (a.Length > 1)//如果长度大于1进行判断,这判断出现的原因是为了不让下边截取字符串部分下标不少于0
    103             {
    104                 string b = a.Substring(a.Length - 1);
    105                 string c = "+";
    106                 string d = "-";
    107                 string h = "*";
    108                 string f = "/";
    109                 string g = ".";
    110                 if (b != c && b != d && b != h && b != f && b != g)//不能连续输入运算符或者小数点
    111                 {
    112                     textBox1.Text = textBox1.Text + ".";
    113                 }
    114             }
    115             else
    116             {
    117                 if (textBox1.Text != "")
    118                 {
    119                     textBox1.Text = textBox1.Text + ".";
    120                 }
    121             }
    122         }
    123 
    124         private void button12_Click(object sender, EventArgs e)
    125         {
    126             //按钮 +
    127             string a = textBox1.Text;
    128             if (a.Length > 1)//如果长度大于1进行判断,这判断出现的原因是为了不让下边截取字符串部分下标不少于0
    129             {
    130                 string b = a.Substring(a.Length - 1);
    131                 string c = "+";
    132                 string d = "-";
    133                 string h = "*";
    134                 string f = "/";
    135                 string g = ".";
    136                 if (b != c && b != d && b != h && b != f && b != g)//不能连续输入运算符或者小数点
    137                 {
    138                     textBox1.Text = textBox1.Text + "+";
    139                 }
    140             }
    141             else
    142             {
    143                 if (textBox1.Text != "")
    144                 {
    145                     textBox1.Text = textBox1.Text + "+";
    146                 }
    147             }
    148         }
    149 
    150         private void button13_Click(object sender, EventArgs e)
    151         {
    152             //按钮 -
    153             string a = textBox1.Text;
    154             if (a.Length > 1)//如果长度大于3进行判断,这判断出现的原因是为了不让下边截取字符串部分下标不少于0
    155             {
    156                 string b = a.Substring(a.Length - 1);
    157                 string c = "+";
    158                 string d = "-";
    159                 string h = "*";
    160                 string f = "/";
    161                 string g = ".";
    162                 if (b != c && b != d && b != h && b != f && b != g)//不能连续输入乘号或者
    163                 {
    164                     textBox1.Text = textBox1.Text + "-";
    165                 }
    166             }
    167             else
    168             {
    169                 if (textBox1.Text != "")
    170                 {
    171                     textBox1.Text = textBox1.Text + "-";
    172                 }
    173             }
    174         }
    175 
    176         private void button14_Click(object sender, EventArgs e)
    177         {
    178             //按钮 *
    179             string a = textBox1.Text;
    180             if (a.Length > 1)//如果长度大于3进行判断,这判断出现的原因是为了不让下边截取字符串部分下标不少于0
    181             {
    182                 string b = a.Substring(a.Length - 1);
    183                 string c = "+";
    184                 string d = "-";
    185                 string h = "*";
    186                 string f = "/";
    187                 string g = ".";
    188                 if (b != c && b != d && b != h && b != f && b != g)//不能连续输入乘号或者
    189                 {
    190                     textBox1.Text = textBox1.Text + "*";
    191                 }
    192             }
    193             else
    194             {
    195                 if (textBox1.Text != "")
    196                 {
    197                     textBox1.Text = textBox1.Text + "*";
    198                 }
    199             }
    200         }
    201 
    202         private void button15_Click(object sender, EventArgs e)
    203         {
    204             //按钮 /
    205             string a = textBox1.Text;
    206             if (a.Length > 1)//如果长度大于3进行判断,这判断出现的原因是为了不让下边截取字符串部分下标不少于0
    207             {
    208                 string b = a.Substring(a.Length - 1);
    209                 string c = "+";
    210                 string d = "-";
    211                 string h = "*";
    212                 string f = "/";
    213                 string g = ".";
    214                 if (b != c && b != d && b != h && b != f && b != g)//不能连续输入乘号或者
    215                 {
    216                     textBox1.Text = textBox1.Text + "/";
    217                 }
    218             }
    219             else
    220             {
    221                 if (textBox1.Text != "")
    222                 {
    223                     textBox1.Text = textBox1.Text + "/";
    224                 }
    225             }
    226         }
    227 
    228         private void button16_Click(object sender, EventArgs e)
    229         {
    230             //按钮 清除一位
    231             if (textBox1.Text != "")
    232             {
    233                 textBox1.Text = textBox1.Text.Substring(0, textBox1.Text.Length - 1);
    234             }
    235         }
    236 
    237         private void button17_Click(object sender, EventArgs e)
    238         {
    239             //按钮 清除所有内容重新开始
    240             textBox1.Text = "0";//显示为零
    241             numberxiabiao_int = 0;
    242             operatorxiabiao_int = 0;//两个记录数组都清空,清空的方式就是把数组的下标重置,而不是重置数组,严格来说也不能
    243         }
    244 
    245         private void button18_Click(object sender, EventArgs e)
    246         {
    247             //按钮 =
    248             string a = textBox1.Text;
    249             int i = 0;//定义一个总的字符串下标
    250             while (a != "" && i < a.Length)//将字符串分为数字还有符号
    251             {
    252                 string b = "";
    253                 while (!a[i].Equals('+') && !a[i].Equals('-') && !a[i].Equals('*') && !a[i].Equals('/') && i < a.Length)//这个循环的出现是为了过滤多位数字,还有小数
    254                 {   //一直把向后叠加,直到后面一位是运算符,这样才能完全统计这个数字
    255                     b = b + a[i];
    256                     i++; 
    257                     if (i < a.Length)
    258                     {
    259                         if (a[i].Equals('+') || a[i].Equals('-') || a[i].Equals('*') || a[i].Equals('/'))//如果下一个字符是运算符,就存储数字
    260                         {
    261                             number_double[numberxiabiao_int] = Convert.ToDouble(b);
    262                             numberxiabiao_int++;
    263                             break;
    264                         }
    265                     }
    266                     else
    267                     {
    268                         number_double[numberxiabiao_int] = Convert.ToDouble(b);
    269                         numberxiabiao_int++;
    270                         break;
    271                     }
    272                 }
    273 
    274                 if (i == a.Length)
    275                 {
    276                     break; 
    277                 }
    278                 else
    279                 {
    280                     operator_string[operatorxiabiao_int] = a[i] + "";
    281                     i++;
    282                     operatorxiabiao_int++;
    283                 }
    284             }
    285             if (numberxiabiao_int == 2)//有两个数字的情况
    286             {
    287                 if (operator_string[operatorxiabiao_int-1] == "+")
    288                 {
    289                     double result = number_double[0] + number_double[1];
    290                     numberxiabiao_int = 0;
    291                     operatorxiabiao_int = 0;
    292                     textBox1.Text = result + "";
    293                 }
    294                 else if (operator_string[operatorxiabiao_int-1] == "-")
    295                 {
    296                     double result = number_double[0] - number_double[1];
    297                     numberxiabiao_int = 0;
    298                     operatorxiabiao_int = 0;
    299                     textBox1.Text = result + "";
    300                 }
    301                 else if (operator_string[operatorxiabiao_int-1] == "*")
    302                 {
    303                     double result = number_double[0] * number_double[1];
    304                     numberxiabiao_int = 0;
    305                     operatorxiabiao_int = 0;
    306                     textBox1.Text = result + "";
    307                 }
    308                 else if (operator_string[operatorxiabiao_int-1] == "/")
    309                 {
    310                     double result = number_double[0] / number_double[1];
    311                     numberxiabiao_int = 0;
    312                     operatorxiabiao_int = 0;
    313                     textBox1.Text = result + "";
    314                 }
    315             }
    316             else if (numberxiabiao_int == 3)//有三个数字的情况
    317             {
    318                 string operator1 = operator_string[0];
    319                 string operator2 = operator_string[1];
    320                 if (operator1 == "+")
    321                 {
    322                     if (operator2 == "+")
    323                     {
    324                         double result = number_double[0] + number_double[1] + number_double[2];
    325                         numberxiabiao_int = 0;
    326                         operatorxiabiao_int = 0;
    327                         textBox1.Text = result + "";
    328                     }
    329                     else if (operator2 == "-")
    330                     {
    331                         double result = number_double[0] + number_double[1] - number_double[2];
    332                         numberxiabiao_int = 0;
    333                         operatorxiabiao_int = 0;
    334                         textBox1.Text = result + "";
    335                     }
    336                     else if (operator2 == "*")
    337                     {
    338                         double result = number_double[0] + number_double[1] * number_double[2];
    339                         numberxiabiao_int = 0;
    340                         operatorxiabiao_int = 0;
    341                         textBox1.Text = result + "";
    342                     }
    343                     else if (operator2 == "/")
    344                     {
    345                         double result = number_double[0] + number_double[1] / number_double[2];
    346                         numberxiabiao_int = 0;
    347                         operatorxiabiao_int = 0;
    348                         textBox1.Text = result + "";
    349                     }
    350                 }
    351                 else if (operator1 == "-")
    352                 {
    353                     if (operator2 == "+")
    354                     {
    355                         double result = number_double[0] - number_double[1] + number_double[2];
    356                         numberxiabiao_int = 0;
    357                         operatorxiabiao_int = 0;
    358                         textBox1.Text = result + "";
    359                     }
    360                     else if (operator2 == "-")
    361                     {
    362                         double result = number_double[0] - number_double[1] - number_double[2];
    363                         numberxiabiao_int = 0;
    364                         operatorxiabiao_int = 0;
    365                         textBox1.Text = result + "";
    366                     }
    367                     else if (operator2 == "*")
    368                     {
    369                         double result = number_double[0] - number_double[1] * number_double[2];
    370                         numberxiabiao_int = 0;
    371                         operatorxiabiao_int = 0;
    372                         textBox1.Text = result + "";
    373                     }
    374                     else if (operator2 == "/")
    375                     {
    376                         double result = number_double[0] - number_double[1] / number_double[2];
    377                         numberxiabiao_int = 0;
    378                         operatorxiabiao_int = 0;
    379                         textBox1.Text = result + "";
    380                     }
    381                 }
    382                 else if (operator1 == "*")
    383                 {
    384                     if (operator2 == "+")
    385                     {
    386                         double result = number_double[0] * number_double[1] + number_double[2];
    387                         numberxiabiao_int = 0;
    388                         operatorxiabiao_int = 0;
    389                         textBox1.Text = result + "";
    390                     }
    391                     else if (operator2 == "-")
    392                     {
    393                         double result = number_double[0] * number_double[1] - number_double[2];
    394                         numberxiabiao_int = 0;
    395                         operatorxiabiao_int = 0;
    396                         textBox1.Text = result + "";
    397                     }
    398                     else if (operator2 == "*")
    399                     {
    400                         double result = number_double[0] * number_double[1] * number_double[2];
    401                         numberxiabiao_int = 0;
    402                         operatorxiabiao_int = 0;
    403                         textBox1.Text = result + "";
    404                     }
    405                     else if (operator2 == "/")
    406                     {
    407                         double result = number_double[0] * number_double[1] / number_double[2];
    408                         numberxiabiao_int = 0;
    409                         operatorxiabiao_int = 0;
    410                         textBox1.Text = result + "";
    411                     }
    412                 }
    413                 else if (operator1 == "/")
    414                 {
    415                     if (operator2 == "+")
    416                     {
    417                         double result = number_double[0] / number_double[1] + number_double[2];
    418                         numberxiabiao_int = 0;
    419                         operatorxiabiao_int = 0;
    420                         textBox1.Text = result + "";
    421                     }
    422                     else if (operator2 == "-")
    423                     {
    424                         double result = number_double[0] / number_double[1] - number_double[2];
    425                         numberxiabiao_int = 0;
    426                         operatorxiabiao_int = 0;
    427                         textBox1.Text = result + "";
    428                     }
    429                     else if (operator2 == "*")
    430                     {
    431                         double result = number_double[0] / number_double[1] * number_double[2];
    432                         numberxiabiao_int = 0;
    433                         operatorxiabiao_int = 0;
    434                         textBox1.Text = result + "";
    435                     }
    436                     else if (operator2 == "/")
    437                     {
    438                         double result = number_double[0] / number_double[1] / number_double[2];
    439                         numberxiabiao_int = 0;
    440                         operatorxiabiao_int = 0;
    441                         textBox1.Text = result + "";
    442                     }
    443                 }
    444             }
    445             numberxiabiao_int = 0;
    446             operatorxiabiao_int = 0;//两个记录数组都清空
    447         }
    448 
    449         private void button19_Click(object sender, EventArgs e)
    450         {
    451             //按钮 开根号
    452             string b = textBox1.Text;//一个简单的承接,把显示的字符串
    453             string c = "";
    454             string c2 = "";
    455             double c1 = 0;
    456             int n = 0;
    457             bool flag = true;//用来判断是不是只有一个数字
    458             for (int j = 0; j < b.Length; j++)
    459             {
    460                 if (b[j].Equals('+') || b[j].Equals('-') || b[j].Equals('*') || b[j].Equals('/'))
    461                 {
    462                     flag = false;
    463                     break;
    464                 }
    465             }
    466             if (flag == false)
    467             {
    468                 if (!b[b.Length - 1].Equals('+') && !b[b.Length - 1].Equals('-') && !b[b.Length - 1].Equals('*') && !b[b.Length - 1].Equals('/'))//只能是最后一个字符为数字是才能执行
    469                 {
    470                     for (int k = b.Length - 1; k > -1; k--)
    471                     {
    472                         if (b[k].Equals('+') || b[k].Equals('-') || b[k].Equals('*') || b[k].Equals('/'))
    473                         {
    474                             n = k;
    475                             break;
    476                         }
    477                     }
    478                 }
    479                 for (int g = n; g < b.Length; g++)
    480                 {
    481                     c += b[g];
    482                 }
    483                 c1 = Convert.ToDouble(c);
    484                 for (int g = 0; g < n + 1; g++)
    485                 {
    486                     c2 += b[g];
    487                 }
    488                 textBox1.Text = c2 + Math.Sqrt(c1);
    489             }
    490             else
    491             {
    492                 textBox1.Text = Math.Sqrt(Convert.ToDouble(b)) + "";
    493             }
    494        
    495         }
    496 
    497         private void button20_Click(object sender, EventArgs e)
    498         {
    499             //按钮 求倒数
    500             string b = textBox1.Text;//一个简单的承接,把显示的字符串
    501             string c = "";
    502             string c2 = "";
    503             double c1 = 0;
    504             int n = 0;
    505             bool flag = true;//用来判断是不是只有一个数字
    506             for (int j = 0; j < b.Length; j++)
    507             {
    508                 if (b[j].Equals('+') || b[j].Equals('-') || b[j].Equals('*') || b[j].Equals('/'))
    509                 {
    510                     flag = false;
    511                     break;
    512                 }
    513             }
    514             if (flag == false)
    515             {
    516                 if (!b[b.Length - 1].Equals('+') && !b[b.Length - 1].Equals('-') && !b[b.Length - 1].Equals('*') && !b[b.Length - 1].Equals('/'))//只能是最后一个字符为数字是才能执行
    517                 {
    518                     for (int k = b.Length - 1; k > -1; k--)
    519                     {
    520                         if (b[k].Equals('+') || b[k].Equals('-') || b[k].Equals('*') || b[k].Equals('/'))
    521                         {
    522                             n = k;
    523                             break;
    524                         }
    525                     }
    526                 }
    527                 for (int g = n; g < b.Length; g++)
    528                 {
    529                     c += b[g];
    530                 }
    531                 c1 = Convert.ToDouble(c);
    532                 for (int g = 0; g < n + 1; g++)
    533                 {
    534                     c2 += b[g];
    535                 }
    536                 textBox1.Text = c2 + 1/c1;
    537             }
    538             else
    539             {
    540                 textBox1.Text = 1/Convert.ToDouble(b) + "";
    541             }
    542 
    543         }
    544 
    545         private void button21_Click(object sender, EventArgs e)
    546         {
    547             //按钮 求倒数
    548             string b = textBox1.Text;//一个简单的承接,把显示的字符串
    549             string c = "";
    550             string c2 = "";
    551             double c1 = 0;
    552             int n = 0;
    553             bool flag = true;//用来判断是不是只有一个数字
    554             for (int j = 0; j < b.Length; j++)
    555             {
    556                 if (b[j].Equals('+') || b[j].Equals('-') || b[j].Equals('*') || b[j].Equals('/'))
    557                 {
    558                     flag = false;
    559                     break;
    560                 }
    561             }
    562             if (flag == false)
    563             {
    564                 if (!b[b.Length - 1].Equals('+') && !b[b.Length - 1].Equals('-') && !b[b.Length - 1].Equals('*') && !b[b.Length - 1].Equals('/'))//只能是最后一个字符为数字是才能执行
    565                 {
    566                     for (int k = b.Length - 1; k > -1; k--)
    567                     {
    568                         if (b[k].Equals('+') || b[k].Equals('-') || b[k].Equals('*') || b[k].Equals('/'))
    569                         {
    570                             n = k;
    571                             break;
    572                         }
    573                     }
    574                 }
    575                 for (int g = n; g < b.Length; g++)
    576                 {
    577                     c += b[g];
    578                 }
    579                 c1 = Convert.ToDouble(c);
    580                 for (int g = 0; g < n + 1; g++)
    581                 {
    582                     c2 += b[g];
    583                 }
    584                 textBox1.Text = c2 + Math.Sqrt(c1);
    585             }
    586             else
    587             {
    588                 textBox1.Text = Math.Sqrt(Convert.ToDouble(b)) + "";
    589             }
    590 
    591         }
    592 
    593         private void button22_Click(object sender, EventArgs e)
    594         {
    595             //按钮 帮助
    596             new Form2().Show();
    597         }
    598 
    599         private void Form1_Load(object sender, EventArgs e)
    600         {
    601             textBox1.Text = "0";//让其一开始显示零
    602             textBox1.TextAlign = HorizontalAlignment.Right;//从右向左输入格式
    603         }
    604 
    605         private void textBox1_TextChanged(object sender, EventArgs e)
    606         {
    607 
    608         }
    609     }
    610 }
    View Code

    代码整体思路介绍:

    本程序的核心思想就是显示还有运算进行分离,先进性显示比如输入3+3*3

    操作顺序为 按钮3 按钮+ 按钮3 按钮* 按钮3 按钮=

    前边五个按钮的工作就是正确显示式子,然后都赋值给textBox1.Text也就是屏幕显示的text文本

    然后在 = 按钮中进行了大量工作,

    第一步将获取的textBox1.Text的值然后分开为数字集合还有运算符号集合

    第二步根据数字集合中数字的数量来判定是两个数字的计算,还是三个数字的计算

    第三步根据数字数组内容还有符号数组内容进行列举式计算

    第四步清空俩个数组,然后输出结果

    注意点:

    1.在程序中创建了两个数组,用来存放数字还有符号

    double[] number_double = new double[10];//数字集合

    string[] operator_string = new string[10];//运算符集合

    int numberxiabiao_int = 0;//数字集合下标

    int operatorxiabiao_int = 0;//运算符下标

      当清空数组的时候是将下标变为0

    2.在进行开根号还有求倒数的计算就是对式子最后一个数字或者只有一个数字的情况进行结果输出例如

    1+4*9的情况 进行 开根号操作 就变成 1+4*3

    9 变成 3

    开根号或者求倒数部分就是如果不止一个数字就将最后一个数字截取出来,然后开根号或者求倒数再放回去

    3.小数按钮只是显示。在分开为数字集合还有符号集合的时候,已经通过遇到运算符才终止本数字的读取的方法来识别了。

    4.关于程序健壮性

    (1)不能连续输入两个运算符,或者小数点。实现部分位置,在运算符还有小数点输入按钮中,通过读取上一个符号是不是运算符或者小于号来实现。

    (2)除法除数不能为0。实现部位位置,数字输入函数public void numclik(int num)

  • 相关阅读:
    题解-FJOI2014 树的重心
    题解-CF1307G Cow and Exercise
    题解-SHOI2005 树的双中心

    【转载】SVN使用教程总结
    Fastcgi、CGI 是什么
    通过js或jq增加的代码,点击事件或其他一些事件不起作用时
    js闭包讲解
    PHP 程序员危机(转载)
    浏览器 User-Agent相关知识
  • 原文地址:https://www.cnblogs.com/sisi-job/p/6117663.html
Copyright © 2020-2023  润新知