How to do this ?

  • Thread starter Thread starter tsair
  • Start date Start date
T

tsair

In TextBox1 have string value as bellow:
TextBox1.Text = "24-10*10/3"

After the user press enter or tab key from TextBox1, the TextBox2 will get
the result as 46.666
TextBox2.Text = 46.666
 
Write these as follow in 'TextBox1.Exit' event:

double r = Int.Parse(TextBox1.Text);

TextBox2.Text = r.ToString();



I 've never tried it, I think it can be OK.
 
It prompt me "Input string was not in a correct format."

I amend this,
TextBox1.Text = "24-10*10/3"
decimal r = decimal.Parse( TextBox1.Text.Trim() ) ;
TextBox2.Text = r.ToString()

Thank you for your reply
 
The decimal.Parse method is by no means an expression evaluator.

You should look for one if you need to evaluate the expression.

An easy way to evaluate expressions is to use JScript.NET

Compile the following:

class Evaluator
{
static function Evaluate(data : String) : Object
{
return eval(data);
}
}

using

jsc /t:library eval.js

This will produce a small eval.dll, which you can reference in your
project (along with Microsoft.JScript) and then use like:

using System;

class Program
{
static void Main()
{
string expr = "24-10*10/3";

Console.WriteLine("{0} = {1}", expr, Evaluator.Evaluate(expr));
}
}

HTH,
Stefan
 
Nope, Int16 (among others) has a Parse method, but ...
The s parameter contains a number of the form:

[ws][sign]digits[ws]

Items in square brackets ([ and ]) are optional; and other items are as follows.

ws
An optional white space.
sign
An optional sign.
digits
A sequence of digits ranging from 0 to 9.
 
Is that any other way that we can do in C# coding ?

Certainly:
write your own math expression Parser (+Tokenizer) and then a class
that can evaluate a parsed math expression.

It all starts with a BNF grammar of what math expressions you want to
evaluate (what operators, what precedence and associativity each has,
do you allow for parenthetical expressions, ...). Fun stuff !

Oh, once you do that, you're on your way to write a compiler :)

If you don't have the time or inclination to do that, look for third
party libraries.

HTH,
F.
 
Wah! So many coding to do just to evalution a expression.

Thank you for your reply


Olorin said:
Is that any other way that we can do in C# coding ?

Certainly:
write your own math expression Parser (+Tokenizer) and then a class
that can evaluate a parsed math expression.

It all starts with a BNF grammar of what math expressions you want to
evaluate (what operators, what precedence and associativity each has,
do you allow for parenthetical expressions, ...). Fun stuff !

Oh, once you do that, you're on your way to write a compiler :)

If you don't have the time or inclination to do that, look for third
party libraries.

HTH,
F.
 
tsair said:
Wah! So many coding to do just to evalution a expression.

Thank you for your reply


Is that any other way that we can do in C# coding ?

Certainly:
write your own math expression Parser (+Tokenizer) and then a class
that can evaluate a parsed math expression.

It all starts with a BNF grammar of what math expressions you want to
evaluate (what operators, what precedence and associativity each has,
do you allow for parenthetical expressions, ...). Fun stuff !

Oh, once you do that, you're on your way to write a compiler :)

If you don't have the time or inclination to do that, look for third
party libraries.

HTH,
F.

And If you don't want to use third party libraries, you should probably
try the way I've recommended... ;) Anyway, what's wrong with it?

HTH,
Stefan
 
Back
Top