码迷,mamicode.com
首页 > 其他好文 > 详细

计算器

时间:2019-01-28 14:03:29      阅读:134      评论:0      收藏:0      [点我收藏+]

标签:box   分享   .data   reading   reset   div   orm   name   space   

技术分享图片

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 Calculator
{
    public partial class FormMain : Form
    {
        private string prev_num = "";  // 存储运算符前的值
        private string oper_str = "";  // 运算符
        private string end_num = "";  // 存储运算符后的值
        public FormMain()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void btn1_Click(object sender, EventArgs e)
        {
            textBox1.AppendText("1");
        }

        private void btn2_Click(object sender, EventArgs e)
        {
            textBox1.AppendText("2");
        }

        private void btn3_Click(object sender, EventArgs e)
        {
            textBox1.AppendText("3");
        }

        private void btn4_Click(object sender, EventArgs e)
        {
            textBox1.AppendText("4");
        }

        private void btn5_Click(object sender, EventArgs e)
        {
            textBox1.AppendText("5");
        }

        private void btn6_Click(object sender, EventArgs e)
        {
            textBox1.AppendText("6");
        }

        private void btn7_Click(object sender, EventArgs e)
        {
            textBox1.AppendText("7");
        }

        private void btn8_Click(object sender, EventArgs e)
        {
            textBox1.AppendText("8");
        }

        private void btn9_Click(object sender, EventArgs e)
        {
            textBox1.AppendText("9");
        }

        private void btnPoint_Click(object sender, EventArgs e)
        {
            textBox1.AppendText(".");
        }
        // 退格按钮
        private void btnBack_Click(object sender, EventArgs e)
        {
            string str = textBox1.Text;
            // 如果大于0就执行该操作
            if (str.Length > 0)
            {
                textBox1.Text = str.Substring(0, str.Length - 1);
            }
        }

        private void btn0_Click(object sender, EventArgs e)
        {
            textBox1.AppendText("0");
        }
        // 取反操作
        private void btnSign_Click(object sender, EventArgs e)
        {
            string str = textBox1.Text;
            // 判断字符串是否包含某字符的方法有:
            // string.Contains、sring.IndexOf
            if (str.Contains("-"))
            {
                textBox1.Text = str.Substring(1,str.Length-1);
                btnSqrt.Enabled = true;  // 正数才能够开平方
            }
            else
            {
                btnSqrt.Enabled = false;  // 负数不能够开平方
                textBox1.Text = "-" + textBox1.Text;
            }
            /*
            if (str.IndexOf(".") >= 0)
            {
                //
            }
            else
            {
                //
            }*/
        }
        // 取倒数
        private void btnInve_Click(object sender, EventArgs e)
        {
            string str = textBox1.Text;
            // 区分有无负号
            if (str.Contains("-"))
            {
                str = str.Substring(1, str.Length - 1);
                double dou_num = Convert.ToDouble(str);
                dou_num  = str_double_replace(dou_num);
                str = "-" + dou_num.ToString();
            }
            else
            {
                double dou_num = Convert.ToDouble(str);
                dou_num = str_double_replace(dou_num);
                str = dou_num.ToString();
            }
            textBox1.Text = str;
        }
        // 这个是取倒数操作
        private double str_double_replace(double num)
        {
            if (num != 0.00)
            {
                double dou_num = 1.00 / num;
                return dou_num;
            }
            else
            {
                return 0.00;
            }
        }
        // 开平方操作
        private void btnSqrt_Click(object sender, EventArgs e)
        {
            double dou_num = Convert.ToDouble(textBox1.Text);
            dou_num = System.Math.Sqrt(dou_num);
            textBox1.Text = dou_num.ToString();
        }
        // 清空这一次输入的值
        private void btnClear_Click(object sender, EventArgs e)
        {
            textBox1.Text = "";
        }
        // 等于操作
        private void btnEq_Click(object sender, EventArgs e)
        {
            double prev_double;
            double end_double;
            if (prev_num.Length != 0)
            {
                end_num = textBox1.Text;
                // 计算前一项,区分有无负号
                if (prev_num.Contains("-"))
                {
                    prev_num = prev_num.Substring(1, prev_num.Length - 1);
                    prev_double = 0.00 - Convert.ToDouble(prev_num);
                }
                else
                {
                    prev_double = Convert.ToDouble(prev_num);
                }
                // 计算后一项
                if (end_num.Contains("-"))
                {
                    end_num = end_num.Substring(1, end_num.Length - 1);
                    end_double = 0.00 - Convert.ToDouble(end_num);
                }
                else
                {
                    end_double = Convert.ToDouble(end_num);
                }
                switch (oper_str)
                {
                    case "+":
                        end_double = prev_double + end_double;
                        break;
                    case "-":
                        end_double = prev_double - end_double;
                        break;
                    case "*":
                        end_double = prev_double * end_double;
                        break;
                    case "/":
                        end_double = prev_double / end_double;
                        break;
                    case "%":
                        end_double = prev_double % end_double;
                        break;
                    default:
                        end_double = 0.0;
                        break;
                }
                textBox1.Text = end_double.ToString();
            }
        }
        // 加 操作
        private void btnAdd_Click(object sender, EventArgs e)
        {
            // 可能是按错符号
            if ((textBox1.Text).Length != 0)
            {
                prev_num = textBox1.Text;  // 获取前一项
            }
            oper_str = "+";
            textBox1.Text = "";
        }
        // 减 操作
        private void btnSub_Click(object sender, EventArgs e)
        {
            // 可能是按错符号
            if ((textBox1.Text).Length != 0)
            {
                prev_num = textBox1.Text;  // 获取前一项
            }
            oper_str = "-";
            textBox1.Text = "";
        }
        // 乘 操作
        private void btnMult_Click(object sender, EventArgs e)
        {
            // 可能是按错符号
            if ((textBox1.Text).Length != 0)
            {
                prev_num = textBox1.Text;  // 获取前一项
            }
            oper_str = "*";
            textBox1.Text = "";
        }
        // 清空
        private void btnReset_Click(object sender, EventArgs e)
        {
            if (prev_num.Length != 0)
            {
                prev_num = "";
            }
            if (oper_str.Length != 0)
            {
                oper_str = "";
            }
            if (end_num.Length != 0)
            {
                end_num = "";
            }
            textBox1.Text = "";
        }
        // 除 操作
        private void btnDivi_Click(object sender, EventArgs e)
        {
            // 可能是按错符号
            if ((textBox1.Text).Length != 0)
            {
                prev_num = textBox1.Text;  // 获取前一项
            }
            oper_str = "/";
            textBox1.Text = "";
        }
        // 取余 操作
        private void btnPerc_Click(object sender, EventArgs e)
        {
            // 可能是按错符号
            if ((textBox1.Text).Length != 0)
            {
                prev_num = textBox1.Text;  // 获取前一项
            }
            oper_str = "%";
            textBox1.Text = "";
        }
    }
}

 

计算器

标签:box   分享   .data   reading   reset   div   orm   name   space   

原文地址:https://www.cnblogs.com/namejr/p/10329691.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!