Click here to Skip to main content
15,886,578 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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
Posted
Comments
DamithSL 2-Jul-14 3:01am    
how you get answer as 72 from above data?
cadsolution 2-Jul-14 3:14am    
15-A
15-A IS 30
{[2-A
2-B]3
5-C}2.
MEANS FIRST (2-A + 2-B) * 3 TIMES = I.E 12
AGAIN BIG BRACKET (5-C .IE 12+5-C)*2 TIMES = 34
LAST 8-D

FINAL SUM IS = 72


George Jonsson 2-Jul-14 3:40am    
Maybe you should update your question with the rules of your calculation.
For me it is not clear how 15 - A = 30 (or did I misunderstand?)

You should probably look into Regular Expressions for parsing the data in the textbox.

There is no "simple" solution to that - you can't just assume that Parse will convert
[2-A 2B]3
Into
(2 + 2) * 3
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
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.
 
Share this answer
 
try with Mathematical Expressions Evaluator NCalc[^]
for example
C#
Expression e = new Expression("2 + 3 * 5");
var result =e.Evaluate(); // 17 

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
C#
Expression e = new Expression("15+15+((2+2)*3+5)*2+8");
var result =e.Evaluate(); // 72 
 
Share this answer
 
Comments
cadsolution 2-Jul-14 3:50am    
dear damithSL

thanks for answer but i want user enterd values total
iam using below code like
int sum = textBox1.Lines.Sum(line => int.Parse(new String(line.
Where(x => Char.IsDigit(x)).ToArray())));
this can be used enterd any value
but i could not code when i using multi brackets

plz help me.
DamithSL 2-Jul-14 4:05am    
yes, that code also I have given in your previous question which already closed.
you need to try yourself to do at least some basic string manipulations to achieve your goal.
don't expect full functioning code from code project for your requirements. You already have enough guide lines to try yourself. Check the OriginalGriff's answer as well.
cadsolution 2-Jul-14 9:03am    
iam trying myself,but iam unable to get correct so plz help us.
cadsolution 5-Jul-14 1:36am    
dear friend,
iam unable to get the idea of coding.plz this time help me.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900