Calculating an Average
The spreadsheet calculates the average of a set of numbers; and then subtracts the average
from each number。 The example demonstrates reading a plete spreadsheet to get a number
and reading individual elements to perform a calculation。
…………………………………………………………Page 331……………………………………………………………
CH AP T E R 1 1 ■ L E A R N IN G AB O U T 。 N E T G E N E R I CS 309
Let’s use the following numbers to calculate the average。
Dim items As Double() = New Double() { 1。0; 2。0; 3。0 }
The average number is 2。0; and if you subtract the average from each number; you will get
the series –1; 0; and 1。
To make this work for a spreadsheet; the first step is to declare and then populate an
IWorksheet(Of BaseType) instance。 To instantiate an IWorksheet(Of BaseType) instance; you
use a factory that will instantiate the Worksheet(Of BaseType) class。 The code looks like this:
Dim sheetAverage As IWorksheet(Of Double) = _
SpreadsheetManager。CreateEmptyWorksheet(Of Double)(〃〃)
Dim items As Double() = New Double() {1; 2; 3}
sheetAverage。Dimension((items。Length + 10); 3)
For row = 0 To items。Length 1
sheetAverage。SetCellState(row; 0; items(row))
Next row
The worksheet is declared as being of type Double (IWorksheet(Of Double)); allowing you
to manage a Double value。 To populate sheetAverage; the numbers are iterated in a For loop
and assigned to the worksheet using the SetCellState() method。 The method Dimension() is
needed to create a fixed…length worksheet。
With the worksheet populated; to make sure everything looks right; you could call the method
ToString() and see if all is as expected。
The next step is to assign the lambda expressions that will be used to calculate the average
and then the individual differences from the average。 When you assign a calculation to the work
sheet; you need to know whether the lambda expression will be stateful or stateless。 Remember
that lambda expressions have some state。 It is just a question of whether you want a shared
state lambda expression or an individual…state lambda expression。 In the case of the lambda
expressions for the average calculations; a shared state is acceptable。
To calculate the average; you use a technique where the average calculation is the last element
in the series of the array calculations。 Thus; when the average calculation is called; it knows
how many elements there are because of the row in which the average calculation is stored。
Function Average(ByVal worksheet As IWorksheet(Of Double); _
ByVal cellRow As Integer; ByVal cellCol As Integer)
Dim runningTotal As Double = 0
Dim row As Integer
For row = 0 To cellRow 1
runningTotal = (runningTotal + worksheet。GetCellState(row; 0))
Next row
Return (runningTotal / CDbl(cellRow))
End Function
。 。 。
sheetAverage。AssignCellCalculation(items。Length; 0; AddressOf Average)
In the example; the average is calculated by using the variable cellRow as a maximum row。
Every cell (GetCellState()) before cellRow is added to a running total (runningTotal); and then;
finally; an average is returned by dividing runningTotal by cellRow。
…………………………………………………………Page 332……………………………………………………………
310 CH AP T E R 1 1 ■ L E A R N I N G A B OU T 。 N E T G E N E R I CS
With the average calculated; the next step is to calculate the difference between the average
and the individual items。 The result will be stored in a column to the right of the item cell state。
This is done by subtracting the average calculation cell state from the item value; as follows:
For row = 0 To items。Length 1
sheetAverage。AssignCellCalculation(row; 1; _
Function(worksheet As IWorksheet(Of Double); cellRow As Integer; _
cellCol As Integer)
(worksheet。GetCellState(cellRow; 0) _
worksheet。Calculate(items。Length; 0)))
Next row
The number of cell…state calculations depends on the count of numbers in items。 Each cell
calculation is assigned a locally declared lambda expression; meaning that the lambda expres
sions of all cell states will be identical and share the same state。 The only shared variable is
items。Length。 All the lambda expressions expect the same length; and so it is acceptable to
share this variable。 The average difference is calculated by calculating the average and then
subtracting it from the worksheet cell item value that is in the zeroth column。
Finally; when everything is assigned; you can call the worksheet。Calculate() method to
calculate the average and difference from the average。
sheetAverage。Calculate()
Console。WriteLine(sheetAverage。ToString())
Understanding Why the Calculation Worked
The cell calculations work because the spreadsheet has the ability to track what has been calculated
and what has not been calculated。 In a typical spreadsheet; you can change one cell in a sheet
and have everything magically recalculate。 There is no such feature for this spreadsheet。 However;
this simpler spreadsheet version can make sure that when there are dependencies; they are not
calculated multiple times。
Look back at the source code to calculate the difference between the average and a number。
The only reason the calculation worked is that the cell that contained the average was called
using the Calculate() method。 Had the GetCellState() method been used; the average might
not have been cal
小说推荐
- oracle从入门到精通(PDF格式)
- -Page 1-Oracle 从入门到精通-Page 2-资源来自网络,仅供学习 Oracle 从入门到精通一、SQL 8
- 最新章:第37章
- C语言游戏编程从入门到精通(PDF格式)
- -Page 1-Page 2-Page 3-Page 4-Page 5-Page 6-Page 7-Page 8-Page 9-Page 10-Page 11-Page 12-Page 13-Page 14
- 最新章:第4章
- Java编程思想第4版[中文版](PDF格式)
- -Page 1-Page 2《Thinking In Java》中文版作者:Bruce Eckel主页:http/BruceEckel.编译:Trans Bot主页:http/memberease~transbot致谢-献给那些直到现在仍在孜孜不倦创造下一代计算机语言的人们!指导您利用万维网的语言进
- 最新章:第295章
- 深入浅出MFC第2版(PDF格式)
- -Page 1-Page 2-山高月小山高月小 水落石出水落石出山高月小山高月小 水落石出水落石出-Page 3-深入淺出MFC(第版 使用Visual C 5.0 MFC 4.2)Dissecting MFC(Second Edition Using Visual C 5.0 MFC 4.2)侯俊
- 最新章:第309章
- VC语言6.0程序设计从入门到精通
- -Page 1-Visual C 6.0 程序设计从入门到精通求是科技 王正军 编著
- 最新章:第136章
- SQL 21日自学通(V3.0)(PDF格式)
- -Page 1-SQL 21 日自学通(V1.0 翻译人 笨猪目录目录 1译者的话 14第一周概貌 16从这里开始 16
- 最新章:第170章
- 2008年青年文摘精编版
- 作者:中国青年出版社“初恋”的惩罚.作者:凡 凡 文章来源《真情》2005年第4期 点击数:6608 更新时间:2005-6-5过了年,我就十八岁了。离高考只剩下四个多月了。这一段,班里的男女生相互间递纸条、写情书、约会等地下活动慢慢的多了起来。我这个“尖子生”也突然感到了不安、慌乱,并且自责。不知
- 最新章:第230章
- JMS简明教程(PDF格式)
- -Page 1-JMS1.1规范中文版卫建军2007‐11‐22-Page 2
- 最新章:第28章
- SQL语言艺术(PDF格式)
- -Page 1-SQLSSQQLL语言艺术内容介绍本书分为12章,每一章包含许多原则或准则,并通过举例的方式对原则进行解释说明。这些例子大多来自于实际案例,对九种SQL经典查询场景以及其性能影响讨论,非常便于实践,为你数据库应用维护人员阅读。资深 SQL 专家 Stéphane Faroult倾力打
- 最新章:第27章