c#实现简易计算器

  • 1.进入vs,按图创建项目
    c#实现简易计算器_第1张图片
  • 2.点击确定后出现form窗体,左侧有工具箱
    c#实现简易计算器_第2张图片
  • 3.将里边的Button和TextBox拖拽到form窗体,textbox是计算器数字输入输出的地方,拖拽一个就行,button的数量按自己的计算器要实现功能来确定

c#实现简易计算器_第3张图片

  • 4.这是我要做的界面,所以拖拽了20个button,我的button从 +/-开始向右加1
    对应的2是button6
    对应x²是button18
    c#实现简易计算器_第4张图片
    (修改button的样式在vs的右下角,如果不清楚怎么改去网上搜一下,但是建议大家不要不要不要在现在就调整背景色,字体,或者改button里边的内容,先写代码,最后回来设计样式)
  • 5 代码(双击窗体或者右击查看代码)
    注意代码和窗体名称的匹配,如果你的button顺序和我的一致,那么代码就可以直接用了。如果不匹配,在右下角属性这里把click名字改一下,比如你的2对应的是button14按钮,那么把click这里名字改成我上边图示的button6_click,效果就和我的一样了
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 计算器实验1
{
    public partial class Form1 : Form
    {
        double first = 0;
        double last = 0;
        double result = 0;
        bool c = false;
        string d= "";

        public Form1()
        {
            InitializeComponent();
        }

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





        private void button2_Click(object sender, EventArgs e)
        {
            if (c == true)

            {

                textBox1.Text = "";

                c = false;

            }

            textBox1.Text += "0";
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (c == true)

            {

                textBox1.Text = "";

                c = false;

            }
            textBox1.Text += ".";
        }



        private void button5_Click(object sender, EventArgs e)
        {
            if (c == true)

            {

                textBox1.Text = "";

                c = false;

            }

            textBox1.Text += "1";
        }

        private void button6_Click(object sender, EventArgs e)
        {
            if (c == true)

            {

                textBox1.Text = "";

                c = false;

            }

            textBox1.Text += "2";
        }

        private void button7_Click(object sender, EventArgs e)
        {
            if (c == true)

            {

                textBox1.Text = "";

                c = false;

            }

            textBox1.Text += "3";
        }



        private void button9_Click(object sender, EventArgs e)
        {
            if (c == true)

            {

                textBox1.Text = "";

                c = false;

            }

            textBox1.Text += "4";
        }

        private void button10_Click(object sender, EventArgs e)
        {
            if (c == true)

            {

                textBox1.Text = "";

                c = false;

            }

            textBox1.Text += "5";
        }

        private void button11_Click(object sender, EventArgs e)
        {
            if (c == true)

            {

                textBox1.Text = "";

                c = false;

            }

            textBox1.Text += "6";
        }


        private void button13_Click(object sender, EventArgs e)
        {
            if (c == true)

            {

                textBox1.Text = "";

                c = false;

            }

            textBox1.Text += "7";
        }

        private void button14_Clic(object sender, EventArgs e)
        {

            if (c == true)

            {

                textBox1.Text = "";

                c = false;

            }

            textBox1.Text += "8";
        }

        private void button15_Click(object sender, EventArgs e)
        {
            if (c == true)

            {

                textBox1.Text = "";

                c = false;

            }

            textBox1.Text += "9";
        }
        private void button20_Click(object sender, EventArgs e)// /
        {
            c = true;
            first = double.Parse(textBox1.Text);
            d = "/";
        }
        private void button16_Click(object sender, EventArgs e) //*
        {
            c = true;
            first = double.Parse(textBox1.Text);
            d = "*";
        }

        private void button12_Click(object sender, EventArgs e) //-
        {
            if (textBox1.Text == "0")
                textBox1.Text = "-";
            else {
                c = true;
                first = double.Parse(textBox1.Text);
                d = "-";
            }

        }

        private void button8_Click(object sender, EventArgs e) //+
        {
            c = true;
            first = double.Parse(textBox1.Text);
            d = "+";
        }
        private void button1_Click(object sender, EventArgs e) //取反
        {
            if (double.Parse(textBox1.Text) > 0)
                textBox1.Text = "-" + textBox1.Text;
            else if(double.Parse(textBox1.Text)<0)
                    textBox1.Text = -double.Parse(textBox1.Text) + "";
         
        }
        private void button17_Click(object sender, EventArgs e)   //C
        {
            textBox1.Text = "";
            first = 0;
            last = 0;
            result = 0;
            d = "";
            c = false;
        }

        private void button4_Click(object sender, EventArgs e) //=
        {
            switch (d)
            {
                case "+":result = first + double.Parse(textBox1.Text);break;
                case "-": result = first - double.Parse(textBox1.Text); break;
                case "*": result = first * double.Parse(textBox1.Text); break;
                case "/": result = first / double.Parse(textBox1.Text); break;
                case "x²":
                    if (first != 0) result = first * first;
                    else if (double.Parse(textBox1.Text) != 0) result = last * last;
                    break;
                case "√X":
                     if (first != 0) result = Math.Sqrt(first); 
                    else if (double.Parse(textBox1.Text) != 0) result = Math.Sqrt(last); 
                    break;
            }
            textBox1.Text = result+"";
                
        }
        private void button18_Click(object sender, EventArgs e) //平方
        {
            c = true;
            first = double.Parse(textBox1.Text);
            d = "x²";
        }

        private void button19_Click(object sender, EventArgs e) //开根号
        {
            c = true;
            first = double.Parse(textBox1.Text);
            d = "√X";
        }


    }
}

你可能感兴趣的:(c#,开发语言,后端)