i've semi created a calulator that accepts more than one operator at a time by storing each pressed no. in an array .....yet wot i need is for this array to be accessed and each operator found needs to be used as an operator (as oppose to string values)
Can anyone HELP
Here is the project so far:
Last edited by Wah; Oct 15th, 2002 at 12:37 PM.
Up the U's, Up the U's. Up The Good Old White And Blues
When We Get Together What a Game We'll See We're As Strong As The Old The Old Oak Tree
All For One And One For All We're Football Good And Clean
BULLSH*T
You Can Hear The Crowd Sing "Oi We've Got A Team"
Up the U's, Up the U's. Up The Uoooos's
The best bet for this is to use Reverse Polish Notation
That way when the calulations get more complex
you won't have to worry about precedence.
A quick search in planet source code
should throw up a few examples
thanks this really helps....... yet i need to knw how to search the array for operators to apply this
Up the U's, Up the U's. Up The Good Old White And Blues
When We Get Together What a Game We'll See We're As Strong As The Old The Old Oak Tree
All For One And One For All We're Football Good And Clean
BULLSH*T
You Can Hear The Crowd Sing "Oi We've Got A Team"
Up the U's, Up the U's. Up The Uoooos's
I've attached some code which should give you
everything you need.
Type in the Algebraic expression i.e. 3+4/2
Then convert it.
Then get the answer
Make sure you convert the equation
before trying to get an answer other wise it will just
fall over
I have been doing something like that in a recent program that takes what amounts to written equations and computes them. I first tried it with strings, but that was slow and somewhat painful. I then tried it using objects. This turned out to be much cleaner, and somewhat faster.
The technique I used was to put both numbers and operators into the same type of object. One field held value, another held an identifier, etc. This leaves the entire equation represented by an array of objects.
I tranlsated this into a number by parsing through the objects, and putting the values into a temporary array based on operator precedence. The idea is that if a number is separated from a second number by either * or /, then do the calculation. If separated by +, then put the first number into the temporary array. If separated by -, then put the negative of the first number into the temporary array. Therefore, 5+6*3/9-3 would have 5,2,-3 in the temporary array. Sum the values in the temporary array to get the result. Parenthesis are handled using a recursive call to the translation function.
Reverse polish notation might improve on this, though it can be highly non-intuitive for for some people.
i don't see how this can help me with my project!!!!!!
Up the U's, Up the U's. Up The Good Old White And Blues
When We Get Together What a Game We'll See We're As Strong As The Old The Old Oak Tree
All For One And One For All We're Football Good And Clean
BULLSH*T
You Can Hear The Crowd Sing "Oi We've Got A Team"
Up the U's, Up the U's. Up The Uoooos's
Your original post mentioned putting all of the numbers, and I assumed operators, in an array. I thought you were trying to figure out how to convert the information in the array into a result. This would require converting the operator from some string representation into an action. You would then have to deal with operator precedence.
What I was talking about was a way to represent numbers and operators in an array such that you could parse through the array in a fashion that allowed you to obtain a result with the rules of operator precedence intact.
If that's not the problem, then I misunderstood. However, I would point out that using strings will be slow. Even if it requires keeping the data in two different formats (a readable form like y=mx+b, and an object form), it may be faster.
u got the idea the first time ...yet the problem is coding this so that it works alongside my project
Up the U's, Up the U's. Up The Good Old White And Blues
When We Get Together What a Game We'll See We're As Strong As The Old The Old Oak Tree
All For One And One For All We're Football Good And Clean
BULLSH*T
You Can Hear The Crowd Sing "Oi We've Got A Team"
Up the U's, Up the U's. Up The Uoooos's
I guess I'm still not understanding something. It appears that you would only need to evaluate the equation at one given time. Most calculators will only evaluate when the user presses the = button.
I am envisioning a bunch of buttons. As each button is pressed, it is evaluated to see if it is an operator, or a number. In either case, it gets added to the array. Then when the user presses the = button, the array is evaluated.
The evaluation is based on the fact that the array will hold a number, followed by an operator, followed by another number. If you use objects to hold the numbers and operators, then one of the fields in the object could be Type. An operator just tells us how to combine number1 with number2. If the different operators are different types, then you can figure out how to combine number1 with number2 using a select case statement on the type.
Up the U's, Up the U's. Up The Good Old White And Blues
When We Get Together What a Game We'll See We're As Strong As The Old The Old Oak Tree
All For One And One For All We're Football Good And Clean
BULLSH*T
You Can Hear The Crowd Sing "Oi We've Got A Team"
Up the U's, Up the U's. Up The Uoooos's
Ok, I looked at the download, though I had to use Notepad, which sucks.
The code, as it currently stands, looks like it will build an equation character by character, then write the equation into a textbox when the user presses the EXE button.
Would I be correct in assuming that what you want to do is write the evaluated equation?
If that is the case, I would suggest that the first step is to go through the array turning the characters for the numbers into actual numbers.
Code:
dim x as long
dim st1 as string
dim d as double
dim array2() as double
dim array3() as string
dim n as long
for x=1 to ubound(iarray)
if iarray(x)='some expression which includes only numeric and '.'.
st1=st1 & iarray(x)
else
n=n+1
redim preserve array2(n)
array2(n)=val(st1)
st1=""
redim preserve array3(n)
array3(n)=iarray(x)
end if
(first time using code formatting, hope it is readable)
The idea is to turn the array of characters into two arrays. The first array holds numbers, while the second array holds operators.
This is where the earlier posts come in. If you could put numbers or operators into objects, you could have one array.
much closer .....now the numerics are sorted.. how do i work out the results from these?????
Up the U's, Up the U's. Up The Good Old White And Blues
When We Get Together What a Game We'll See We're As Strong As The Old The Old Oak Tree
All For One And One For All We're Football Good And Clean
BULLSH*T
You Can Hear The Crowd Sing "Oi We've Got A Team"
Up the U's, Up the U's. Up The Uoooos's
With the two arrays, note that array2(1) is the first number, and array3(1) is the first operator. Each number in array2 is separated by the operator in array3. Therefore, by reading array2(1), array3(1), array2(2), array3(2)...etc. You are reading the equation.
Now make array4() as double.
Ok, I'll try another formatting.
[Visual Basic Code]
dim array4() as double
dim ubnd as long
dim x as long
dim n as long
'Array2() will have one more value than array3(), but we're
'walking the operator array.
d=array2(1)
for x=1 to ubnd
if array3(x)="+" then
n=n+1
redim preserve array4(n)
array4(n)=d
d=array2(x+1)
elseif array3(x)="-" then
n=n+1
redim preserve array4(n)
array4(n)=d
d=-array2(x+1)
elseif array3(x)="*" then
d=d*array2(x+1)
elseif array3(x)="/" then
d=d/array2(x+1)
end if
next x
'Something will remain in d, this must be added to array4()
n=n+1
redim preserve array4(n)
array4(n)=d
This should work fairly well, though I may have a typo in there, it was written on the fly.
Basically, if a high level operator (* or /) is encountered, it is evaluated on the fly. d holds the partial equation. Whenever a + or a - is encountered, d is set aside into array4(), and a new value is put into d. Notice that the negative of the value is used if the operator is -. At the end of this loop, array4() holds some number of values (equal to the number of + and -, but that is irrelevant) with their signs correct. Add all the values in array4(), and you have the result of the equation.
Last edited by Shaggy Hiker; Oct 15th, 2002 at 03:35 PM.
Originally posted by Shaggy Hiker Ok, how do you indent?
You should indent previously.
Normally I type code in VBasic and then I paste it. If you are using notepad then you should indent it in the notepad previously to paste it