多行文本框值的总和 [英] sum of multiline textbox values

查看:118
本文介绍了多行文本框值的总和的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的彼得,



这是我的新朋友,所以我想创建我的简单项目,所以PLZ帮助我

不要关闭这个文章。

plz给我一个想法代码解决方案

i尝试下面的代码,但我在使用多个支架时面临问题



int sum = textBox1.Lines.Sum(line => int.Parse(new String(line。

Where(x => Char.IsDigit(x))。ToArray( )));



如果我输入如下所示的多行文本框

15-A。

15 -B。

{[2-A

2-B] 3。

5-C} 2.

8-D



总计= 72



当我点击时,我想在TEXTBOX2中找到TEXTBOX1 SUM BUTTON

TOTAL应该是72

Dear peter,

iam new beginer in this so i want to creat my simple project so plz help me
dont close this article.
plz give me a idea code solution
i trying below code but i have facing problem in use of multiple brackets

int sum = textBox1.Lines.Sum(line => int.Parse(new String(line.
Where(x => Char.IsDigit(x)).ToArray())));

if i entered in multiline textbox1 like below
15-A.
15-B.
{[2-A
2-B]3.
5-C}2.
8-D

TOTAL = 72

I WANT SUM OF TEXTBOX1 IN TEXTBOX2 WHEN I CLICK SUM BUTTON
TOTAL SHOULD BE 72

推荐答案

没有简单的解决方案 - 你不能只假设Parse会转换
There is no "simple" solution to that - you can't just assume that Parse will convert
[2-A 2B]3

进入

(2 + 2) * 3

这就是你要求它做:帕尔斯e将123转换为整数值123,而不是其他任何东西。如果它意味着任何不是数字的东西,它就会失败。所以你的Linq表达式(基本上抛弃了非数字字符)可以尝试添加

which is what you are asking it to do: Parse converts "123" into an integer value 123, and nothing else. If it means anything which isn't numeric, it fails. So your Linq expression (which basically throws away non-numeric characters) works out to trying to add

15
15
2
23
52
8

永远不会是72 ...



如果你想威胁你所描述的方式中的文本(坦率地说这很奇怪)然后你需要构建一个完整的词法解析器来查看整个数据,并准确地计算它应该用它做什么。 (AFAIK)没有解析器可以处理该格式的数据。

Which is never going to be 72...

If you want to threat that text in the way you describe (which frankly is rather odd) then you will need to build an entire lexical parser to look at the entire data and work out exactly what it is supposed to do with it. There is (AFAIK) no parser which works with data in that format.


尝试使用数学表达式求值器 NCalc [ ^ ]

例如

try with Mathematical Expressions Evaluator NCalc[^]
for example
Expression e = new Expression("2 + 3 * 5");
var result =e.Evaluate(); // 17 



要使用NCalc你需要使用您的输入构建有效的表达式。尝试从输入中删除字母并用+连接每一行,并将{和[等替换为(......等等......



最后你需要有如下表达式


To use NCalc you need to build valid expression using your input. Try to remove letters from the input and join each line with "+" and replace "{" and "[" etc with "(" ... so on...

at the end you will need to have expression like below

Expression e = new Expression("15+15+((2+2)*3+5)*2+8");
var result =e.Evaluate(); // 72 


这篇关于多行文本框值的总和的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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