C#评估扩展 [英] C# evaluate expreassion

查看:86
本文介绍了C#评估扩展的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我是新手,为此我深表歉意,但我需要帮助.

我想读取输入字符串Ex1 var1-var!= var3
例2. var1 + var2 == var3
例3. var1 + var2> = var3


该字符串将包含数字Ex. 201-200!= 1或300-400< 50

输入限制为整数.我正在寻找最简单的方法.

如何验证输入的expession?(是或否)

我可以使表达式动态化,以便输入变量的数量无关紧要吗?我可以使用+-*/以及不同的运算符,分别大于,等于,小于等,

感谢我能得到的所有帮助.

//Nicke

解决方案

我错过了什么,还是只需要一个简单的代数?
对于所有整数,n-(n + 1)等于-1,因此无论"n"上的值(或数据类型)如何,表达式始终为true.


好吧,正如Bob指出的那样,解析主题很大.另外,您需要做的事情和需要显示的内容可能并不总是相同的(多数民众赞成在涉及临时黑客时:))我想给您一个概述.首先定义您的规则.假设您的输入是表达式",则规则可能类似于:

表达式:relational_expression.
Relational_expression:a_expression.
相关表达式:a_expression relop a_expression.
a_expression:term.
a_expression:a_expression addop术语.
期限:因素.
术语:术语mulop因子.

因素:数字.
因素:'  true'.
因素:'  false'.

addop:'  +'/'-'.
mulop:'  *'/'/'/'%'.
relop:'  =='/'!='/'<' /'>' /'< ='/'> ='. 


请注意,此处考虑了运算符优先级,这就是规则集有点长的原因.例如,您不想在"300-400< 50"中的"300-400"之前评估"400< 50".
我没有讲这些规则的细节,可能会争辩说这太长,太短,多汁或什至是糊状……无论如何,遵循这些规则,您会发现首先需要找到比较运算符并对其进行左右评估,休息取决于您.请注意,以上规则仅是准则而非代码. System.Text.RegularExpressions [


Hi all,

I am a newbie and I apologize for that but I need help.

I want to read an input string Ex1 var1-var != var3
Ex2. var1 + var2 == var3
Ex3. var1 + var2 >= var3


The string will consist of numeric Ex. 201-200!=1 or 300-400<50

The inputs are restricted to integers. I am looking for the easiest way.

How can I verify the input expession?(true or false)

Can I make the expression dynamic so that the number of input variables dosen''t matter and I can use + - * / and different operators greater then, equal, less then and so on.

Grateful for all help I can get.

//Nicke

Am I missing something, or is all you need a little simple algebra?
For all integers n - (n + 1) is equal to -1, so you expression will always be true, regardless of the value (or datatype) on "n".

Why are you asking this re C#?


Well, as Bob pointed out the parsing subject is very big one. Also, what you need to do and what you need to show may not always the same ( thats when ad hoc hacking involves :) ) I''d like to give you an outline. First define your rules. Lets say your input is an "expression" then rules might be like:

expression: relational_expression .
relational_expression: a_expression .
relational_expression: a_expression relop a_expression .
a_expression: term .
a_expression: a_expression addop term .
term: factor .
term: term mulop factor .

factor: number .
factor: 'true' .
factor: 'false' .

addop: '+' / '-' .
mulop: '*' / '/' / '%' .
relop: '==' / '!=' / '<' / '>' / '<=' / '>=' .


Note that here operator precedence is taken in to account thats why rule set is a little long. For instance, you do not want to evaluate "400<50" before "300-400" in "300-400<50".
I am not going into details of those rules, one might argue that this is too long, short, juicy or even squishy... Whatever, following those rules you see that first you need to locate comparison operator and evaluate left and right of it, rest is up to you. Note that above rules are just guideline not code.
System.Text.RegularExpressions[^] namespace has all the tools you need to chop down input strings.


这篇关于C#评估扩展的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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