引言

本次项目目的主要为了熟悉winform控件使用,以及学习Microsoft.CSharp的使用。

技术栈

C# winform

实现效果

设计与实现

按键使用button空间,计算算式以及计算结果使用label控件。运算核心使用Microsoft.CSharp,利用C#动态编译特性,将算式转换成源码,交给编译器计算。


//算式运算代码
public static double EvaluateExpression(string expression)
{
CSharpCodeProvider provider = new CSharpCodeProvider();//提供编译源码的方法
CompilerParameters parameters = new CompilerParameters();//提供编译过程中的各种参数
parameters.GenerateInMemory = true;//不生成物理文件,使用内存
//动态编译时需要指向它所需要的库
parameters.ReferencedAssemblies.Add("mscorlib.dll");
parameters.ReferencedAssemblies.Add("System.Core.dll");
//CompilerResults存储编译后的结果
//@后定义的是c#源码
//编译器会将源码执行,自动进行算术运算,返回expression的结果
CompilerResults results = provider.CompileAssemblyFromSource(parameters, @"
using System;
public class ExpressionEvaluator
{
public static double Evaluate()
{
return " + expression + @";
}
}");
//编译出错返回
if (results.Errors.HasErrors)
{
throw new ArgumentException("Error compiling expression.", nameof(expression));
}
//通过反射获取程序集中的ExpressionEvaluator
Type evaluatorType = results.CompiledAssembly.GetType("ExpressionEvaluator");
//获取"Evaluate"方法
MethodInfo methodInfo = evaluatorType.GetMethod("Evaluate");
//返回结果(第一个null表示:调用静态方法,第二个null表示:方法没有参数)
return (double)methodInfo.Invoke(null, null);
}

挑战与解决方案

原本想要直接将算式String分解,一步一步运算,但是太麻烦,所以采用Microsoft.CSharp

winform计算器的更多相关文章

  1. winform 计算器

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...

  2. Winform——计算器

    namespace 计算器2._0 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } pr ...

  3. Winform——计算器进制转换

    namespace 进制转换2._0 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } p ...

  4. [原][C#][winForm]分级基金折溢价WinForm网络计算器

    分级基金折溢价WinForm网络计算器 通过子/母基金代码,从 [ 东方财富网,天天基金网,新浪 ] 抓取分级基金的子母基金数据(代码,名称,净值,价格), 并计算出子基金(A基金,B基金)以及母基金 ...

  5. winform操作windows系统计算器

    winform对系统计算器的调用,启动,最大化最小化显示,在mainwindow设置topmost=true时,正常显示计算器并置顶. /// <summary> /// 获取窗体的句柄函 ...

  6. 1.C#WinForm基础制作简单计算器

    利用c#语言编写简单计算器: 核心知识点: MessageBox.Show(Convert.ToString(comboBox1.SelectedIndex));//下拉序号 MessageBox.S ...

  7. winform(四)——简单计算器制作

    效果图: 代码区: using System; using System.Collections.Generic; using System.ComponentModel; using System. ...

  8. winform制作简单计算器

    public Form1() { InitializeComponent(); textBox2.Text = ";//主显示屏 textBox1.Text = "";/ ...

  9. WinForm 制作一个简单的计算器

    namespace WindowsFormsApplication6 { public partial class Form1 : Form { //存储上次点击了什么按钮,0代表什么都没有点击,1代 ...

  10. C# winform(计算器)

随机推荐

  1. 【入门教程】5分钟教你快速学会集成Java springboot ~

    介绍 Apache DolphinScheduler是一个分布式易扩展的开源分布式调度系统,支持海量数据处理,具有任务流程调度.任务流程编排.任务监控告警.工作流引擎等功能. 本文将介绍如何将Apac ...

  2. 利用标准IO函数接口实现文件拷贝

    把本地磁盘的文件A中的数据完整的拷贝到另一个文本B中,如果文本B不存在则创建,要求文本A的名称和文本B的名称通过命令行传递,并进行验证是否正确. /************************** ...

  3. nvic stm32g0

    STM32G0 32个可屏蔽中断通道 4个可编程优先等级(抢占等级:0~3) 低延时异常和中断处理 电源管理控制

  4. vscode 下配置 clang

    需要在workspace的文件夹下添加文件: .clang-format 更多参数说明: https://clang.llvm.org/docs/ClangFormatStyleOptions.htm ...

  5. Ubuntu 设置远程桌面(VNC)

    连接 Xfce 4 远程桌面 下载 Xfce 4 桌面环境: sudo apt install -y xfce4 xfce4-goodies 这里会提示你设置显示管理器,我们设置 gdm3 就好. 安 ...

  6. 【2019年第一篇总结】之Mac安装Python系列软件目录汇总

    1.第一步,安装Python环境 <[Mac + Python]苹果系统之安装Python3.6.x环境> 2.安装PyCharm并激活 <[转载][Pycharm编辑器破解步骤]之 ...

  7. 开源大模型占GPU显存计算方法

    运行大模型GPU占用计算公式: \(M=\frac{(P * 4B)}{32 / Q} * 1/2\) M : 以GB标识的GPU内存 P : 模型中的参数数量,例如一个7B模型有70亿参数 4B : ...

  8. springboot 集成 onlyoffice 实现文档预览、编辑、pdf转化、缩略图生成

    开源地址 https://gitee.com/lboot/lucy-onlyoffice 介绍 lucy-onlyoffice是依赖于onlyoffice的springboot文档预览编辑集成解决方案 ...

  9. DOM & BOM – Input File, Drag & Drop File, File Reader, Blob, ArrayBuffer, File, UTF-8 Encode/Decode, Download File

    前言 之前写过 2 篇关于读写文件和二进制相关的文章 Bit, Byte, ASCII, Unicode, UTF, Base64 和 ASP.NET Core – Byte, Stream, Dir ...

  10. Figma 学习笔记 – Layout Grid

    前言 我原本以为, 在 Figma 只要用 Auto Layout 就可以打天下. 真的是 too young too simple. 要做一个简单的 7:3 比例, 用 Auto Layout 是做 ...