Skip to content

Commit 353dea2

Browse files
committed
1.2.18 Finished
1 parent 2270782 commit 353dea2

File tree

6 files changed

+184
-0
lines changed

6 files changed

+184
-0
lines changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
4+
<PropertyGroup>
5+
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
6+
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
7+
<ProjectGuid>{3BBA4BF1-2037-4DC2-8CC5-11F5B7165C8D}</ProjectGuid>
8+
<OutputType>Exe</OutputType>
9+
<RootNamespace>_1._2._18</RootNamespace>
10+
<AssemblyName>1.2.18</AssemblyName>
11+
<TargetFrameworkVersion>v4.7</TargetFrameworkVersion>
12+
<FileAlignment>512</FileAlignment>
13+
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
14+
</PropertyGroup>
15+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
16+
<PlatformTarget>AnyCPU</PlatformTarget>
17+
<DebugSymbols>true</DebugSymbols>
18+
<DebugType>full</DebugType>
19+
<Optimize>false</Optimize>
20+
<OutputPath>bin\Debug\</OutputPath>
21+
<DefineConstants>DEBUG;TRACE</DefineConstants>
22+
<ErrorReport>prompt</ErrorReport>
23+
<WarningLevel>4</WarningLevel>
24+
</PropertyGroup>
25+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
26+
<PlatformTarget>AnyCPU</PlatformTarget>
27+
<DebugType>pdbonly</DebugType>
28+
<Optimize>true</Optimize>
29+
<OutputPath>bin\Release\</OutputPath>
30+
<DefineConstants>TRACE</DefineConstants>
31+
<ErrorReport>prompt</ErrorReport>
32+
<WarningLevel>4</WarningLevel>
33+
</PropertyGroup>
34+
<ItemGroup>
35+
<Reference Include="System" />
36+
<Reference Include="System.Core" />
37+
<Reference Include="System.Xml.Linq" />
38+
<Reference Include="System.Data.DataSetExtensions" />
39+
<Reference Include="Microsoft.CSharp" />
40+
<Reference Include="System.Data" />
41+
<Reference Include="System.Net.Http" />
42+
<Reference Include="System.Xml" />
43+
</ItemGroup>
44+
<ItemGroup>
45+
<Compile Include="Accumulator.cs" />
46+
<Compile Include="Program.cs" />
47+
<Compile Include="Properties\AssemblyInfo.cs" />
48+
</ItemGroup>
49+
<ItemGroup>
50+
<None Include="App.config" />
51+
</ItemGroup>
52+
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
53+
</Project>
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace _1._2._18
8+
{
9+
public class Accumulator
10+
{
11+
private double m;
12+
private double s;
13+
private int N;
14+
15+
public void AddDataValue(double x)
16+
{
17+
N++;
18+
s = s + 1.0 * (N - 1) / N * (x - m) * (x - m);
19+
m = m + (x - m) / N;
20+
}
21+
public double Mean()
22+
{
23+
return m;
24+
}
25+
public double Var()
26+
{
27+
return s / (N - 1);
28+
}
29+
public double Stddev()
30+
{
31+
return Math.Sqrt(this.Var());
32+
}
33+
public override string ToString()
34+
{
35+
return "Mean (" + N + " values): " + string.Format("{0, 7:F5}", Mean());
36+
}
37+
}
38+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<configuration>
3+
<startup>
4+
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7" />
5+
</startup>
6+
</configuration>
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace _1._2._18
8+
{
9+
/*
10+
* 1.2.18
11+
*
12+
* 累加器的方法。
13+
* 以下代码为 Accumulator 类添加了 var() 和 stddev() 方法,
14+
* 它们计算了 addDataValue() 方法的参数的方差和标准差,验证这段代码:
15+
* 与直接对所有数据的平方求和的方法相比较,
16+
* 这种实现能够更好的避免四舍五入产生的误差。
17+
*
18+
*/
19+
class Program
20+
{
21+
//当数据比较大时—— 例如 10^9 加上随机小数组成的数列,这时 double 的小数精度将受限。
22+
//求和之后整数部分更大,小数部分将自动四舍五入,出现误差
23+
//这时再计算平均值时将会带来较大的误差。
24+
//因此采用另一个递推公式:
25+
//k 为下标。
26+
//Mk = Mk-1+ (xk – Mk-1)/k
27+
//Sk = Sk-1 + (xk – Mk-1)*(xk – Mk).
28+
//方差 s^2 = Sk/(k – 1).
29+
//这种情况下并没有直接对所有输入值求和,小数精度不受到整数部分长度的影响。
30+
static void Main(string[] args)
31+
{
32+
int T = 100000;
33+
Random random = new Random();
34+
Accumulator a = new Accumulator();
35+
for (int t = 0; t < T; ++t)
36+
{
37+
a.AddDataValue(random.NextDouble() + 1000000000);
38+
}
39+
40+
Console.WriteLine(a.Stddev());
41+
Console.WriteLine(a);
42+
}
43+
}
44+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using System.Reflection;
2+
using System.Runtime.CompilerServices;
3+
using System.Runtime.InteropServices;
4+
5+
// 有关程序集的一般信息由以下
6+
// 控制。更改这些特性值可修改
7+
// 与程序集关联的信息。
8+
[assembly: AssemblyTitle("1.2.18")]
9+
[assembly: AssemblyDescription("")]
10+
[assembly: AssemblyConfiguration("")]
11+
[assembly: AssemblyCompany("")]
12+
[assembly: AssemblyProduct("1.2.18")]
13+
[assembly: AssemblyCopyright("Copyright © 2017")]
14+
[assembly: AssemblyTrademark("")]
15+
[assembly: AssemblyCulture("")]
16+
17+
// 将 ComVisible 设置为 false 会使此程序集中的类型
18+
//对 COM 组件不可见。如果需要从 COM 访问此程序集中的类型
19+
//请将此类型的 ComVisible 特性设置为 true。
20+
[assembly: ComVisible(false)]
21+
22+
// 如果此项目向 COM 公开,则下列 GUID 用于类型库的 ID
23+
[assembly: Guid("3bba4bf1-2037-4dc2-8cc5-11f5b7165c8d")]
24+
25+
// 程序集的版本信息由下列四个值组成:
26+
//
27+
// 主版本
28+
// 次版本
29+
// 生成号
30+
// 修订号
31+
//
32+
// 可以指定所有值,也可以使用以下所示的 "*" 预置版本号和修订号
33+
// 方法是按如下所示使用“*”: :
34+
// [assembly: AssemblyVersion("1.0.*")]
35+
[assembly: AssemblyVersion("1.0.0.0")]
36+
[assembly: AssemblyFileVersion("1.0.0.0")]

Algorithms 4th Edition.sln

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "0-类库", "0-类库", "{2A
143143
EndProject
144144
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "1.2.17", "1 Fundamental\1.2\1.2.17\1.2.17.csproj", "{45B8DC5B-4439-42DF-AD86-40548C5CD01D}"
145145
EndProject
146+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "1.2.18", "1 Fundamental\1.2\1.2.18\1.2.18.csproj", "{3BBA4BF1-2037-4DC2-8CC5-11F5B7165C8D}"
147+
EndProject
146148
Global
147149
GlobalSection(SolutionConfigurationPlatforms) = preSolution
148150
Debug|Any CPU = Debug|Any CPU
@@ -381,6 +383,10 @@ Global
381383
{45B8DC5B-4439-42DF-AD86-40548C5CD01D}.Debug|Any CPU.Build.0 = Debug|Any CPU
382384
{45B8DC5B-4439-42DF-AD86-40548C5CD01D}.Release|Any CPU.ActiveCfg = Release|Any CPU
383385
{45B8DC5B-4439-42DF-AD86-40548C5CD01D}.Release|Any CPU.Build.0 = Release|Any CPU
386+
{3BBA4BF1-2037-4DC2-8CC5-11F5B7165C8D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
387+
{3BBA4BF1-2037-4DC2-8CC5-11F5B7165C8D}.Debug|Any CPU.Build.0 = Debug|Any CPU
388+
{3BBA4BF1-2037-4DC2-8CC5-11F5B7165C8D}.Release|Any CPU.ActiveCfg = Release|Any CPU
389+
{3BBA4BF1-2037-4DC2-8CC5-11F5B7165C8D}.Release|Any CPU.Build.0 = Release|Any CPU
384390
EndGlobalSection
385391
GlobalSection(SolutionProperties) = preSolution
386392
HideSolutionNode = FALSE
@@ -452,5 +458,6 @@ Global
452458
{5D194E09-4D75-4F8F-8296-B923CCEC6C0E} = {1EF9D9DA-B4CF-4467-A5F4-CDBACEAA34CD}
453459
{2AF0C58F-9CCB-457D-97C5-C73DB45F31BD} = {9D948552-DF2E-412F-AEA6-B59F8BB708C5}
454460
{45B8DC5B-4439-42DF-AD86-40548C5CD01D} = {1EF9D9DA-B4CF-4467-A5F4-CDBACEAA34CD}
461+
{3BBA4BF1-2037-4DC2-8CC5-11F5B7165C8D} = {1EF9D9DA-B4CF-4467-A5F4-CDBACEAA34CD}
455462
EndGlobalSection
456463
EndGlobal

0 commit comments

Comments
 (0)