有没有将评估一个字符串并产生一个整数的方法(假设字符串是一个公式)在C# [英] Is there a method that will evaluate a string and produce an integer (assuming the string is an equation) in C#

查看:171
本文介绍了有没有将评估一个字符串并产生一个整数的方法(假设字符串是一个公式)在C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,我知道,在Python中,假设你有一个名为

Okay, I know that in Python, say you had a string called

strExpression,它的价值是1 + 2-(3 * 5)

strExpression and it's value was "1+2-(3*5)"

有是一个Python方法(函数,对不起,我得到的术语混淆),将评估该字符串并返回一个数字整数值(在这种情况下,它应该返回-12。我不记得了Python语法,因为我在一门课程前一阵子做了(然后我打破了我的胳膊,不得不退出,因为我不能输入),但希望有人知道我是什么意思

There is a python method (function, sorry I get the terms confused) that will evaluate that string and return a numerical integer value (in this case, it should return -12. I don't recall the python syntax because I did it a while ago in a course (and then I broke my arm and had to quit because I couldn't type), but hopefully someone knows what I mean

有没有办法在C#中做到这一点。

Is there a way to do this in C#

我只是学习,正在创建一个简单的计算器程序,我已经一个基本的,允许你添加到先前的数字,但我想扩展它有点为自己的利益。非常感谢您的任何帮助。

I am only learning, and am creating a simple calculator program, and I have made a basic one that allows you to add to the previous number, but I would like to extend it a bit for my own benefit. Thank you very much for any help.

推荐答案

您可以使用它暴露的所有客户端的Microsoft.JScript程序在C#中的JavaScript函数

You can use the Microsoft.JScript which exposes all client side javascript functions in C#

using System;
using Microsoft.JScript;
using Microsoft.JScript.Vsa;

namespace JustTest
{
    class Program
    {
        static void Main(string[] args)
        {
            object value = Eval.JScriptEvaluate("1+2-(3*5)", VsaEngine.CreateEngine());
            Console.Write(value);
            Console.ReadLine();
        }
    }
}

这篇关于有没有将评估一个字符串并产生一个整数的方法(假设字符串是一个公式)在C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆