c#入门经典入门经典-调试函数的运用
见代码:
1
using System;
2
using System.Collections.Generic;
3
using System.Text;
4
using System.Diagnostics;
5
//这里用到两个函数Debug.WriteLine()和Trace.WriteLine()
6
//Debug.WriteLine()和Trace.WriteLine类似于Console.WriteLine
7
//区别是把信息输出到output窗口
8
//Debug.WriteLine()仅在调试时输入,生成可发布版本时自动消失
9
//在调试的时候,如果设置为debug在可以看到output中看到完整的信息
10
//调试时如果设置为release模式,即发布模式,则看到output窗口中的完整信息
11
//
12
13
namespace Ch07Ex01
14
{
15
class Program
16
{
17
static void Main(string[] args)
18
{
19
int[] testArray = { 4, 7, 4, 2, 7, 3, 7, 8, 3, 9, 1, 9 };
20
//存储下标数组,为什么是数组?因为有可能有多个相同最大值,其下标是不同的,
21
//因此就用一个数组存储.
22
int[] maxValIndices;
23
24
int maxVal = Maxima(testArray, out maxValIndices);
25
Console.WriteLine("Maximum value {0} found at element indices:", maxVal);
26
27
foreach (int index in maxValIndices)
28
{
29
//输出maxValIndices对象中的元素,其中存储的数组下标,本例中从0开始到最大值9分别存在9和11位
30
Console.WriteLine(index);
31
}
32
Console.ReadKey();
33
}
34
35
/// <summary>
36
/// 找出给定数组中最大值
37
/// </summary>
38
/// <param name="integers">integers存储传来的数组</param>
39
/// <param name="indices">indices数组存储数组下标,定于为out,为引用调用,有本函数初始化数组</param>
40
/// <returns>传入数组中的最大值</returns>
41
static int Maxima(int[] integers, out int[] indices)
42
{
43
//初始化,处理第一个元素
44
Debug.WriteLine("Maximum value search started.");
45
indices = new int[1];
46
int maxVal = integers[0];
47
indices[0] = 0;
48
int count = 1;
49
//Debug.Assert(count > 1, "my test", "contact me");
50
//在此我自己的添加的代码,可以忽略。
51
Debug.WriteLine("Maximum value initiallized to " + maxVal + ", at element index 0.");
52
53
for (int i = 1; i < integers.Length; i++)
54
{
55
Debug.WriteLine("Now looking at element at index " + i + ".");
56
//当数组中只有一个最大值的情况
57
if (integers[i] > maxVal)
58
{
59
maxVal = integers[i];
60
count = 1;
61
indices = new int[1];
62
indices[0] = i;
63
Debug.WriteLine("New maxmum found. New value is " + maxVal + ", at element index " + i + ".");
64
}
65
else
66
{
67
//最大值有两个时的情况
68
if (maxVal == integers[i])
69
{
70
count++;
71
/*数组发生变化,不能用new int[1],此时要存储多个下标,因此数组长度将发生变化,
72
* new int[1]不在适用,因此要扩大数组大小,但是数组长度是不能动态增长的,处理办法如下 :
73
* 1.把原来下标数组中的值存储到一个备份数组中
74
* 2.重新构造下标数组的维数,维数大小代表重复最大值的个数,以这个值为依据进行构造
75
* 其中用到了数组拷贝函数CopyTo() 用法为:老数组.CopyTo(新数组,拷贝到新数组的哪个下标开始的位置)。
76
*/
77
78
//数组备份
79
int[] oldIndices = indices;
80
//根据新的count值构造数组的长度
81
indices = new int[count];
82
//数组拷贝
83
oldIndices.CopyTo(indices, 0);
84
//新的下标加入数组
85
indices[count - 1] = i;
86
Debug.WriteLine("Duplication maximum found at element index " + i + ".");
87
}
88
}
89
}
90
91
Trace.WriteLine("Maximum value " + maxVal + " found, with " + count + " occurrence.");
92
Debug.WriteLine("Maximum value search completed.");
93
//返回最大值,由于下标数组是out的,所以不用返回
94
return maxVal;
95
}
96
}
97
}
98
using System;2
using System.Collections.Generic;3
using System.Text;4
using System.Diagnostics;5
//这里用到两个函数Debug.WriteLine()和Trace.WriteLine()6
//Debug.WriteLine()和Trace.WriteLine类似于Console.WriteLine7
//区别是把信息输出到output窗口8
//Debug.WriteLine()仅在调试时输入,生成可发布版本时自动消失9
//在调试的时候,如果设置为debug在可以看到output中看到完整的信息10
//调试时如果设置为release模式,即发布模式,则看到output窗口中的完整信息11
//12

13
namespace Ch07Ex0114
{15
class Program16
{17
static void Main(string[] args)18
{19
int[] testArray = { 4, 7, 4, 2, 7, 3, 7, 8, 3, 9, 1, 9 };20
//存储下标数组,为什么是数组?因为有可能有多个相同最大值,其下标是不同的,21
//因此就用一个数组存储.22
int[] maxValIndices;23
24
int maxVal = Maxima(testArray, out maxValIndices);25
Console.WriteLine("Maximum value {0} found at element indices:", maxVal);26

27
foreach (int index in maxValIndices)28
{29
//输出maxValIndices对象中的元素,其中存储的数组下标,本例中从0开始到最大值9分别存在9和11位30
Console.WriteLine(index);31
}32
Console.ReadKey();33
}34

35
/// <summary>36
/// 找出给定数组中最大值37
/// </summary>38
/// <param name="integers">integers存储传来的数组</param>39
/// <param name="indices">indices数组存储数组下标,定于为out,为引用调用,有本函数初始化数组</param>40
/// <returns>传入数组中的最大值</returns>41
static int Maxima(int[] integers, out int[] indices)42
{43
//初始化,处理第一个元素44
Debug.WriteLine("Maximum value search started.");45
indices = new int[1];46
int maxVal = integers[0];47
indices[0] = 0;48
int count = 1;49
//Debug.Assert(count > 1, "my test", "contact me");50
//在此我自己的添加的代码,可以忽略。51
Debug.WriteLine("Maximum value initiallized to " + maxVal + ", at element index 0.");52
53
for (int i = 1; i < integers.Length; i++)54
{55
Debug.WriteLine("Now looking at element at index " + i + ".");56
//当数组中只有一个最大值的情况57
if (integers[i] > maxVal)58
{59
maxVal = integers[i];60
count = 1;61
indices = new int[1];62
indices[0] = i;63
Debug.WriteLine("New maxmum found. New value is " + maxVal + ", at element index " + i + ".");64
}65
else66
{67
//最大值有两个时的情况68
if (maxVal == integers[i])69
{70
count++;71
/*数组发生变化,不能用new int[1],此时要存储多个下标,因此数组长度将发生变化,72
* new int[1]不在适用,因此要扩大数组大小,但是数组长度是不能动态增长的,处理办法如下 :73
* 1.把原来下标数组中的值存储到一个备份数组中74
* 2.重新构造下标数组的维数,维数大小代表重复最大值的个数,以这个值为依据进行构造75
* 其中用到了数组拷贝函数CopyTo() 用法为:老数组.CopyTo(新数组,拷贝到新数组的哪个下标开始的位置)。76
*/77
78
//数组备份79
int[] oldIndices = indices;80
//根据新的count值构造数组的长度81
indices = new int[count];82
//数组拷贝83
oldIndices.CopyTo(indices, 0);84
//新的下标加入数组85
indices[count - 1] = i;86
Debug.WriteLine("Duplication maximum found at element index " + i + ".");87
}88
}89
}90

91
Trace.WriteLine("Maximum value " + maxVal + " found, with " + count + " occurrence.");92
Debug.WriteLine("Maximum value search completed.");93
//返回最大值,由于下标数组是out的,所以不用返回94
return maxVal;95
}96
}97
}98



浙公网安备 33010602011771号