C#多功能计算器(winform版)

在这里插入图片描述

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++就是你个人能力提升,职业之路进阶的不二之选。 【课程特色】 1.课程共19大章节,239课时内容,涵盖数据结构、函数、类、指针、标准库全部知识体系。 2.带你从知识与思想的层面从0构建C++知识框架,分析大型项目实践思路,为你打下坚实的基础。 3.李宁老师结合4大国外顶级C++著作的精华为大家推出的《征服C++11》课程。 【学完后我将达到什么水平?】 1.对C++的各个知识能够熟练配置、开发、部署; 2.吊打一切关于C++的笔试面试题; 3.面向物联网的“嵌入式”和面向大型化的“分布式”开发,掌握职业钥匙,把握行业先机。 【面向人群】 1.希望一站式快速入门的C++初学者; 2.希望快速学习 C++、掌握编程要义、修炼内功的开发者; 3.有志于挑战更高级的开发项目,成为资深开发的工程师。 【课程设计】 本课程包含3大模块 基础篇 本篇主要讲解c++的基础概念,包含数据类型、运算符等基本语法,数组、指针、字符串等基本词法,循环、函数、类等基本句法等。 进阶篇 本篇主要讲解编程中常用的一些技能,包含类的高级技术、类的继承、编译链接和命名空间等。 提升篇: 本篇可以帮助学员更加高效的进行c++开发,其中包含类型转换、文件操作、异常处理、代码重用等内容。
©️2020 CSDN 皮肤主题: 撸撸猫 设计师:C马雯娟 返回首页