How's it going everybody. Need some help writing a calculator in VB.NET


Right now i have it written so that when a user clicks on a number (ie the number one) it writes it to a text box. no problem.

but what i need to do is be able to store that information into an array that i can iterate through so that i can do the math on the number.


so if a user enters "5" "+" "6" "x" "7" "="

it will store the information into an array as

myarray(5, "plus")
myarray(6, "times")
myarray(7, "equals")

'where the symbol following the number will create the key for 'the number

now i need to iterate it through it like this


if the key = "times" then do somthing like

do current arrayval * next arrayval

store the answer into array as

myarray(45, equals) 'i do this because the key for the 7 was equals, since the times was used in the previous step

now it needs to do this

if current ArrayValKey = "plus"

do

current arrayval + nextarrayval

store the answer to array as

myarray(50, "equals")

then do

if ArrayValKey = equals

then ArrayValKey = answerbox.Text

does anyone have any insight or examples that i may look at??

Recommended Answers

All 9 Replies

Thats making things difficult. Normally a calulator works it out on the fly. When you press 5 then + then 4 then the next key you press the display would show 9 while it waited for the next number. Basically there are two groups... a number and an operator and of course the running result.

Number keys can be mulitple to handle large numbers... eg. 5 6 6 4 but your cue that the number has finished is the operator key being hit (+ - / * = ). When you start the calculator you wait on a number key(s) and ignore an operator key. Once a number key has been pressed you display it and store it. Keep adding to it while more number keys are pressed until an operator key is pressed. That number is the running result. If an operator key is pressed again it overrides the previous one until a number key is pressed. Store the operator key and the new number key and keep adding number keys until a new operator key is pressed. When a new operator key is pressed you need to take your running result perform the stored operation on it with the new temp number and store (and display) the new running result and the new operator key and keep going. If the operator key is the = key then the next number hit clears the running result.

It sounds complicated on here but is very simple and no iterating through arrays. All done as running results and button click events.

IF i have lost you through not explaining clearly let me know... not so easy in text

i want a smart calculator though


i want my user to be able to enter

5+5x4^3 and have it evaluate it correctly

4^3 then do 5 times that answer then add 5.


i think you might have lost me somewhere in the explination.

ok that is complex. you will have to first break the string into an array but different to your example. if they entered 55+5*4^3 you would need to have an array such as...
array(0) = 55
array(1) = +
array(2) = 5
array(3) = *
array(4) = 4
array(5) = ^
array(6) = 3

Then you will have to keep traversing the array looking for the math rules in prioriy order probably in a reverse traverse (ie array 6 to array 0). in the order ^ * / - + etc.
so starting with ^ you would traverse the array until you found the first instance. This would tell you it was at index 5. You would then have to take the numbers at index 4 and 6 and perform the math on them. Then you would have to delete the values at 4, 5, and 6 and then insert the new result at index 4.

Then keep going until all the ^ have been completed. Then repeat for the next operator (*) and keep going. You should eventually have an array with just 1 item in it which will be your result.

Hope im clearer this time :) Though it is a totally different explanation than last time too :)

just to make it clearer using my other as an example (as it had double digits in it) 55+5*4^3 would go into an array as we said before:
array(0) = 55
array(1) = +
array(2) = 5
array(3) = *
array(4) = 4
array(5) = ^
array(6) = 3

Then traverse looking for ^. it says it is at index 5. So take the number at index 4 (=4) and the number at index 6 (=3) and perform the math (4^3) which gives you a result of 64. Delete indexes 4, 5, 6 so your array looks like this
array(0) = 55
array(1) = +
array(2) = 5
array(3) = *

and add the result (64) to index 4 (just happens to be on the end in this example). so your array now looks like this
array(0) = 55
array(1) = +
array(2) = 5
array(3) = *
array(4) = 64
Keep looking for the ^ in the rest of the array... there are none so move to the next math operator in the priority which is *. Traverse the array and look for it.. first instance is index 3. so take the numbers at index 2 and index 4 and perform the math (5*64 = 320). Delete the indexes at 2, 3 and 4 and insert the new result at index 2 so your array now looks like
array(0) = 55
array(1) = +
array(2) = 320

Keep looking for *. None. Now look for /. None. Now look for -. None. Now look for +. Found one at 1. take value at index 0 and index 2 and perform the operation (55+320=375). Delete indexes 0, 1 and 2 and replace with the result at index 0. Your array looks like this
array(0) = 375
As you have only one index now it must be your result.

Make more sense?

ok that is complex. you will have to first break the string into an array but different to your example. if they entered 55+5*4^3 you would need to have an array such as...
array(0) = 55
array(1) = +
array(2) = 5
array(3) = *
array(4) = 4
array(5) = ^
array(6) = 3

Then you will have to keep traversing the array looking for the math rules in prioriy order probably in a reverse traverse (ie array 6 to array 0). in the order ^ * / - + etc.
so starting with ^ you would traverse the array until you found the first instance. This would tell you it was at index 5. You would then have to take the numbers at index 4 and 6 and perform the math on them. Then you would have to delete the values at 4, 5, and 6 and then insert the new result at index 4.

Then keep going until all the ^ have been completed. Then repeat for the next operator (*) and keep going. You should eventually have an array with just 1 item in it which will be your result.

Hope im clearer this time :) Though it is a totally different explanation than last time too :)

did an awesome job and was exactly what i was thinking of doing.


now just to figure out how to implement that yup yup

You using 2005 or 2003? Make use of the generic list in 2005 if you have it.
You cant do a split on your string it is too complex. But is easy enough to go through one character at a time and build up a string of numbers until you hit an operator. Add the number to the list and then the operator to the next index. then keep going until you finish the string (doing some error checking on the way of course).

Then comes the fun bit. But it is really traversing 2 arrays. One of math operators in priority order and the other your new array (i would go in reverse order on this one). Then it is a matter of finding a match and literally grabbing the two values either side, do the math and delete them and insertat the new value (remember to do the insert at otherwise it will be in the wrong place and it has to be one less than the index you found the operator.

something like (please excuse my code im a c# person and vb is rusty)

Dim result as string
for i = 0 to mathoperatorsarray.count -1

for j = calculationarray.count - 1 to 0 step -1
  If calcualationarray(j).value = mathoperatorsarray(i).value then
    result = somemathfunctionwhichyoucancall(calcualationarray(j-1),calcualationarray(j),calcualationarray(j+1))
'this will take the 2 numbers the operator, work out which operator it is and do the math and give you the result back
calcualationarray(j+1).remove
calcualationarray(j).remove
calcualationarray(j-1).value = result
'i would now check to see if i have only one item in the array now so dont need to process anymore and can exit the two loops
  end if
end

end
'should have one item in the array now which is your result

its rusty but should give you an idea. you may have to get the two counts before using in the for loop as you are manipulating the array and so messing the enumerator up but it is no biggie :)

im working on my code and now i need to know how to remove an array element, i am having such a hard time working through and finding this information.


so far i have no problem doing the math on the array. but now when i go to remove the element

math(count).Remove i get an error, it is looking for more arguments after the remove statement and actually tries to append () to the end of remove. i just want it to remove the value at the current count so that when i have it itterate through the array i dont have empty array strings.


any help would be greatly appreciated.

array.remove wants the object to remove... so you would say math.remove(math(count)) which is a bit messy.

use the removeat (i think it is in vb too) which wants an index and would be math.removeat(count) which is cleaner.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.