c# 计算器

作者在 2010-10-15 22:49:46 发布以下内容
  //清空按钮
        private void button5_Click(object sender, EventArgs e)
        {
            textBox1.Text = null;
            numa = 10241024;
            numb = 10241024;
            num = null;

        }


        private void button15_Click(object sender, EventArgs e)
        {//这是按钮1,其他按钮用托管实现
            Button clickBtn;
            clickBtn = (Button)sender;

            button16.Focus();

            textBox1.Text += clickBtn.Text;
            num += clickBtn.Text;

        }





        // 正负号按钮
        private void button19_Click(object sender, EventArgs e)
        {
            Button clickBtn;
            clickBtn = (Button)sender;
            if (num == null)
            {
                textBox1.Text = "-";
                num = textBox1.Text;

            }
            else
            {
                if (textBox1.Text[0] == '-')
                {
                    textBox1.Text = textBox1.Text.Replace("-", null);
                    num = textBox1.Text;
                }
                else
                {
                    textBox1.Text = '-' + textBox1.Text;
                    num = textBox1.Text;
                }
            }
        }



        //  开根号按钮
        private void button6_Click(object sender, EventArgs e)
        {
            numall = Convert.ToDouble(textBox1.Text);
            numall = Math.Sqrt(numall);
            textBox1.Text = Convert.ToString(numall);
            num = textBox1.Text;
        }




        //小数点按钮
        private void button18_Click(object sender, EventArgs e)
        {
            Button clickBtn;
            clickBtn = (Button)sender;
            if (textBox1.Text.IndexOf(".") > -1)
            {
                textBox1.Text = textBox1.Text;
                num += num;

            }
            else
            {
                textBox1.Text += clickBtn.Text;
                num += clickBtn.Text;


            }
        }




        //求倒数按钮
        private void button11_Click(object sender, EventArgs e)
        {
            numall = Convert.ToDouble(textBox1.Text);
            numall = 1.0 / numall;
            textBox1.Text = Convert.ToString(numall);
            num = textBox1.Text;
        }

        //除号
        private void button4_Click(object sender, EventArgs e)
        {
            SendNumber(num);
            if (sign == null || numb == 10241024)
            {
                sign = "/";
                textBox1.Text = null;
                num = null;
                return;
            }
            if (numb != 10241024)
            {
                //开始进行计算
                Computer ComPro = new Computer(sign, numa, numb);
                this.textBox1.Text = ComPro.Comt(out numa).ToString();


                //开始初始化数据
                sign = "/";
                textBox1.Text = null;
                numb = 10241024;
                num = null;
                return;
            }


        }

        //等于号
        private void button16_Click(object sender, EventArgs e)
        {
          
            SendNumber(num);

            //判断是否设置了numb的值
            if (numb == 10241024)
            {
                numb = Convert.ToDouble(num);
                num = null;
            }

            //判断是否设置了numa的值
            if (numa == 10241024)
            {
                numa = Convert.ToDouble(num);
                num = null;
            }

            //判断是否有sign的值
            if (sign == null)
            {
                textBox1.Text = textBox1.Text;
                num = num;
            }
            else
            {
                //开始进行计算
                Computer ComPro = new Computer(sign, numa, numb);
                this.textBox1.Text = ComPro.Comt(out numa).ToString();



                //开始初始化数据
                sign = null;

                numb = 10241024;
                num = null;
            }
        }



        //乘号
        private void button7_Click(object sender, EventArgs e)
        {
            SendNumber(num);
            if (sign == null || numb == 10241024)
            {
                sign = "*";
                textBox1.Text = null;
                num = null;
                return;
            }
            if (numb != 10241024)
            {
                //开始进行计算
                Computer ComPro = new Computer(sign, numa, numb);
                this.textBox1.Text = ComPro.Comt(out numa).ToString();


                //开始初始化数据
                sign = "*";
                textBox1.Text = null;
                numb = 10241024;
                num = null;
                return;
            }
        }

        private void button12_Click(object sender, EventArgs e)
        {
            SendNumber(num);
            if (sign == null || numb == 10241024)
            {
                sign = "-";
                textBox1.Text = null;
                num = null;
                return;
            }
            if (numb != 10241024)
            {
                //开始进行计算
                Computer ComPro = new Computer(sign, numa, numb);
                this.textBox1.Text = ComPro.Comt(out numa).ToString();


                //开始初始化数据
                sign = "-";
                textBox1.Text = null;
                numb = 10241024;
                num = null;
                return;
            }
        }

        private void button17_Click(object sender, EventArgs e)
        {
            SendNumber(num);
            if (sign == null || numb == 10241024)
            {
                sign = "+";
                textBox1.Text = null;
                num = null;
                return;
            }
            if (numb != 10241024)
            {
                //开始进行计算
                Computer ComPro = new Computer(sign, numa, numb);
                this.textBox1.Text = ComPro.Comt(out numa).ToString();


                //开始初始化数据
                sign = "+";
                textBox1.Text = null;
                numb = 10241024;
                num = null;
                return;
            }
        }

        private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void 复制ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Clipboard.SetDataObject(textBox1.SelectedText);

        }

        private void 粘贴ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            IDataObject iData = Clipboard.GetDataObject();


            if (iData.GetDataPresent(DataFormats.Text))
            {

                textBox1.Text = (String)iData.GetData(DataFormats.Text);
            }


        }

        private void 关于ToolStripMenuItem_Click(object sender, EventArgs e)
        {

            MessageBox.Show("班级:******\n姓名:********\n学号:*******");
        }



        private void 帮助文档ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            MessageBox.Show("无需帮助请点确定或关闭哈");
        }

        private void 主页ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            System.Diagnostics.Process.Start("http://www.piikee.net");

        }

      



        //计算类
        public class Computer
        {
            private string sign;
            private double numa;
            private double numb;

            public Computer(string str, double first, double second)
            {
                sign = str;
                numa = first;
                numb = second;
            }

            //进行运算
            public double Comt(out double first)
            {

                if (sign == "+")
                {
                    first = numa + numb;
                    return numa + numb;

                }
                else if (sign == "-")
                {
                    first = numa - numb;
                    return numa - numb;

                }
                else if (sign == "*")
                {
                    first = numa * numb;
                    return numa * numb;

                }
                else if (sign == "/")
                {
                    if (numb == 0)
                    {
                        first = 1024.1024;
                        return first;
                    }
                    else
                    {
                        first = numa / numb;
                        return first;

                    }
                }
                else
                {
                    first = 1024.1024;
                    return first;
                }
            }
默认分类 | 阅读 363 次
文章评论,共2条
tanghf1014
2010-10-17 09:22
1
这个以前也做过报告,不过都忘了。。。
变幻小子
2011-03-20 19:56
2
游客请输入验证码
文章分类
文章归档