当前位置: 首页 > article >正文

C#桌面应用制作计算器

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

页面实现效果f5daa1720da24927b1bea8d986610baa.png

功能实现

布局

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

布局效果

1f401ee0f36d412c86a9d32d934a4ca7.png

数字按键功能

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

数字按键功能实现效果56531a5a10fa4de0aec077eb64c2f980.jpeg

数字按键功能实现代码

 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并投射到屏幕上。

运算符按键功能实现效果

67025697bb754c34b1573bcdfe8789df.jpeg

运算符按键功能实现代码

 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;
             }
         }
     }
 }

fc5eab724c064e31a69db83569d09e67.jpeg

AC按键功能

点击清除所有信息。

AC按键功能实现效果

b7366c9f8a6e494f82956f40d67ba227.jpeg

AC按键功能实现代码

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

Del按键功能

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

Del按键功能实现效果

6c46b6b178da430182f8e7655f846ef0.jpeg

Del按键功能实现代码

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"(按键‘.’同样效果)。

+/-按键功能实现效果

3617ec09196f41fd80797efa07ceacd0.jpeg

+/-按键功能实现代码

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"。

%按键功能实现效果

1118c7dc9bd04dafbc6199824006897f.jpeg

%按键功能实现代码

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"。

等号按键功能实现效果

0fe2986654cc4e0798863c2072115a6a.jpeg

等号按键功能实现代码

 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;
         }
     }
 }

完整代码实现

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;
                }
            }
        }
    }
}

 

 

 

 


http://www.kler.cn/a/395723.html

相关文章:

  • python程序对服务器cpu和内存资源占用的管理。
  • JSON.stringify的应用说明
  • 使用热冻结数据层生命周期优化在 Elastic Cloud 中存储日志的成本
  • 无人机应用场景:石油管道巡检技术详解
  • 删库跑路,启动!
  • 浅谈“通感一体”
  • 开源项目推荐——OpenDroneMap无人机影像数据处理
  • Oracle 单机及 RAC 环境 db_files 参数修改
  • Linux基础—pxe装机
  • 【拉箱子——模拟+DFS】
  • JAVA学习-练习试用Java实现“网络编程”
  • LlamaFactory介绍
  • Java爬虫:获取商品历史价格信息 API 数据
  • 英伟达基于Mistral 7B开发新一代Embedding模型——NV-Embed-v2
  • CTF攻防世界小白刷题自学笔记12
  • 企业生产环境-麒麟V10(ARM架构)操作系统部署kafka高可用集群
  • Lambda常用方法
  • Kafka、RabbitMQ、RocketMQ对比
  • 开源对象存储新选择:在Docker上部署MinIO并实现远程管理
  • sql在按照当前表查询返回
  • 聊天服务器(9)一对一聊天功能
  • 求10000以内n的阶乘
  • SpringBoot开发——整合AJ-Captcha实现安全高效的滑动验证码
  • day-82 最少翻转次数使二进制矩阵回文 I
  • SQL LEFT JOIN 简介
  • windbg的线程信息dt命令