• c#简易计算器


    微软MSDN的代码库就有示例

    http://code.msdn.microsoft.com/Simple-Calculator-54ec8e4a

     

    using System; 
    using System.Collections.Generic; 
    using System.ComponentModel; 
    using System.Data; 
    using System.Drawing; 
    using System.Linq; 
    using System.Text; 
    using System.Windows.Forms; 
     
    namespace SimpleCalculator 

        public partial class frmCalculator : Form 
        { 
            string operand1 = string.Empty; 
            string operand2 = string.Empty; 
            string result; 
            char operation; 
     
            public frmCalculator() 
            { 
                InitializeComponent(); 
            } 
     
            private void frmCalculator_Load(object sender, EventArgs e) 
            { 
                btnOne.Click += new EventHandler(btn_Click); 
                btnTwo.Click += new EventHandler(btn_Click); 
                btnThree.Click += new EventHandler(btn_Click); 
                btnFour.Click += new EventHandler(btn_Click); 
                btnFive.Click += new EventHandler(btn_Click); 
                btnSix.Click += new EventHandler(btn_Click); 
                btnSeven.Click += new EventHandler(btn_Click); 
                btnEight.Click += new EventHandler(btn_Click); 
                btnNine.Click += new EventHandler(btn_Click); 
                btnZero.Click += new EventHandler(btn_Click); 
                btnDot.Click += new EventHandler(btn_Click); 
            } 
     
            void btn_Click(object sender, EventArgs e) 
            { 
                try 
                { 
                    Button btn = sender as Button; 
     
                    switch (btn.Name) 
                    { 
                        case "btnOne": 
                            txtInput.Text += "1"; 
                            break; 
                        case "btnTwo": 
                            txtInput.Text += "2"; 
                            break; 
                        case "btnThree": 
                            txtInput.Text += "3"; 
                            break; 
                        case "btnFour": 
                            txtInput.Text += "4"; 
                            break; 
                        case "btnFive": 
                            txtInput.Text += "5"; 
                            break; 
                        case "btnSix": 
                            txtInput.Text += "6"; 
                            break; 
                        case "btnSeven": 
                            txtInput.Text += "7"; 
                            break; 
                        case "btnEight": 
                            txtInput.Text += "8"; 
                            break; 
                        case "btnNine": 
                            txtInput.Text += "9"; 
                            break; 
                        case "btnZero": 
                            txtInput.Text += "0"; 
                            break; 
                        case "btnDot": 
                            if(!txtInput.Text.Contains(".")) 
                                txtInput.Text += "."; 
                            break; 
     
                    } 
                } 
                catch(Exception ex) 
                { 
                    MessageBox.Show("Sorry for the inconvenience, Unexpected error occured. Details: " + 
                        ex.Message); 
                } 
            } 
     
            private void txtInput_KeyPress(object sender, KeyPressEventArgs e) 
            { 
                switch (e.KeyChar) 
                { 
                    case '1': 
                    case '2': 
                    case '3': 
                    case '4': 
                    case '5': 
                    case '6': 
                    case '7': 
                    case '8': 
                    case '9': 
                    case '0': 
                    //case '+': 
                    //case '-': 
                    //case '*': 
                    //case '/': 
                    //case '.': 
                        break; 
                    default: 
                        e.Handled = true; 
                        MessageBox.Show("Only numbers, +, -, ., *, / are allowed"); 
                        break; 
                }            
            } 
     
            private void txtInput_TextChanged(object sender, EventArgs e) 
            { 
                 
            } 
     
            private void btnPlus_Click(object sender, EventArgs e) 
            { 
                operand1 = txtInput.Text; 
                operation = '+'; 
                txtInput.Text = string.Empty; 
            } 
     
            private void btnMinus_Click(object sender, EventArgs e) 
            { 
                operand1 = txtInput.Text; 
                operation = '-'; 
                txtInput.Text = string.Empty; 
            } 
     
            private void btnMulitply_Click(object sender, EventArgs e) 
            { 
                operand1 = txtInput.Text; 
                operation = '*'; 
                txtInput.Text = string.Empty; 
            } 
     
            private void btnDivide_Click(object sender, EventArgs e) 
            { 
                operand1 = txtInput.Text; 
                operation = '/'; 
                txtInput.Text = string.Empty; 
            } 
     
            private void btnEqual_Click(object sender, EventArgs e) 
            { 
                operand2 = txtInput.Text; 
     
                double opr1, opr2; 
                double.TryParse(operand1, out opr1); 
                double.TryParse(operand2, out opr2); 
     
                switch (operation) 
                { 
                    case '+': 
                        result = (opr1 + opr2).ToString(); 
                        break; 
     
                    case '-': 
                        result = (opr1 - opr2).ToString(); 
                        break; 
     
                    case '*': 
                        result = (opr1 * opr2).ToString(); 
                        break; 
     
                    case '/': 
                        if (opr2 != 0) 
                        { 
                            result = (opr1 / opr2).ToString(); 
                        } 
                        else 
                        { 
                            MessageBox.Show("Can't divide by zero"); 
                        } 
                        break; 
                } 
     
                txtInput.Text = result.ToString(); 
            } 
     
            private void btnClear_Click(object sender, EventArgs e) 
            { 
                txtInput.Text = string.Empty; 
                operand1 = string.Empty; 
                operand2 = string.Empty; 
            } 
     
            private void btnSqrRoot_Click(object sender, EventArgs e) 
            { 
                double opr1; 
                if (double.TryParse(txtInput.Text, out opr1)) 
                { 
                    txtInput.Text = (Math.Sqrt(opr1)).ToString(); 
                } 
            } 
     
            private void btnByTwo_Click(object sender, EventArgs e) 
            { 
                double opr1; 
                if (double.TryParse(txtInput.Text, out opr1)) 
                { 
                    txtInput.Text = (opr1 / 2).ToString(); 
                } 
            } 
     
            private void btnByFour_Click(object sender, EventArgs e) 
            { 
                double opr1; 
                if (double.TryParse(txtInput.Text, out opr1)) 
                { 
                    txtInput.Text = (opr1 / 4).ToString(); 
                } 
            } 
        } 
    }
  • 相关阅读:
    jQuery Mobile的学习 jQuery Mobile工具栏、标题栏、页脚栏的定位学习
    【centOS7】centOS7上普通用户切换root用户,相互切换
    【elaseticsearch】elaseticsearch启动报错Caused by: org.elasticsearch.transport.BindTransportException: Failed to bind to [9300-9400]
    【docker】docker启动、重启、关闭命令,附带:docker启动容器报错:docker: Error response from daemon: driver failed programming external connectivity on endpoint es2-node
    【centOS】centos7 查看和关闭防火墙
    【elasticsearceh】elasticsearch.yml配置文件详解
    【docker】关于docker 中 镜像、容器的关系理解
    【xshell】xshell设置快捷键 设置Ctrl+C Ctrl+V快捷键为复制粘贴
    【docker】elasticsearch-head无法连接elasticsearch的原因和解决,集群健康值:未连接,ElasticSearch——跨域访问的问题
    【spring cloud】spring cloud Sleuth 和Zipkin 进行分布式链路跟踪
  • 原文地址:https://www.cnblogs.com/markli/p/3707579.html
Copyright © 2020-2023  润新知