欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

C#多功能计算器

程序员文章站 2022-07-16 19:21:13
...

C#多功能计算器C#多功能计算器

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

        private void Form1_Load(object sender, EventArgs e)
        {
            this.Location=new Point(Screen.PrimaryScreen.WorkingArea.Width/2-this.Width/2,Screen.PrimaryScreen.WorkingArea.Height/2-this.Height/2);
            textBox1.ReadOnly = true; // 禁止输入
            textBox2.ReadOnly = true;
        }

        private const int V = 2;
        string fuhao; // 符号存储
        double num1, num2; // 第一个数,第二个数
        bool isCale = false;  //记录上一个字符是否是运算符
        // 1
        private void button1_Click(object sender, EventArgs e)
        {
            if (isCale==true)
            {
                isCale = false;
                textBox1.Text = "";
            }
            textBox1.Text += 1;
            textBox2.Text += 1;
        }

        // 2
        private void button2_Click(object sender, EventArgs e)
        {
            if (isCale == true)
            {
                isCale = false;
                textBox1.Text = "";
            }
            textBox1.Text += 2;
            textBox2.Text += 2;
        }
        // 3
        private void button3_Click(object sender, EventArgs e)
        {
            if (isCale == true)
            {
                isCale = false;
                textBox1.Text = "";
            }
            textBox1.Text += 3;
            textBox2.Text += 3;
        }

        // 4
        private void button4_Click(object sender, EventArgs e)
        {
            if (isCale == true)
            {
                isCale = false;
                textBox1.Text = "";
            }
            textBox1.Text += 4;
            textBox2.Text += 4;
        }
        // 5
        private void button5_Click(object sender, EventArgs e)
        {
            if (isCale == true)
            {
                isCale = false;
                textBox1.Text = "";
            }
            textBox1.Text += 5;
            textBox2.Text += 5;
        }
        // 6
        private void button6_Click(object sender, EventArgs e)
        {
            if (isCale == true)
            {
                isCale = false;
                textBox1.Text = "";
            }
            textBox1.Text += 6;
            textBox2.Text += 6;
        }
        // 7
        private void button7_Click(object sender, EventArgs e)
        {
            if (isCale == true)
            {
                isCale = false;
                textBox1.Text = "";
            }
            textBox1.Text += 7;
            textBox2.Text += 7;
        }
        // 8
        private void button8_Click(object sender, EventArgs e)
        {
            if (isCale == true)
            {
                isCale = false;
                textBox1.Text = "";
            }
            textBox1.Text += 8;
            textBox2.Text += 8;
        }
        // 9
        private void button9_Click(object sender, EventArgs e)
        {
            if (isCale == true)
            {
                isCale = false;
                textBox1.Text = "";
            }
            textBox1.Text += 9;
            textBox2.Text += 9;
        }
        // 0
        private void button10_Click(object sender, EventArgs e)
        {
            if (isCale == true)
            {
                isCale = false;
                textBox1.Text = "";
            }
            textBox1.Text += 0;
            textBox2.Text += 0;
        }
        // 点
        private void button11_Click(object sender, EventArgs e)
        {
            textBox2.Text += ".";
        }
        // π
        private void button12_Click(object sender, EventArgs e)
        {
            textBox2.Text += Math.PI;
        }
        // 加
        private void button16_Click(object sender, EventArgs e)
        {
            num1 = double.Parse(textBox1.Text);
            textBox2.Text += "+";
            isCale = true;
            fuhao = "+";
        }
        // 减
        private void button15_Click(object sender, EventArgs e)
        {
            num1 = double.Parse(textBox1.Text);
            textBox2.Text += "-";
            fuhao = "-";
            isCale = true;
        }
        // 乘
        private void button14_Click(object sender, EventArgs e)
        {
            num1 = double.Parse(textBox1.Text);
            textBox2.Text += "×";
            fuhao = "×";
            isCale = true;
        }
        // 除
        private void button13_Click(object sender, EventArgs e)
        {
            num1 = double.Parse(textBox1.Text);
            textBox2.Text += "÷";
            fuhao = "÷";
            isCale = true;
        }
        // 余
        private void button20_Click(object sender, EventArgs e)
        {
            num1 = double.Parse(textBox1.Text);
            textBox2.Text += "%";
            fuhao = "%";
            isCale = true;
        }
        // 平方
        private void button19_Click(object sender, EventArgs e)
        {
            num1 = double.Parse(textBox1.Text);
            textBox2.Text += "²";
            fuhao = "²";
            isCale = true;
        }
        // 立方
        private void button18_Click(object sender, EventArgs e)
        {
            num1 = double.Parse(textBox1.Text);
            textBox2.Text += "³";
            fuhao = "³";
            isCale = true;
        }
        // 开平方
        private void button17_Click(object sender, EventArgs e)
        {
            num1 = double.Parse(textBox1.Text);
            textBox2.Text += "√";
            fuhao = "√";
            isCale = true;
        }
        // sin
        private void button24_Click(object sender, EventArgs e)
        {
            num1 = double.Parse(textBox1.Text);
            textBox2.Text += "sin";
            fuhao = "s";
            isCale = true;
        }
        // cos
        private void button23_Click(object sender, EventArgs e)
        {
            num1 = double.Parse(textBox1.Text);
            textBox2.Text += "cos";
            fuhao = "c";
            isCale = true;
        }
        // tan 
        private void button22_Click(object sender, EventArgs e)
        {
            num1 = double.Parse(textBox1.Text);
            textBox2.Text += "tan";
            fuhao = "t";
            isCale = true;
        }
        // 转二进制
        private void button32_Click(object sender, EventArgs e)
        {
            num1 = int.Parse(textBox1.Text);
            textBox2.Text += "BIN";
            fuhao = "bin";

        }
        //转八进制
        private void button31_Click(object sender, EventArgs e)
        {
            num1 = int.Parse(textBox1.Text);
            textBox2.Text += "OCT";
            fuhao = "oct";
        }
        // 转十六进制
        private void button33_Click(object sender, EventArgs e)
        {
            num1 = int.Parse(textBox1.Text);
            textBox2.Text += "HEA";
            fuhao = "hea";
        }
        // &
        private void button21_Click(object sender, EventArgs e)
        {
            num1 = int.Parse(textBox1.Text);
            textBox2.Text += "&";
            fuhao = "&";
            isCale = true;
        }
        // |
        private void button28_Click(object sender, EventArgs e)
        {
            num1 = int.Parse(textBox1.Text);
            textBox2.Text += "|";
            fuhao = "|";
            isCale = true;
        }
        // ^
        private void button29_Click(object sender, EventArgs e)
        {
            num1 = int.Parse(textBox1.Text);
            textBox2.Text += "^";
            fuhao = "^";
            isCale = true;
        }
        // 清除
        private void button26_Click(object sender, EventArgs e)
        {
            textBox2.Text = null;
            textBox1.Text = null;
        }
        // 删除一格
        private void button27_Click(object sender, EventArgs e)
        {
            if (textBox2.Text.Length>0)
            {
                textBox2.Text = textBox2.Text.Substring(0, textBox2.Text.Length - 1);
            }
        }
        // 等于
        private void button25_Click(object sender, EventArgs e)
        {
            isCale = true;
            num2 = double.Parse(textBox1.Text);
            if (fuhao == "+")
            {
                textBox1.Text = (num1 + num2).ToString();
            }
            if (fuhao == "-")
            {
                textBox1.Text = (num1 - num2).ToString();
            }
            if (fuhao == "×")
            {
                textBox1.Text = (num1 * num2).ToString();
            }
            if (fuhao == "÷")
            {
                textBox1.Text = (num1 % num2).ToString();
            }
            if (fuhao == "%")
            {
                textBox1.Text = (num1 % num2).ToString();
            }
            if (fuhao == "²")
            {
                textBox1.Text =(Math.Pow(num2, 2)).ToString();
            }
            if (fuhao== "³")
            {
                textBox1.Text = (Math.Pow(num2, 3)).ToString();
            }
            if (fuhao== "s")
            {
                textBox1.Text = (Math.Sin(num2 * Math.PI) / 180).ToString();
            }
            if (fuhao== "c")
            {
                textBox1.Text = (Math.Cos(num2 * Math.PI) / 180).ToString();
            }
            if (fuhao== "t")
            {
                textBox1.Text = (Math.Tan(num2 * Math.PI / 180)).ToString();
            }
            if (fuhao == "&")
            {
                textBox1.Text =((int)num1&(int)num2).ToString();
            }
            if (fuhao == "|")
            {
                textBox1.Text = ((int)num1 | (int)num2).ToString();
            }
            if (fuhao == "^")
            {
                textBox1.Text = ((int)num1 ^ (int)num2).ToString();
            }
            if (fuhao == "bin")
            {
               textBox1.Text = Convert.ToString((int)num2,2);
            }
            if (fuhao == "oct")
            {
                textBox1.Text = Convert.ToString((int)num2, 8);
            }
            if (fuhao == "hea")
            {
                textBox1.Text = Convert.ToString((int)num2, 16);
            }
            textBox2.Text += "=" + textBox1.Text;
        }
    }
}

上一篇: C#计算器

下一篇: C# 入门 基础