熟悉c#语言:数组和类的定义-数据结构与算法实验

上传人:第*** 文档编号:32759442 上传时间:2018-02-12 格式:DOC 页数:14 大小:83KB
返回 下载 相关 举报
熟悉c#语言:数组和类的定义-数据结构与算法实验_第1页
第1页 / 共14页
熟悉c#语言:数组和类的定义-数据结构与算法实验_第2页
第2页 / 共14页
熟悉c#语言:数组和类的定义-数据结构与算法实验_第3页
第3页 / 共14页
熟悉c#语言:数组和类的定义-数据结构与算法实验_第4页
第4页 / 共14页
熟悉c#语言:数组和类的定义-数据结构与算法实验_第5页
第5页 / 共14页
点击查看更多>>
资源描述

《熟悉c#语言:数组和类的定义-数据结构与算法实验》由会员分享,可在线阅读,更多相关《熟悉c#语言:数组和类的定义-数据结构与算法实验(14页珍藏版)》请在金锄头文库上搜索。

1、实验 1 熟悉 C#语言:数组和类的定义实验目的:理解 C#的基本概念及其基本操作,重点是数组的处理和新的类型的定义。题意:1. 定义和初始化数组,在数组中查找特定数据,对数组中的数据进行排序。2. 设计复数类,实现复数的基本操作。1. 泛型方法C#语言中泛型的优越性在下面的一段例子中应能较好的显示出来。对于同样的运算逻辑(例子中是交换两个变量的内容) ,但仅是数据的类型不一样,可能就需要定义一堆相似的方法;而应用泛型特性则可仅定义一个泛型方法(例子中是 swap) 。static void Main(string args) int a = 3; int b = 7;swapint(ref

2、a, ref b);double ad = 3.5; double bd = 7.5;swapdouble(ref ad, ref bd);swap(ref a, ref b);static void swapint(ref int a, ref int b) int x = a;a = b;b = x;static void swapdouble(ref double a, ref double b) double x = a;a = b;b = x;static void swap(ref T a, ref T b) T x = a;a = b;b = x; 2. 定义和初始化数组,在数组

3、中查找特定数据,对数组中的数据进行排序。class Program static void Main(string args) int a = new int20;RandomizeData(a);Show(a);int i = Array.IndexOf(a, 10);Console.WriteLine(10s index is: 0, i);Console.WriteLine(Sorted Array: );Array.Sort(a);Show(a);private static void Show(int a) for (int i = 0; i (ca, new Complex();i

4、nt i = Array.IndexOf(ca, ca5);Console.WriteLine(0s index is: 1, ca5, i);Console.WriteLine(Sorted Array: ); Array.Sort(ca);Show(ca);private static void Show(Complex a) for (int i = 0; i 0)s = rp.ToString(F) + + + ip.ToString(F) + j;else if (ip 0)s = ip.ToString(F) + j;else if (ip = Math.Abs(c.ip) e =

5、 c.ip / c.rp;f = c.rp + e * c.ip;x = (rp + ip * e) / f;y = (ip - rp * e) / f;else e = c.rp / c.ip;f = c.ip + e * c.rp;rp = (rp * e + ip) / f;ip = (ip * e - rp) / f;return this;/* 计算复数的模* * return double型,指定复数的模*/public double Abs() return (Math.Sqrt(rp*rp +ip*ip);/* 计算复数的实幂指数* * param x - 待求实幂指数的幂次*

6、 return Complex型,复数的实幂指数值*/public Complex Pow(double x) / 常量const double PI = 3.14159265358979;/ 局部变量double r, t;/ 特殊值处理if (rp = 0) & (ip = 0)return this; / 幂运算公式中的三角函数运算if (rp = 0) if (ip 0)t = 1.5707963268;elset = -1.5707963268;else if (rp 0)t = Math.Atan2(ip, rp);else if (ip = 0)t = Math.Atan2(ip

7、, rp) + PI;elset = Math.Atan2(ip, rp) - PI;/ 模的幂r = Math.Exp(x * Math.Log(Math.Sqrt(rp * rp + ip * ip);/ 复数的实幂指数rp = r * Math.Cos(x * t);ip = r * Math.Sin(x * t);return this;/* 计算复数的自然对数* * return Complex型,复数的自然对数值*/public Complex Log() double p = Math.Log(Math.Sqrt(rp * rp + ip * ip);rp = p; ip = M

8、ath.Atan2(ip, rp);return this;/* 计算复数的正弦* * return Complex型,复数的正弦值 */public Complex Sin() int i;double x, y, y1, br, b1, b2;double c = new double6;/ 切比雪夫公式的常数系数c0 = 1.13031820798497;c1 = 0.04433684984866;c2 = 0.00054292631191;c3 = 0.00000319843646;c4 = 0.00000001103607;c5 = 0.00000000002498;y1 = Mat

9、h.Exp(ip);x = 0.5 * (y1 + 1 / y1);br = 0;if (Math.Abs(ip) = 1)y = 0.5 * (y1 - 1 / y1);else b1 = 0;b2 = 0;y1 = 2 * (2 * ip * ip - 1);for (i = 5; i = 0; -i) br = y1 * b1 - b2 - ci;if (i != 0) b2 = b1;b1 = br;y = ip * (br - b1);/ 组合计算结果rp = x * Math.Sin(rp);ip = y * Math.Cos(rp);return this;/* 计算复数的余弦*

10、 * return Complex型,复数的余弦值*/public Complex Cos() int i;double x, y, y1, br, b1, b2;double c = new double6;/ 切比雪夫公式的常数系数c0 = 1.13031820798497;c1 = 0.04433684984866;c2 = 0.00054292631191;c3 = 0.00000319843646;c4 = 0.00000001103607;c5 = 0.00000000002498;y1 = Math.Exp(ip);x = 0.5 * (y1 + 1 / y1);br = 0;i

11、f (Math.Abs(ip) = 1)y = 0.5 * (y1 - 1 / y1);else b1 = 0;b2 = 0;y1 = 2 * (2 * ip * ip - 1);for (i = 5; i = 0; -i) br = y1 * b1 - b2 - ci;if (i != 0) b2 = b1;b1 = br;y = ip * (br - b1);/ 组合计算结果rp = x * Math.Cos(rp);ip = -y * Math.Sin(rp);return this;public int CompareTo(object obj) Complex c = obj as Complex; if (c = null)throw new ArgumentException(CompareTo(Complex));if (this.Equals(c)return 0;double d1, d2;d1 = this.Abs();d2 = c.Abs();if (d1 d2)return -1;elsereturn 1;

展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 建筑/环境 > 工程造价

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号