Objects Performing Maths Functions
-
I have a table that I require calculations to be performed against. To achieve this, I have written several methods for the required functions. I have no problem with this solution although it does appear limited. As an example, I have created a form which is passed the DataTable. The form allows the user to choose the required function EG Divide, Mltiple, Add or Subtract and then asks what value/column the function should be performed against, what factor value/column should be used in the calculation and finally, asks where the result should be stored. Now if the user wants to Divide the value in columnA by the value in columnB and store the result somewhere, it all works fine. But imagine if the user wants to perform a further calculation on the result, at present the result has to be stored somewhere else and a second pass performed using the desired function. The only way I have though about solving this problem is to create some kind of math object which can be lnked in a daisy-chain or stored in an array so that multiple calculations can be performed therfore creating an equation. Has anyone had any experience of doing this or similar? Any ideas on removing the restrictions of the curent design? Regards Wayne Phipps ____________ Time is the greatest teacher... unfortunately, it kills all of its students LearnVisualStudio.Net
-
I have a table that I require calculations to be performed against. To achieve this, I have written several methods for the required functions. I have no problem with this solution although it does appear limited. As an example, I have created a form which is passed the DataTable. The form allows the user to choose the required function EG Divide, Mltiple, Add or Subtract and then asks what value/column the function should be performed against, what factor value/column should be used in the calculation and finally, asks where the result should be stored. Now if the user wants to Divide the value in columnA by the value in columnB and store the result somewhere, it all works fine. But imagine if the user wants to perform a further calculation on the result, at present the result has to be stored somewhere else and a second pass performed using the desired function. The only way I have though about solving this problem is to create some kind of math object which can be lnked in a daisy-chain or stored in an array so that multiple calculations can be performed therfore creating an equation. Has anyone had any experience of doing this or similar? Any ideas on removing the restrictions of the curent design? Regards Wayne Phipps ____________ Time is the greatest teacher... unfortunately, it kills all of its students LearnVisualStudio.Net
Using a
DataTable
is rather inefficient with lots of overhead you don't need for a simple state machine and what could be done using simple state variables. Store your result in a variable just like you do for your controls you dropped onto the form in the designer. Read the operands from the form, calculate, and store the result in the variable. If you want to remember all the results use a growable list likeArrayList
orStack
. A stack is what your more advanced calculators use to store previous results, and stacks are what are typically used by calculators to evaluate expressions. You can find lots of information on the web about evaluating expressions to perform calculations. Even if you want to stick with using aDataTable
add a column for metadata about the operands and/or operators you have stored in theDataRow
s. You could, for example, push the result of a calculation into a newDataRow
with information that states whether the data is an operand, operator, or result. This posting is provided "AS IS" with no warranties, and confers no rights. Software Design Engineer Developer Division Sustained Engineering Microsoft [My Articles] [My Blog]