C#多功能计算器

C#多功能计算器

1.设计工具

    Microsoft Visual 2017

2.设计内容

   运用C#知识,新建一个Windows应用程序,制作一个多功能计算器。

3.设计步骤

   (1)新建一个程序项目,窗体默认为 Form1.cs。
   (2)设置窗体的Text属性为“多功能计算器”,设置Font属性为宋体、小五。
   (3)添加控件
   向窗体中添加一个TextBox控件textBox1,一个GroupBox控件groupBox1,二十四个Button控件button1~button24。

4.实现方法

(1)利用textBox1的Text属性,进行存储字符;
(2)定义两个浮点变量a,b,一个布尔类型的变量c(用于清空textBox1的Text),一个字符串类型变量d(用于表示计算器的运算符);
(3)利用条件语句switch语句对计算器的功能进行存储;
(4)利用C#自带的Math函数及基本的运算方法,实现运算功能。

5.程序源代码

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 calculate
{
public partial class Form1 : Form
{
double a = 0;
double b = 0;
bool c = false;
string d;
public Form1()
{
InitializeComponent();
}

    private void button13_Click(object sender, EventArgs e)
    {
        if (c == true)
        {
            textBox1.Text = "";
            c = false;
        }
        textBox1.Text += "0";
        if (d == "/")
        {
            textBox1.Clear();
            MessageBox.Show("除数不能为零", "错误提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
        }
    }

    private void button1_Click(object sender, EventArgs e)
    {
        if (c == true)
        {
            textBox1.Text = "";
            c = false;
        }
        textBox1.Text += "1";
    }

    private void button2_Click(object sender, EventArgs e)
    {
        if (c == true)
        {
            textBox1.Text = "";
            c = false;
        }
        textBox1.Text += "2";
    }

    private void button3_Click(object sender, EventArgs e)
    {
        if (c == true)
        {
            textBox1.Text = "";
            c = false;
        }
        textBox1.Text += "3";
    }

    private void button5_Click(object sender, EventArgs e)
    {
        if (c == true)
        {
            textBox1.Text = "";
            c = false;
        }
        textBox1.Text += "4";
    }

    private void button6_Click(object sender, EventArgs e)
    {
        if (c == true)
        {
            textBox1.Text = "";
            c = false;
        }
        textBox1.Text += "5";
    }

    private void button7_Click(object sender, EventArgs e)
    {
        if (c == true)
        {
            textBox1.Text = "";
            c = false;
        }
        textBox1.Text += "6";
    }

    private void button9_Click(object sender, EventArgs e)
    {
        if (c == true)
        {
            textBox1.Text = "";
            c = false;
        }
        textBox1.Text += "7";
    }

    private void button10_Click(object sender, EventArgs e)
    {
        if (c == true)
        {
            textBox1.Text = "";
            c = false;
        }
        textBox1.Text += "8";
    }

    private void button11_Click(object sender, EventArgs e)
    {
        if (c == true)
        {
            textBox1.Text = "";
            c = false;
        }
        textBox1.Text += "9";
    }


    private void button21_Click(object sender, EventArgs e)
    {
        if (c == true)
        {
            textBox1.Text = "";
            c = false;
        }
        textBox1.Text += ".";
    }
    private void button14_Click(object sender, EventArgs e)
    {
        textBox1.Text = "";
    }

    private void button15_Click(object sender, EventArgs e)    //设置等于号功能
    {
        switch (d)
        {
            case "+": a = b + double.Parse(textBox1.Text); break;
            case "-": a = b - double.Parse(textBox1.Text); break;
            case "*": a = b * double.Parse(textBox1.Text); break;
            case "/": a = b / double.Parse(textBox1.Text); break;
            case "x2": a = b * double.Parse(textBox1.Text); break;
            case "sqrt": a = Math.Sqrt(b); break;
            case "log": a = Math.Log(double.Parse(textBox1.Text), b); break;
            case "ln": a = Math.Log(b, Math.E); break;
            case "1/x": a = 1/ double.Parse(textBox1.Text); break;
            case "%": a = b/100.0; break;
            case "x~y": a = Math.Pow(b,double.Parse(textBox1.Text)); break;
        }
        textBox1.Text = a + "";
        c = true;
    }
    
    private void button16_Click(object sender, EventArgs e)    //除法运算
    {
        c = true;
        b = double.Parse(textBox1.Text);
        d = "/";
    }

    private void button12_Click(object sender, EventArgs e)    //乘法运算
    {
        c = true;
        b = double.Parse(textBox1.Text);
        d = "*";
    }

    private void button8_Click(object sender, EventArgs e)
    {
        c = true;
        b = double.Parse(textBox1.Text);
        d = "-";
    }

    private void button4_Click(object sender, EventArgs e)    //加法运算
    {
        c = true;
        b = double.Parse(textBox1.Text);
        d = "+";
    }

    private void button17_Click(object sender, EventArgs e)   //平方运算
    {
        c = true;
        b = double.Parse(textBox1.Text);
        d = "x2";
    }

    private void button18_Click(object sender, EventArgs e)   //开方运算
    {
        c = true;
        b = double.Parse(textBox1.Text);
        d = "sqrt";
    }

    private void button19_Click(object sender, EventArgs e)   //对数运算
    {
        c = true;
        b = double.Parse(textBox1.Text);
        d = "log";
    }

    private void button20_Click(object sender, EventArgs e)  //自然对数运算
    {
        c = true;
        b = double.Parse(textBox1.Text);
        d = "ln";
    }

    private void button22_Click(object sender, EventArgs e)  //倒数运算
    {
        c = true;
        b = double.Parse(textBox1.Text);
        d = "1/x";
    }

    private void button23_Click(object sender, EventArgs e)  //百分号运算
    {
        c = true;
        b = double.Parse(textBox1.Text);
        d = "%";
    }

    private void button24_Click(object sender, EventArgs e)  //幂运算
    {
        c = true;
        b = double.Parse(textBox1.Text);
        d = "x~y";
    }
}

}

6.实现效果

主界面图:
在这里插入图片描述

7.特点

 计算器已实现加、减、乘、除法运算,倒数运算、百分数运算、平方运算、开方运算、幂运算、对数运算、自然对数运算及清楚功能。
 
 如有问题,请提出,相互交流学习噢~
相关推荐
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页