winform 简单计算器

今天是周五 马上就要过双休了  闲着无聊 想起来学习的时候还有个计算器没写出来 当时怎么也整不明白 今天就写写试试

 

自己写的计算器挺简单  现在还不能算小数

 

只用两个方法就可以做一个简单的计算器

 

1234数字键的方法

public void shu(string a)
        {

            if (textBox1.Text == "0")
            { textBox1.Text = a; }
            else
            {
                textBox1.Text = textBox1.Text + a;
            }

        }

 

 

加减乘除单独一个方法

调用完suanfa 把texbox1清0

 

 public void suanfa(string c)
        {

            string t1 = textBox1.Text;
            string t2 = textBox2.Text;

            t2 = t2.Substring(t2.Length - 1, 1);

     if (t1 == "0")
            {
                if (t2 != "0")
                {
                    textBox2.Text = textBox2.Text.Substring(0, textBox2.Text.Length - 1) + c;
                    return;
                }
            }
            else if (t2 == "+")
            {
                textBox2.Text = (Convert.ToInt64(textBox2.Text.Substring(0, textBox2.Text.Length - 1)) + Convert.ToInt64(t1)).ToString() + c;
            }
            else if (t2 == "-")
            {
                textBox2.Text = (Convert.ToInt64(textBox2.Text.Substring(0, textBox2.Text.Length - 1)) - Convert.ToInt64(t1)).ToString() + c;
            }

            else if (t2 == "*")
            {
                textBox2.Text = (Convert.ToInt64(textBox2.Text.Substring(0, textBox2.Text.Length - 1)) * Convert.ToInt64(t1)).ToString() + c;
            }
            else if (t2 == "/")
            {
                textBox2.Text = (Convert.ToInt64(textBox2.Text.Substring(0, textBox2.Text.Length - 1)) / Convert.ToInt64(t1)).ToString() + c;
            }
           
            else
            {
                textBox2.Text = t1+c;
            }


        }
       

 

删除键  按钮事件

 

 

if (textBox1.Text.Length >1 )
            {

                textBox1.Text = textBox1.Text.Substring(0, textBox1.Text.Length - 1);
            }
            else
            {
                textBox1.Text = "0";
            }

 

 

等于号  事件

 


            string t1 = textBox1.Text;
            string t2 = textBox2.Text;

            t2 = t2.Substring(t2.Length - 1, 1);
            if (t2 == "+")
            {
               
                textBox1.Text = (Convert.ToInt64(textBox2.Text.Substring(0, textBox2.Text.Length - 1)) + Convert.ToInt64(t1)).ToString();
                textBox2.Text = "0";
            }
            else if (t2 == "-")
            {
               
                textBox1.Text = (Convert.ToInt64(textBox2.Text.Substring(0, textBox2.Text.Length - 1)) - Convert.ToInt64(t1)).ToString();
                textBox2.Text = "0";
            }

            else if (t2 == "*")
            {
              
                textBox1.Text = (Convert.ToInt64(textBox2.Text.Substring(0, textBox2.Text.Length - 1)) * Convert.ToInt64(t1)).ToString();
                textBox2.Text = "0";
            }
            else if (t2 == "/")
            {
               
                textBox1.Text = (Convert.ToInt64(textBox2.Text.Substring(0, textBox2.Text.Length - 1)) / Convert.ToInt64(t1)).ToString();
                textBox2.Text = "0";
            }

            else
            {
               
                textBox1.Text = t1;
                textBox2.Text = "0";
            }

 

 

一个简单的计算器就这么完成了

 

posted @ 2017-07-28 13:53  v587yy  阅读(412)  评论(0编辑  收藏  举报