winform计算器
引言
本次项目目的主要为了熟悉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计算器的更多相关文章
- winform 计算器
		
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...
 - Winform——计算器
		
namespace 计算器2._0 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } pr ...
 - Winform——计算器进制转换
		
namespace 进制转换2._0 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } p ...
 - [原][C#][winForm]分级基金折溢价WinForm网络计算器
		
分级基金折溢价WinForm网络计算器 通过子/母基金代码,从 [ 东方财富网,天天基金网,新浪 ] 抓取分级基金的子母基金数据(代码,名称,净值,价格), 并计算出子基金(A基金,B基金)以及母基金 ...
 - winform操作windows系统计算器
		
winform对系统计算器的调用,启动,最大化最小化显示,在mainwindow设置topmost=true时,正常显示计算器并置顶. /// <summary> /// 获取窗体的句柄函 ...
 - 1.C#WinForm基础制作简单计算器
		
利用c#语言编写简单计算器: 核心知识点: MessageBox.Show(Convert.ToString(comboBox1.SelectedIndex));//下拉序号 MessageBox.S ...
 - winform(四)——简单计算器制作
		
效果图: 代码区: using System; using System.Collections.Generic; using System.ComponentModel; using System. ...
 - winform制作简单计算器
		
public Form1() { InitializeComponent(); textBox2.Text = ";//主显示屏 textBox1.Text = "";/ ...
 - WinForm 制作一个简单的计算器
		
namespace WindowsFormsApplication6 { public partial class Form1 : Form { //存储上次点击了什么按钮,0代表什么都没有点击,1代 ...
 - C# winform(计算器)
 
随机推荐
- "揭秘CentosChina爬虫项目:掌握Scrapy框架的必备技巧与数据库设计"
			
Centoschina 项目要求 爬取centoschina_cn的所有问题,包括文章标题和内容 数据库表设计 库表设计: 数据展示: 项目亮点 低耦合,高内聚. 爬虫专有settings custo ...
 - 疫情远程办公Citrix XenDesktop 2203长期稳定版本虚拟云桌面部署教程
			
下面列出来的是目录大纲,里面包含了Citrix完整搭建的教程,此次实验环境是利用Citrix XenDesktop 7.15 CU3长期稳定版虚拟桌面详细安装教程, 含PVS及UPM相关配置,期待视频 ...
 - 面试官:说说volatile应用和实现原理?
			
volatile 是并发编程中的重要关键字,它的名气甚至是可以与 synchronized.ReentrantLock 等齐名,也是属于并发编程五杰之一. 需要注意的是 volatile 并不能保证原 ...
 - AtCoder Beginner Contest 311
			
Toyota Programming Contest 2023#4(AtCoder Beginner Contest 311) A - First ABC (atcoder.jp) 记录一下\(ABC ...
 - MYSQL——mysql检索不包含字母U的数据
			
2024/07/09 1. NOT LIKE 2. IS NOT.<>.!= 3. NOT IN 如题,正确答案如下: SELECT * FROM your_table_name WHER ...
 - Vue  子组件修改父组件传递过来的值
			
实现效果:通过点击选中的按钮控制左边的树是否进行展示 子组件篇: <el-button v-if="isShowTree&hasTree" type="te ...
 - vue 报错 Component template should contain exactly one root element. If you are using v-if on multiple elements
			
vue 报错 Component template should contain exactly one root element. If you are using v-if on multiple ...
 - idea 的Tomcat 的简单配置
			
不限速网盘下载: https://kohler.lanzouv.com/iSSfc0akw3vc 官网下载: https://tomcat.apache.org/download-80.cgi 正常解 ...
 - MySQL read view 在RR和RC隔离级别下的异同
			
1.首先了解下什么是read view 这里说的 read view 是InnoDB 在实现 MVCC 时用到的一致性读视图,即 consistent read view,用于支持 RC(Read C ...
 - Kubelet证书自动续签(为 kubelet 配置证书轮换)
			
1.概述 Kubelet 使用证书进行 Kubernetes API 的认证. 默认情况下,这些证书的签发期限为一年,所以不需要太频繁地进行更新. Kubernetes 包含特性 Kubelet 证书 ...