Fastest way to develop a calculator C# - string expression to mathematical expression

Fastest way to develop a calculator C# - string expression to mathematical expression

Friends today I will tell you something very amazing. The easiest and fastest way to create a calculator in C#. You would have earlier created calculator by adding two textbox on form where you can enter two numbers and then operator buttons to calculate the result. For example you entered 5 in textbox1 and 7 in textbox2 then if you click on multiply button on the form you get the result as 35. Similar you can design other operator buttons like add, subtract and division and they will work in similar way. The only issue with this kind of calculator is that you can have only 2 operands and one operator in a expression. With this kind of calculator it is difficult to do huge calculations.
C# Technical Tips and Tricks
Fastest way to develop a calculator in C# language. Convert string expression into mathematical expression.
So the solution to this issue is to have a calculator which can compute the value of an expression. So here the concept of an expression calculator arises. Many of you would be aware of expression calculator where in one textbox you will enter full expression 5+6-2*1 and then when you will click on calculate it will show the result 9. So it is good for large and small calculations both. It is far better and most used calculator.
Now coming to its implementation:
Traditional approach will be to write a huge program:
1. You will parse whole string in terms of operands and operators.
2. You will remember their sequences.
3. You will have to remember the BODMAS implementation works correctly as you yourself are creating this calculator from scratch.
4. It will surely take atleast 4 hours if you are doing it for first time.

Now I will tell you the tricky way of doing this. You can create a calculator with in 5 minutes though we will definitely use some already defined functions. But I assure you that you can bring the result of any string expression. You don't have to parse anything.

So lets assume you have a textbox with ID Textbox1 to enter the expression. Then a Calculate button and a result Textbox2 to show the result.
So you can do this as follows. In your code behind file write
using System.Data;
//On button click you have to write these lines
DataTable dt = new DataTable();
Textbox2.Text=dt.Compute(Textbox1.Text,"");

//that's it. You have implemented your calculator with help of Compute method available in System.Data.
Try this and let me know if you face any issue. If it sounds interesting to you then definitely share it with your programmer friends. #FrandsCodeKarLo #Hahaha

Other important links to explore

datatable compute c#, CSharp Tips and Tricks, create calculator in C#, develop c# calculator

Comments

Popular posts from this blog

Convert to two decimal places C# Round to 2 decimal places

Indore is actually the cleanest city of India

Do you know how many toilets have been built in Swachh Bharat Mission?

When you actually meet your Achchhe Din

Total Pageviews