微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

C#Windows Forms (2)--xdd

一、计算器

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 _4计算器
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button8_Click(object sender, EventArgs e)
        {
            textBox1.Text = textBox1.Text + button8.Text;
        }

        private void button11_Click(object sender, EventArgs e)
        {
            double r;//用于保存结果
            string t = textBox1.Text;//用以保存文本框中的算术表达式
            int space = t.IndexOf(' ');//用以搜索文本框中的空格位置,注意是位置
            string s1 = t.Substring(0, space);//s1用于保存第一个运算数
            char op = Convert.tochar(t.Substring(space + 1, 1));//op用以保存运算符
            string s2 = t.Substring(space+3);//s2用于保存第二个运算数
            double arg1 = Convert.Todouble(s1);//将运算数从string转换成double
            double arg2 = Convert.Todouble(s2);
            switch (op)
            {
                case '+':
                    r = arg1 + arg2;
                    break;
                case '-':
                    r = arg1 - arg2;
                    break;
                case '*':
                    r = arg1 * arg2;
                    break;
                case '/':
                    if (arg2 == 0)
                    {
                      // MessageBox.Show("0不能做分母","错误",MessageBoxButtons.OK,MessageBoxIcon.Error);
                       throw new ApplicationException();
                    }
                    else
                    {
                        r = arg1 / arg2;
                        break;     //跳出if语句
                    }
                    break;   //这是case'/'的break,跳出switch
                default:
                    throw new ApplicationException();
            }
            textBox1.Text = r.ToString();
        }

        private void button1_Click(object sender, EventArgs e)
        {
           // Button btn = (Button)sender;
           // textBox1.Text = textBox1.Text + btn.Text;
            textBox1.Text = textBox1.Text + button1.Text;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            textBox1.Text = textBox1.Text + button2.Text;
        }

        private void button3_Click(object sender, EventArgs e)
        {
            textBox1.Text = textBox1.Text + button3.Text;
        }

        private void button6_Click(object sender, EventArgs e)
        {
            textBox1.Text = textBox1.Text + button6.Text;
        }

        private void button5_Click(object sender, EventArgs e)
        {
            textBox1.Text = textBox1.Text + button5.Text;
        }

        private void button4_Click(object sender, EventArgs e)
        {
            textBox1.Text = textBox1.Text + button4.Text;
        }

        private void button9_Click(object sender, EventArgs e)
        {
            textBox1.Text = textBox1.Text + button9.Text;
        }

        private void button7_Click(object sender, EventArgs e)
        {
            textBox1.Text = textBox1.Text + button7.Text;
        }

        private void button12_Click(object sender, EventArgs e)
        {
            textBox1.Text = textBox1.Text + button12.Text;
        }

        private void button14_Click(object sender, EventArgs e)
        {
            textBox1.Text = textBox1.Text + " "+button14.Text+" ";
        }

        private void button13_Click(object sender, EventArgs e)
        {
            textBox1.Text = textBox1.Text + " " + button13.Text + " ";
        }

        private void button10_Click(object sender, EventArgs e)
        {
            textBox1.Text = textBox1.Text + " " + button10.Text + " ";
        }

        private void button15_Click(object sender, EventArgs e)
        {
            textBox1.Text = textBox1.Text + " " + button15.Text + " ";
        }

        private void button16_Click(object sender, EventArgs e)
        {
            textBox1.Text = "";
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            DialogResult dr=MessageBox.Show("确定退出吗","退出",MessageBoxButtons.OKCancel,MessageBoxIcon.Question);
            if (dr==DialogResult.OK)
            {
                e.Cancel=false;
            }
            else
            {
                e.Cancel=true;
            }
        }
    }
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐