C#桌面应用制作计算器

C#桌面应用制作简易计算器,可实现数字之间的加减乘除、AC按键清屏、Del按键清除末尾数字、+/-按键取数字相反数、%按键使数字缩小100倍、=按键显示运算结果等......

页面实现效果

功能实现

布局

计算器主体使用Panel容器,然后将button控件排列放置Panel容器做为计算器按键,显示主体使用groudBox容器,然后在groudBox容器里放置一个Panel容器作为显示屏,再在Panel容器放置一个label控件用于显示输入和运算结果。

布局效果

数字按键功能

点击数字按键使其数字投射到计算器平面上,因为屏幕大小有限,所以使其数字长度大于16时,字体缩小一倍,数字长度大于30时,弹窗提示"字符长度超出范围"。

数字按键功能实现效果

数字按键功能实现代码

cs 复制代码
 private void button8_Click(object sender, EventArgs e)
 {
     string seven = "7";
     if (label1.Text.Length < 30)
     {
         if (label1.Text == "+" || label1.Text == "-" || label1.Text == "×" || label1.Text == "÷")
         {
             label1.Text = null;//如果前面有运算符,则先消除运算符再把数字投射到屏幕上
         }
         label1.Text = label1.Text + seven;//将数字投射到屏幕上
         if (label1.Text.Length > 16)
         {
             label1.Font = new Font(label1.Font.FontFamily, 13f, label1.Font.Style);
         }
         else
         {
             label1.Font = new Font(label1.Font.FontFamily, 26f, label1.Font.Style);
         }
     }
     else
     {
         MessageBox.Show("字符长度超出范围");
     }
 }

运算符按键功能

如果在输入运算符前未输入数字,则弹窗提示"无运算数字";如果输入运算符前已经输入数字且symbol(运算符)为null,则清除已经输入的并且投射到屏幕上的数字并将其赋值至num1,然后将运算符赋值给symbol并投射到屏幕上;如果输入运算符前屏幕上已经显示了输入的运算符,则弹窗提示"您已输入运算符";如果输入运算符前已经输入数字且symbol(运算符)已被赋值,则清除已经输入的并且投射到屏幕上的数字并将其赋值至num2并计算num1 运算符 num2结果并将其赋值至num1,然后将运算符赋值给symbol并投射到屏幕上。

运算符按键功能实现效果

运算符按键功能实现代码

cs 复制代码
 private void button4_Click(object sender, EventArgs e)
 {
     string divide = "÷";
     if (symbol == null && label1.Text.Length<1&&num1==null)
     {
         MessageBox.Show("无运算数字");
     }
     else
     {
         if (symbol == null)
         {
             if(num1 == null)
             {
                 num1 = label1.Text;
             }
             symbol = divide;
             label1.Text = symbol;
         }
         else
         {
                 if (label1.Text == "+" || label1.Text == "-" || label1.Text == "×" || label1.Text == "÷") {
                     MessageBox.Show("您已输入运算符");
                 }
             else
             {
                 num2 = label1.Text;
                 rt = double.Parse(num1) / double.Parse(num2);
                 num1 = rt.ToString();
                 symbol = divide;
                 label1.Text = symbol;
                 num2 = null;
             }
         }
     }
 }

AC按键功能

点击清除所有信息。

AC按键功能实现效果

AC按键功能实现代码

cs 复制代码
 private void button1_Click(object sender, EventArgs e)
 {
     label1.Text = null;
     num1=null;
     num2=null;
     symbol = null;
 }

Del按键功能

点击清除末尾字符,如果屏幕上末尾字符显示的是输入的运算符,则点击后清除运算符并把num1的值投射到屏幕上。

Del按键功能实现效果

Del按键功能实现代码

cs 复制代码
private void button2_Click(object sender, EventArgs e)
{
    if (label1.Text.Length == 0)
    {
        label1.Text = null;
    }
    else
    {
        if (label1.Text == "+" || label1.Text == "-" || label1.Text == "×" || label1.Text == "÷")
        {
            label1.Text = num1;
            symbol = null;
        }
        else
        {
            string del = label1.Text;
            label1.Text = del.Substring(0, del.Length - 1);
            if (label1.Text.Length < 30)
            {
                if (label1.Text.Length > 16)
                {
                    label1.Font = new Font(label1.Font.FontFamily, 13f, label1.Font.Style);
                }
                else
                {
                    label1.Font = new Font(label1.Font.FontFamily, 26f, label1.Font.Style);
                }
            }
        }
    }
}

+/-按键功能

点击取屏幕上数字的相反数并投射到屏幕上;但是如果点击+/-按键时屏幕上没有数字或屏幕上为运算符,则按键提示"error"(按键'.'同样效果)。

+/-按键功能实现效果

+/-按键功能实现代码

cs 复制代码
private void button3_Click(object sender, EventArgs e)
{
    if (label1.Text.Length == 0)
    {
        MessageBox.Show("error");
    }
    else if (label1.Text == "+" || label1.Text == "-" || label1.Text == "×" || label1.Text == "÷")
    {
        MessageBox.Show("error");
    }
    else
    {
        double reserve = double.Parse(label1.Text);
        reserve = -reserve;
        label1.Text = reserve.ToString();
    }
}

%按键功能

点击实现屏幕上数字缩小一百倍效果并将结果投射到屏幕上,但是如果点击%按键时屏幕上没有数字或屏幕上为运算符,则按键提示"error"。

%按键功能实现效果

%按键功能实现代码

cs 复制代码
private void button20_Click(object sender, EventArgs e)
{
    if (label1.Text.Length == 0)
    {
        MessageBox.Show("error");
    }
    else if (label1.Text == "+" || label1.Text == "-" || label1.Text == "×" || label1.Text == "÷")
    {
        MessageBox.Show("error");
    }
    else
    {
        double reserve = double.Parse(label1.Text);
        reserve = reserve / 100;
        label1.Text = reserve.ToString();
    }
}

等号按键功能

点击实现运算效果并将最终结果输出到屏幕上;如果在输入等号前输入数字但是未输入运算符,屏幕显示字符保持原来不变,不进行运算;如果在输入等号前未输入数字也未输入运算符,则弹窗提示"无运算数字";如果只输入一个数字和一个运算符,则弹窗提示"error"。

等号按键功能实现效果

等号按键功能实现代码

cs 复制代码
 private void button17_Click(object sender, EventArgs e)
 {
     if (symbol == null && label1.Text.Length < 1)
     {
         MessageBox.Show("无运算数字");
     }
     else
     {
         if (symbol == null)
         {
             num1 = label1.Text;
         }
         else
         {
             if (label1.Text != null)
             {
                 if (label1.Text == "+" || label1.Text == "-" || label1.Text == "×" || label1.Text == "÷")
                 {
                     MessageBox.Show("error");
                 }
                 else
                 {
                     num2 = label1.Text;
                 }
             }
         }
     }
     if (num2 != null)
     {
         if (symbol == "+")
         {
             rt = double.Parse(num1) + double.Parse(num2);
             label1.Text = rt.ToString();
             num1 = rt.ToString();
             symbol = null;
             num2 = null;
         }
         else if (symbol == "-")
         {
             rt = double.Parse(num1) - double.Parse(num2);
             label1.Text = rt.ToString();
             num1 = rt.ToString();
             symbol = null;
             num2 = null;
         }
         else if (symbol == "×")
         {
             rt = double.Parse(num1) * double.Parse(num2);
             label1.Text = rt.ToString();
             num1 = rt.ToString();
             symbol = null;
             num2 = null;
         }
         else if (symbol == "÷")
         {
             rt = double.Parse(num1) / double.Parse(num2);
             label1.Text = rt.ToString();
             num1 = rt.ToString();
             symbol = null;
             num2 = null;
         }
     }
 }

完整代码实现

cs 复制代码
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 WindowsFormsApp计算器
{
    public partial class Form1 : Form
    {
        string symbol, num1, num2,Num1,Num2;
        double rt;
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            label1.Text = null;
            num1=null;
            num2=null;
            symbol = null;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (label1.Text.Length == 0)
            {
                label1.Text = null;
            }
            else
            {
                if (label1.Text == "+" || label1.Text == "-" || label1.Text == "×" || label1.Text == "÷")
                {
                    label1.Text = num1;
                    symbol = null;
                }
                else
                {
                    string del = label1.Text;
                    label1.Text = del.Substring(0, del.Length - 1);
                    if (label1.Text.Length < 30)
                    {
                        if (label1.Text.Length > 16)
                        {
                            label1.Font = new Font(label1.Font.FontFamily, 13f, label1.Font.Style);
                        }
                        else
                        {
                            label1.Font = new Font(label1.Font.FontFamily, 26f, label1.Font.Style);
                        }
                    }
                }
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (label1.Text.Length == 0)
            {
                MessageBox.Show("error");
            }
            else if (label1.Text == "+" || label1.Text == "-" || label1.Text == "×" || label1.Text == "÷")
            {
                MessageBox.Show("error");
            }
            else
            {
                double reserve = double.Parse(label1.Text);
                reserve = -reserve;
                label1.Text = reserve.ToString();
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            string divide = "÷";
            if (symbol == null && label1.Text.Length<1&&num1==null)
            {
                MessageBox.Show("无运算数字");
            }
            else
            {
                if (symbol == null)
                {
                    if(num1 == null)
                    {
                        num1 = label1.Text;
                    }
                    symbol = divide;
                    label1.Text = symbol;
                }
                else
                {
                        if (label1.Text == "+" || label1.Text == "-" || label1.Text == "×" || label1.Text == "÷") {
                            MessageBox.Show("您已输入运算符");
                        }
                    else
                    {
                        num2 = label1.Text;
                        rt = double.Parse(num1) / double.Parse(num2);
                        num1 = rt.ToString();
                        symbol = divide;
                        label1.Text = symbol;
                        num2 = null;
                    }
                }
            }
        }

        private void button8_Click(object sender, EventArgs e)
        {
            string seven = "7";
            if (label1.Text.Length < 30)
            {
                if (label1.Text == "+" || label1.Text == "-" || label1.Text == "×" || label1.Text == "÷")
                {
                    label1.Text = null;//如果前面有运算符,则先消除运算符再把数字投射到屏幕上
                }
                label1.Text = label1.Text + seven;//将数字投射到屏幕上
                if (label1.Text.Length > 16)
                {
                    label1.Font = new Font(label1.Font.FontFamily, 13f, label1.Font.Style);
                }
                else
                {
                    label1.Font = new Font(label1.Font.FontFamily, 26f, label1.Font.Style);
                }
            }
            else
            {
                MessageBox.Show("字符长度超出范围");
            }
        }

        private void button7_Click(object sender, EventArgs e)
        {
            string eight = "8";
            if (label1.Text.Length < 30)
            {
                if (label1.Text == "+" || label1.Text == "-" || label1.Text == "×" || label1.Text == "÷")
                {
                    label1.Text = null;
                }
                label1.Text = label1.Text + eight;
                if (label1.Text.Length > 16)
                {
                    label1.Font = new Font(label1.Font.FontFamily, 13f, label1.Font.Style);
                }
                else
                {
                    label1.Font = new Font(label1.Font.FontFamily, 26f, label1.Font.Style);
                }
            }
            else
            {
                MessageBox.Show("字符长度超出范围");
            }
        }

        private void button6_Click(object sender, EventArgs e)
        {
            string nine = "9";
            if (label1.Text.Length < 30)
            {
                if (label1.Text == "+" || label1.Text == "-" || label1.Text == "×" || label1.Text == "÷")
                {
                    label1.Text = null;
                }
                label1.Text = label1.Text + nine;
                if (label1.Text.Length > 16)
                {
                    label1.Font = new Font(label1.Font.FontFamily, 13f, label1.Font.Style);
                }
                else
                {
                    label1.Font = new Font(label1.Font.FontFamily, 26f, label1.Font.Style);
                }
            }
            else
            {
                MessageBox.Show("字符长度超出范围");
            }
        }

        private void button5_Click(object sender, EventArgs e)
        {
            string multiply = "×";
            if (symbol == null && label1.Text.Length < 1 && num1 == null)
            {
                MessageBox.Show("无运算数字");
            }
            else
            {
                if (symbol == null)
                {
                    if (num1 == null)
                    {
                        num1 = label1.Text;
                    }
                    symbol = multiply;
                    label1.Text = symbol;
                }
                else
                {
                    if (label1.Text == "+" || label1.Text == "-" || label1.Text == "×" || label1.Text == "÷")
                    {
                        MessageBox.Show("您已输入运算符");
                    }
                    else
                    {
                        num2 = label1.Text;
                        rt = double.Parse(num1) * double.Parse(num2);
                        num1 = rt.ToString();
                        symbol = multiply;
                        label1.Text = symbol;
                        num2 = null;
                    }
                }
            }
        }

        private void button12_Click(object sender, EventArgs e)
        {
            string four = "4";
            if (label1.Text.Length < 30)
            {
                if (label1.Text == "+" || label1.Text == "-" || label1.Text == "×" || label1.Text == "÷")
                {
                    label1.Text = null;
                }
                label1.Text = label1.Text + four;
                if (label1.Text.Length > 16)
                {
                    label1.Font = new Font(label1.Font.FontFamily, 13f, label1.Font.Style);
                }
                else
                {
                    label1.Font = new Font(label1.Font.FontFamily, 26f, label1.Font.Style);
                }
            }
            else
            {
                MessageBox.Show("字符长度超出范围");
            }
        }

        private void button11_Click(object sender, EventArgs e)
        {
            string five = "5";
            if (label1.Text.Length < 30)
            {
                if (label1.Text == "+" || label1.Text == "-" || label1.Text == "×" || label1.Text == "÷")
                {
                    label1.Text = null;
                }
                label1.Text = label1.Text + five;
                if (label1.Text.Length > 16)
                {
                    label1.Font = new Font(label1.Font.FontFamily, 13f, label1.Font.Style);
                }
                else
                {
                    label1.Font = new Font(label1.Font.FontFamily, 26f, label1.Font.Style);
                }
            }
            else
            {
                MessageBox.Show("字符长度超出范围");
            }
        }

        private void button10_Click(object sender, EventArgs e)
        {
            string six = "6";
            if (label1.Text.Length < 30)
            {
                if (label1.Text == "+" || label1.Text == "-" || label1.Text == "×" || label1.Text == "÷")
                {
                    label1.Text = null;
                }
                label1.Text = label1.Text + six;
                if (label1.Text.Length > 16)
                {
                    label1.Font = new Font(label1.Font.FontFamily, 13f, label1.Font.Style);
                }
                else
                {
                    label1.Font = new Font(label1.Font.FontFamily, 26f, label1.Font.Style);
                }
            }
            else
            {
                MessageBox.Show("字符长度超出范围");
            }
        }

        private void button9_Click(object sender, EventArgs e)
        {
            string subtract = "-";
            if (symbol == null && label1.Text.Length < 1 && num1 == null)
            {
                MessageBox.Show("无运算数字");
            }
            else
            {
                if (symbol == null)
                {
                    if (num1 == null)
                    {
                        num1 = label1.Text;
                    }
                    symbol = subtract;
                    label1.Text = symbol;
                }
                else
                {
                    if (label1.Text == "+" || label1.Text == "-" || label1.Text == "×" || label1.Text == "÷")
                    {
                        MessageBox.Show("您已输入运算符");
                    }
                    else
                    {
                        num2 = label1.Text;
                        rt = double.Parse(num1) - double.Parse(num2);
                        num1 = rt.ToString();
                        symbol = subtract;
                        label1.Text = symbol;
                        num2 = null;
                    }
                }
            }
        }

        private void button16_Click(object sender, EventArgs e)
        {
            string one = "1";
            if (label1.Text.Length < 30)
            {
                if (label1.Text == "+" || label1.Text == "-" || label1.Text == "×" || label1.Text == "÷")
                {
                    label1.Text = null;
                }
                label1.Text = label1.Text + one;
                if (label1.Text.Length > 16)
                {
                    label1.Font = new Font(label1.Font.FontFamily, 13f, label1.Font.Style);
                }
                else
                {
                    label1.Font = new Font(label1.Font.FontFamily, 26f, label1.Font.Style);
                }
            }
            else
            {
                MessageBox.Show("字符长度超出范围");
            }
        }

        private void button15_Click(object sender, EventArgs e)
        {
            string two = "2";
            if (label1.Text.Length < 30)
            {
                if (label1.Text == "+" || label1.Text == "-" || label1.Text == "×" || label1.Text == "÷")
                {
                    label1.Text = null;
                }
                label1.Text = label1.Text + two;
                if (label1.Text.Length > 16)
                {
                    label1.Font = new Font(label1.Font.FontFamily, 13f, label1.Font.Style);
                }
                else
                {
                    label1.Font = new Font(label1.Font.FontFamily, 26f, label1.Font.Style);
                }
            }
            else
            {
                MessageBox.Show("字符长度超出范围");
            }
        }

        private void button14_Click(object sender, EventArgs e)
        {
            string three = "3";
            if (label1.Text.Length < 30)
            {
                if (label1.Text == "+" || label1.Text == "-" || label1.Text == "×" || label1.Text == "÷")
                {
                    label1.Text = null;
                }
                label1.Text = label1.Text + three;
                if (label1.Text.Length > 16)
                {
                    label1.Font = new Font(label1.Font.FontFamily, 13f, label1.Font.Style);
                }
                else
                {
                    label1.Font = new Font(label1.Font.FontFamily, 26f, label1.Font.Style);
                }
            }
            else
            {
                MessageBox.Show("字符长度超出范围");
            }
        }

        private void button13_Click(object sender, EventArgs e)
        {
            string add = "+";
            if (symbol == null && label1.Text.Length < 1 && num1 == null)
            {
                MessageBox.Show("无运算数字");
            }
            else
            {
                if (symbol == null)
                {
                    if (num1 == null)
                    {
                        num1 = label1.Text;
                    }
                    symbol = add;
                    label1.Text = symbol;
                }
                else
                {
                    if (label1.Text == "+" || label1.Text == "-" || label1.Text == "×" || label1.Text == "÷")
                    {
                        MessageBox.Show("您已输入运算符");
                    }
                    else
                    {
                        num2 = label1.Text;
                        rt = double.Parse(num1)+ double.Parse(num2);
                        num1 = rt.ToString();
                        symbol = add;
                        label1.Text = symbol;
                        num2 = null;
                    }
                }
            }
        }

        private void button20_Click(object sender, EventArgs e)
        {
            if (label1.Text.Length == 0)
            {
                MessageBox.Show("error");
            }
            else if (label1.Text == "+" || label1.Text == "-" || label1.Text == "×" || label1.Text == "÷")
            {
                MessageBox.Show("error");
            }
            else
            {
                double reserve = double.Parse(label1.Text);
                reserve = reserve / 100;
                label1.Text = reserve.ToString();
            }
        }

        private void button19_Click(object sender, EventArgs e)
        {
            string zero = "0";
            if(label1.Text.Length < 30)
            {
                label1.Text = label1.Text + zero;
                if (label1.Text.Length > 16)
                {
                    label1.Font = new Font(label1.Font.FontFamily, 13f, label1.Font.Style);
                }
                else
                {
                    label1.Font = new Font(label1.Font.FontFamily, 26f, label1.Font.Style);
                }
            }
            else
            {
                MessageBox.Show("字符长度超出范围");
            }
        }

        private void button18_Click(object sender, EventArgs e)
        {
            string dot = ".";
            int f=label1.Text.IndexOf(".");
            if(f == -1)
            {
                if(label1.Text.Length == 0)
                {
                    MessageBox.Show("error");
                }
                else if (label1.Text == "+" || label1.Text == "-" || label1.Text == "×" || label1.Text == "÷")
                {
                    MessageBox.Show("error");
                }
                else
                {
                    if (label1.Text.Length < 30)
                    {
                        label1.Text = label1.Text + dot;
                        if (label1.Text.Length > 16)
                        {
                            label1.Font = new Font(label1.Font.FontFamily, 13f, label1.Font.Style);
                        }
                        else
                        {
                            label1.Font = new Font(label1.Font.FontFamily, 26f, label1.Font.Style);
                        }
                    }
                    else
                    {
                        MessageBox.Show("字符长度超出范围");
                    }
                }
            }
            else
            {
                MessageBox.Show("error");
            }
        }

        private void button17_Click(object sender, EventArgs e)
        {
            if (symbol == null && label1.Text.Length < 1)
            {
                MessageBox.Show("无运算数字");
            }
            else
            {
                if (symbol == null)
                {
                    num1 = label1.Text;
                }
                else
                {
                    if (label1.Text != null)
                    {
                        if (label1.Text == "+" || label1.Text == "-" || label1.Text == "×" || label1.Text == "÷")
                        {
                            MessageBox.Show("error");
                        }
                        else
                        {
                            num2 = label1.Text;
                        }
                    }
                }
            }
            if (num2 != null)
            {
                if (symbol == "+")
                {
                    rt = double.Parse(num1) + double.Parse(num2);
                    label1.Text = rt.ToString();
                    num1 = rt.ToString();
                    symbol = null;
                    num2 = null;
                }
                else if (symbol == "-")
                {
                    rt = double.Parse(num1) - double.Parse(num2);
                    label1.Text = rt.ToString();
                    num1 = rt.ToString();
                    symbol = null;
                    num2 = null;
                }
                else if (symbol == "×")
                {
                    rt = double.Parse(num1) * double.Parse(num2);
                    label1.Text = rt.ToString();
                    num1 = rt.ToString();
                    symbol = null;
                    num2 = null;
                }
                else if (symbol == "÷")
                {
                    rt = double.Parse(num1) / double.Parse(num2);
                    label1.Text = rt.ToString();
                    num1 = rt.ToString();
                    symbol = null;
                    num2 = null;
                }
            }
        }
    }
}
相关推荐
shepherd枸杞泡茶6 小时前
WPF动画
c#·.net·wpf
code_shenbing6 小时前
跨平台WPF框架Avalonia教程 三
前端·microsoft·ui·c#·wpf·跨平台·界面设计
三天不学习6 小时前
C# AutoMapper 10个常用方法总结
c#·automapper·对象映射
oioihoii7 小时前
单例模式详解
c++·单例模式·c#·大学必学
suzh1qian9 小时前
Unity类银河战士恶魔城学习总结(P133 Merge skill tree with Parry skill把技能树和格挡技能相组合)
学习·unity·c#·游戏引擎
sukalot9 小时前
windows C#-异步编程模型(下)
开发语言·windows·c#
zls3653659 小时前
C#开发最快的浏览器,打造极速浏览体验
开发语言·c#
战族狼魂13 小时前
C#的数据类型总结:decimal ,double,float的区别
开发语言·c#
中游鱼13 小时前
万能程序补丁工具 C# 源代码详解
c#·万能程序补丁工具·源代码详解