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 下面。
网盘下载全部工程文件及字体文件:
百度网盘链接 提取码 7jv4https://pan.baidu.com/s/1dbWtYfelgIZLggIPPFPKTA?pwd=7jv4 提取码:7jv4
相关文章
- C#-CHTTPDownload
- C#中Stack<T>类的使用及部分成员函数的源代码分析
- 常量,字段,构造方法 调试 ms 源代码 一个C#二维码图片识别的Demo 近期ASP.NET问题汇总及对应的解决办法 c# chart控件柱状图,改变柱子宽度 使用C#创建Windows服务 C#服务端判断客户端socket是否已断开的方法 线程 线程池 Task .NET 单元测试的利剑——模拟框架Moq
- C#字符串数组排序 C#排序算法大全 C#字符串比较方法 一个.NET通用JSON解析/构建类的实现(c#) C#处理Json文件 asp.net使用Jquery+iframe传值问题
- Word控件Spire.Doc 【脚注】教程(4): 在 Word 中插入或删除脚注-C#/VB.NET
- Word控件Spire.Doc 【邮件合并】教程(六): 使用 C# 识别 Word 中的合并字段名称
- C# (江湖小新)- 多线程之线程池 (线程也有专门的管理部门)
- C#【中级篇】typeof和GetType()的使用
- C#,超级阿格里数字(超级丑数,Super Ugly Number)的算法与源代码
- C#,蛇梯问题(Snake and Ladder Problem)的算法与源代码
- C#,老鼠迷宫问题的回溯法求解(Rat in a Maze)算法与源代码
- C#,数据检索算法之三元搜索(Ternary Search)的源代码
- C#,迭代深化搜索(IDS)或迭代深化深度优先搜索(IDDFS)算法的源代码
- C#,归并排序算法(Merge Sort Algorithm)的源代码及数据可视化
- C#,数值计算,矩阵相乘的源代码与数据可视化
- C#,深度优先搜索(DFS)、广度优先搜索(BFS)算法的源代码与数据可视化
- C#,码海拾贝(20)——一般实矩阵的奇异值分解(Singular Value Decomposition)方法之C#源代码,《C#数值计算算法编程》源代码升级改进版
- C#,码海拾贝(12)——拉格朗日(Lagrange)曲面插值算法,《C#数值计算算法编程》源代码升级改进版
- C#,码海拾贝(10)——三次样条(Cubic Spline)曲线插值算法,《C#数值计算算法编程》源代码升级改进版
- C#,码海拾贝(04)——拉格朗日(Lagrange)曲线插值算法及其拓展,《C#数值计算算法编程》源代码升级改进版
- 用C#实现通用守护进程
- c# 属性说明
- C#委托的发布和订阅
- C#-DataTable与实体类互相转换
- C#-IO-文件读写并显示文件读取进度
- C# 正则表达式