calculator program

juggernot

Well-known member
Joined
Sep 28, 2006
Messages
173
Programming Experience
Beginner
I am making a calculator program. Most of the buttons simply add a character to a textbox at the top of the screen. When the equals button is pressed i need to take that string and make it a number. The string could be something like : "134/(5)^2". Is there an easy way to do this? Or do I have to code an entire string minipulation/order of operations thing?
 
I would have it make calculations along the way instead of adding it all to one string. It can make that calculation just fine but it will apply order of operations as it sees fit, most likely now how the user preferred.
 
You cannot include math operators inside a string. You need to convert each numeric string to a number one by one and execute the math processing in a separate expression.

You need to completely revise your project.
 
I don't think it needs complete revision. I didn't think it would work the simple way, so I was already planning on doing it the hard way. I just need to do some string manipulation, convert some strings to integers and do the math according to the order of operations. It will be tough, but not impossible. Essentially, all my difficult code will be in the equals button. I somehow need to loop through my string looking for + - / * () ^ in order of operation, then change the string(s) inside the brackets or on either side of the operators to an integer, and carry out the math until I'm done.
 
i once had an idea like this

actually my idea was to make a vb.net graphing calculator that had all the capabilities of the TI-89 graphing calculator

it didnt work, i couldnt come up with an algorithm to parse text in this fashion. i wouldnt mind trying to do something like this sometime though

if i get any ideas, i'll shoot em your way
 
Can't you add all the operations you want into a list or something, then execute them in the order of the list?

I can see complications arising with that method, but still I think it would work.
 
It won't be that simple. I need to create a logarithm which starte by looking for brackets. If an opening bracket is found, it checks to see whether a closing bracket or an opening bracket comes next. This way it finds the innermost bracket, and can work its way out. It then checks for mathematical operators. This is the tricky part. It needs to find all the operators within the bracket, split the string up until all the operators and integers are seperate, and execute the math according to order of operation.
 
I believe I have come up with an algorithm which would work. Currently It's only on paper, and will not have time to code this program for a few days. The algorithm finds the innermost bracket and works its way out. In each bracket it evaluates and replaces the strings in order of operation. For instance, take the question 3(4(2/4^2*5)). The algorithm would find the innermost bracket, and then it would search for the "^". Once found, it would search on either side of it for the "^" for the closest operator. It would then take the integerst between those operators and put them into an equation. Essentially, it will find the 4 and the 2 on either side of the "^", and then carry out the equation. Then we simply replace the "4^2" with the answer, "16". Now it would do the same thing for Division. It will find the 2 and the 16 on each side of the "/", and evaluate it. It would then replace it and go onto the next operator. In this way we will eventually arrive at a single integer. Its actually quite simple, but took me some time to wrap my head around.
 
Since your original post asked if there was an easy way to do this I'll share the idea of an article I read once that I can't find now.

What you're trying to do is called equation evaluation. J# has this built in so you could create a dll in J# (or download one off the internets) and call it from VB.NET.
Another easy way to do this would be to search for equation evaluation for VB.NET; it's been done many times. But that would take all the fun out of it.
 
Yes, using decimal our double woult be the safe bet...But the answer could be really long as well. I need something that can include a decimal, but is as big as a long? Is there a data type like this?

At this point, if there is an easy way I've decided not to do it. I'd rather do it the hard way. That way I can learn the logic behind it.
 
Decimal and Double's hold decimal points and I don't know of a variable type that can hold a value as big as a long and still have a decimal
 
Here is an expression evaluator I wrote some time ago and transported to vb net. If you have any problems let me know. I hope this helps.
 

Attachments

  • ExpressionEvaluation.zip
    12 KB · Views: 17
Last edited:
Back
Top