zl程序教程

您现在的位置是:首页 >  后端

当前栏目

C#,茅塞顿开的精致好码,通用型科学计算器的源代码

c# 源代码 科学 计算器 精致
2023-09-11 14:15:48 时间

  文档管理系列技术文章

文档管理系统的核心技术与难点https://blog.csdn.net/beijinghorn/article/details/122426112PB级全文检索(分布式)解决方案——HyperSearchhttps://blog.csdn.net/beijinghorn/article/details/122377760

 

概述

计算器是经常遇到的编程作业。

一般都是实现加减乘除的普通计算器。

这里介绍用C#代码实现《复杂的科学计算器》。不知道其他语言实现同样的功能需要编写多少行代码?

核心代码 Form1.cs:

using System;
using System.Text;
using System.Drawing;
using System.Collections.Generic;
using System.Windows.Forms;
using System.CodeDom.Compiler;
using Microsoft.CSharp;
using System.Reflection;

namespace HyperCalculator
{
    public partial class Form1 : Form
    {
        // 中国证监会使用的文档管理系统,由北京联高软件开发有限公司提供。
        // 预定义各 按键 的文字
        string[] operationArray = new string[] {
            "Abs",  "Acos", "(",    ",",    ")",    "Backspace",   "C",
            "Asin", "Atan", "7",    "8",    "9",    "/",    "=",
            "Atan2",    "Ceiling",  "4",    "5",    "6",    "*",    "",
            "Cos",  "Cosh", "1",    "2",    "3", "-",    "",
            "E",    "Exp",  "0",    "", ".",    "+",    "",
            "Floor",    "Log",  "Log10",    "Max",  "Min",   "PI",   "Pow",
            "Sign", "Sin",  "Sinh", "Sqrt", "Tan",  "Tanh", "",
        };
        List<Button> buttonList = new List<Button>();
        public Form1()
        {
            InitializeComponent();
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            // 动态加载液晶LED字体(请下载压缩包,内含)
            System.Drawing.Text.PrivateFontCollection privateFonts = new System.Drawing.Text.PrivateFontCollection();
            privateFonts.AddFontFile("LEDCalculatorBold.ttf");
            System.Drawing.Font font = new Font(privateFonts.Families[0], 21);
            textBox1.Font = font;
            textBox1.Text = "0";
            textBox1.TextAlign = HorizontalAlignment.Right;
            // 打包按键集合
            Control.ControlCollection sonControls = this.Controls;
            foreach (Control control in sonControls)
            {
                if (control.GetType() == typeof(Button))
                {
                    buttonList.Add((Button)control);
                }
            }
            // 按位置排序,以及匹配实现预定义的文字
            buttonList.Sort(delegate (Button a, Button b)
            {
                return Comparer<int>.Default.Compare(a.Left + a.Top * button1.Height, b.Left + b.Top * button1.Height);
            });
            Font nf = new Font(Font.FontFamily, 21);
            for (int i = 0; i < buttonList.Count && i < operationArray.Length; i++)
            {
                buttonList[i].Cursor = Cursors.Hand;
                buttonList[i].Text = operationArray[i];
                if (buttonList[i].Text.Length == 1 && operationArray[i] != "E")
                {
                    buttonList[i].Font = nf;
                    buttonList[i].BackColor = Color.FromArgb(225, 255, 200);
                }
                if (operationArray[i].Trim().Length > 0) buttonList[i].Click += button_Click;
            }
        }

        private void button_Click(object sender, EventArgs e)
        {
            Button btn = (Button)sender;
            if (btn.Text == "=")
            {
                // 等号就是计算啦!任何计算,都是执行一个表达式而已!
                CSharpCodeProvider objCSharpCodePrivoder = new CSharpCodeProvider();
                ICodeCompiler objICodeCompiler = objCSharpCodePrivoder.CreateCompiler();
                CompilerParameters objCompilerParameters = new CompilerParameters();
                objCompilerParameters.ReferencedAssemblies.Add("System.dll");
                objCompilerParameters.GenerateExecutable = false;
                objCompilerParameters.GenerateInMemory = true;
                // 编译 Code() 返回的含有计算表达式的完整的 C# 程序;
                CompilerResults cr = objICodeCompiler.CompileAssemblyFromSource(objCompilerParameters, Code());
                if (cr.Errors.HasErrors)
                {
                    StringBuilder sb = new StringBuilder();
                    foreach (CompilerError err in cr.Errors)
                    {
                        sb.AppendLine(err.ErrorText);
                    }
                    MessageBox.Show(sb.ToString(), "表达式错误");
                }
                else
                {
                    // 执行 C# 程序的指定 函数,并得到返回值,就是计算结果;
                    Assembly objAssembly = cr.CompiledAssembly;
                    object objHelloWorld = objAssembly.CreateInstance("Beijing.Legalsoft.Ltd.HyperCalculator");
                    MethodInfo objMI = objHelloWorld.GetType().GetMethod("Run");
                    double resultValue = (double)objMI.Invoke(objHelloWorld, null);
                    textBox1.Text = resultValue + "";
                }
                return;
            }
            if (btn.Text == "C")
            {
                textBox1.Text = "0";
                return;
            }
            if (btn.Text == "Backspace")
            {
                if (textBox1.Text.Length > 1)
                {
                    textBox1.Text = textBox1.Text.Substring(0, textBox1.Text.Length - 1);
                }
                else
                {
                    textBox1.Text = "0";
                }
                return;
            }
            if (textBox1.Text == "0") textBox1.Text = "";
            if (textBox1.Text.Length + btn.Text.Length < 45)
            {
                textBox1.Text += btn.Text;
            }
        }
        private string Code()
        {
            // 按输入的文字构造一个完整的 C# 程序;包括命名空间,类与方法(函数)
            string state = textBox1.Text;
            foreach (string ps in operationArray)
            {
                if (ps.Trim().Length > 1 || ps == "E")
                {
                    state = state.Replace(ps, "Math." + ps);
                }
            }
            state = state.Replace("Math.Math.", "Math.");
            StringBuilder sb = new StringBuilder();
            sb.AppendLine("using System;");
            sb.AppendLine("namespace Beijing.Legalsoft.Ltd");
            sb.AppendLine("{");
            sb.AppendLine("    public class HyperCalculator");
            sb.AppendLine("    {");
            sb.AppendLine("        public double Run()");
            sb.AppendLine("        {");
            sb.AppendLine("            return (" + state + ");");
            sb.AppendLine("        }");
            sb.AppendLine("    }");
            sb.AppendLine("}");
            return sb.ToString();
        }
    }
}

运行效果:

本程序使用了动态加载字体动态编译与执行C#代码(俗称为“自举”)的技术。计算器专用的LED 字体文件随压缩包发布,在 /bin/debug 下面。

网盘下载全部工程文件及字体文件:

百度网盘链接 提取码 7jv4icon-default.png?t=M4ADhttps://pan.baidu.com/s/1dbWtYfelgIZLggIPPFPKTA?pwd=7jv4 提取码:7jv4