Click here to Skip to main content
15,920,513 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
for example:

i have " 25+5-2*2", how can i calculate this equation entered in one textbox taking in consideration the priority of " *".
If someone can write me the code please,
I Need Help!
Posted
Comments
Kornfeld Eliyahu Peter 4-Nov-14 9:48am    
No. We do not write code for your - or anyone else for that matter. You have to write your code yourself and we may can help you to fix bugs along the road...
Member 10746198 4-Nov-14 9:51am    
Can you at least give me a hint how to start?
I've been at it for 2h.
Kornfeld Eliyahu Peter 4-Nov-14 9:53am    
Start searching for 'C# calculator' in Google...

You will want to convert it to Reverse Polish Notation.
There are several articles about it here on CP, mine is: A Simple, Infix to Reverse Polish Notation Transformer, Written in C#[^]


Another possibility is to use: DataTable.Compute[^]
Try that first.
 
Share this answer
 
v2
Comments
BillWoodruff 4-Nov-14 11:37am    
+5 That is a very interesting article. Back in my PostScript days I could dream in RPN.
Dim result As Double
Dim plus As String()
Dim Substract As String()
Dim multiply As String()
Dim divide As String()
Dim powerto As String()
Dim num As Long

If TextBox1.Text.Contains("^") Then
powerto = TextBox1.Text.Split("^")
For Each NumToPower As String In powerto
num ^= CStr(NumToPower)
Next
End If


If TextBox1.Text.Contains("/") Then
divide = TextBox1.Text.Split("/")
For Each numbertodivide As String In divide
If numbertodivide <> 0 Then
num /= CStr(numbertodivide)
result = num
Else
MessageBox.Show("Cannot Divide By 0")

Exit Sub
End If
Next

End If

If TextBox1.Text.Contains("*") Then
multiply = TextBox1.Text.Split("*")
For Each NumToPower As String In multiply
num *= CStr(NumToPower)
Next
End If

If TextBox1.Text.Contains("+") Then
plus = TextBox1.Text.Split("+")
For Each NumToPower As String In plus
num += CStr(NumToPower)
Next
End If

If TextBox1.Text.Contains("-") Then
Substract = TextBox1.Text.Split("-")
For Each NumToPower As String In Substract
num -= CStr(NumToPower)
Next
End If


MessageBox.Show(num)


Now i am having a problem getting the numbers, for example if i have 25*2+3 written in the textbox. how i get only the 25 and only the 2 .
 
Share this answer
 
Comments
Member 10746198 5-Nov-14 5:01am    
Now i am having a problem getting the numbers, for example if i have 25*2+3 written in the textbox. how i get only the 25 and only the 2 .
We don't give code here on CodeProject. You would appreciate the fact that people don’t get paid here to be part of community. They are also having proper jobs just like you, but are enthusiastic enough to help others in a community.

And that’s what it makes CodeProject such a great community.

Anyway, if you are just interested in code there is always “www.RentACoder.com
 
Share this answer
 
Comments
PIEBALDconsult 4-Nov-14 9:59am    
"We don't give code here on CodeProject" Well, we do actually, but we don't write code on demand. _Especially_ not on demand.
BillWoodruff 4-Nov-14 11:44am    
In response to this an image comes to my mind of a colony of Aphids that Ants are "farming:" the Aphids emit their sugary exudate when they are stroked in the right way by the Ants.
PIEBALDconsult 4-Nov-14 11:49am    
Cue Billy Squire...

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