![]() |
#2
yhlvht2016-10-12 21:56
![]() /*设计向量类Vector, (1) 字义私有字段,实型数组;double[] a = new double[10]; (2) 定义索引函数,完成对数组每个元素的读写操作; (3) 定义公有无参构造函数public Vector(),数组长度为10个元素,随机产生10个数,给数组赋值; (4) 定义公有有参构造函数public Vector(int len),数组长度为len个元素,随机产生len个数,给数组赋值; (5) 定义公有有参构造函数public Vector(params double[] b),对数组字段赋值; (6) 定义公有函数,求向量中的最大值public double Max(); (7) 定义公有函数,求向量各元素之和public double GetSum(); (8) 定义公有函数,查找某个元素是否在向量之中public bool IsVector(double d)。 (9) 设计测试类,完成上述功能的测试。*/ using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace vector { class Program { static void Main(string[] args) { //Vector v1 = new Vector(); //Console.WriteLine("向量的最大值为:{0}",v1.Max() ); //Console.WriteLine("向量各元素之和为:{0}",v1.Getsum ()); //Console.WriteLine(v1.ISVector (10)); Console.WriteLine("测试开始"); Console.WriteLine("构造1"); Vector v1 = new Vector(); Console.WriteLine("构造2"); Vector v2 = new Vector(20); Console.WriteLine("构造3"); double[] b = new double[30]; Vector v3 = new Vector(b); Console.WriteLine("构造1向量的最大值为:{0}", v1.Max()); Console.WriteLine("构造2向量的最大值为:{0}", v2.Max()); Console.WriteLine("构造3向量的最大值为:{0}", v3.Max()); Console.WriteLine("构造1向量各元素之和为:{0}", v1.Getsum()); Console.WriteLine("构造2向量各元素之和为:{0}", v2.Getsum()); Console.WriteLine("构造3向量各元素之和为:{0}", v3.Getsum()); Console.WriteLine("测试10是否在向量中"); Console.WriteLine(v1.ISVector(10)); Console.WriteLine(v2.ISVector(10)); Console.WriteLine(v3.ISVector(10)); Console.WriteLine("测试索引函数"); v1[5] = 10; v2[5] = 10; v3[5] = 10; Console.WriteLine("测试10是否在向量中"); Console.WriteLine(v1.ISVector(10)); Console.WriteLine(v2.ISVector(10)); Console.WriteLine(v3.ISVector(10)); Console.Read(); } } class Vector { //首先构造函数的作用是构造该类,有三个构造函数,则表示该类可以通过三种不同的方式来构造 //然后该类具有三个功能函数,以及一个索引函数,无论通过哪一种方式构造出该类,功能函数和索引函数都需要起作用 //(1) 字义私有字段,实型数组;double[] a = new double[10]; //定义私有字段a(实型数组),并初始化为10个元数,这个初始化的目的主要用于(3)无参构造函数,即当不指定数组元素个数时,默认为10个元素 private double[] a = new double[10]; //索引函数,用于对每个元数的读写操作 public double this[int i] { get { return this.a[i]; } set { this.a[i] = value; } } //无参构造函用,用于不指定元素个数时,使用默认的元素个数 public Vector() { int i; Random rd = new Random (); //Console.WriteLine("首次产生的随机数为:");这没有什么首次二次的说法,是理解错误 for (i = 0; i < a.Length ; i++) { double n = rd.Next(); a[i] = n; Console.WriteLine("{0,-5}",a[i]); } } //具有指定数组元素个数的有参构造函数,使用传入的参数作为元素个数 public Vector(int len) { //使用传入的参数重新构造数组 a = new double[len]; int i; Random rd = new Random(); //Console.WriteLine("第二次产生的随机数为:"); for (i = 0; i < len ; i++) { a[i] = rd.Next(); Console.WriteLine("{0,-5}", a[i]); } } //具有指定数组的有参构造函数,使用传入的数组作为目标数组,并赋值 public Vector(params double[] b) { //将传入的数组b赋给变量a a = b; int i; Random rd = new Random(); //Console.WriteLine("第二次产生的随机数为:"); for (i = 0; i < a.Length; i++) { a[i] = rd.Next(); Console.WriteLine("{0,-5}", a[i]); } } //求最大值 public double Max() { double max = a[0]; for (int i = 0; i < a.Length; i++) { if (a[i] > max) { max = a[i]; } } return max; } //求和 public double Getsum() { double sum = 0; int i; for (i = 0; i < 10; i++) { sum = sum + a[i]; } return sum; } //查询 public bool ISVector(double d) { //int i = 0; //if (a[i] == d) return true; //else // return false; //需要循环数组中的每一个元数,才能确定需要查找的值在不在数组中 for (int i = 0; i < a.Length; i++) { if (a[i] == d) { return true; } } return false; } } } |
设计向量类Vector,
(1) 字义私有字段,实型数组;double[] a = new double[10];
(2) 定义索引函数,完成对数组每个元素的读写操作;
(3) 定义公有无参构造函数public Vector(),数组长度为10个元素,随机产生10个数,给数组赋值;
(4) 定义公有有参构造函数public Vector(int len),数组长度为len个元素,随机产生len个数,给数组赋值;
(5) 定义公有有参构造函数public Vector(params double[] b),对数组字段赋值;
(6) 定义公有函数,求向量中的最大值public double Max();
(7) 定义公有函数,求向量各元素之和public double GetSum();
(8) 定义公有函数,查找某个元素是否在向量之中public bool IsVector(double d)。
(9) 设计测试类,完成上述功能的测试。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace vector
{
class Program
{
static void Main(string[] args)
{
Vector v1 = new Vector();
Console.WriteLine("向量的最大值为:{0}",v1.Max() );
Console.WriteLine("向量各元素之和为:{0}",v1.Getsum ());
Console.WriteLine(v1.ISVector (10));
Console.Read();
}
}
class Vector
{
private double[] a=new double[10];
public double this[int i]
{
get { return this.a[i]; }
set { this.a[i] = value; }
}
public Vector()
{
int i;
Random rd=new Random ();
Console.WriteLine("首次产生的随机数为:");
for (i = 0; i < a.Length ; i++)
{
double n = rd.Next();
a[i] = n ;
Console.WriteLine("{0,-5}",a[i ]);
}
}
public Vector(int len)
{
int i;
Random rd = new Random();
Console.WriteLine("第二次产生的随机数为:");
for (i = 0; i < len ; i++)
{
a[i] = rd.Next();
Console.WriteLine();
}
}
public Vector(params double[] b)
{
int i;
for (i = 0; i < b.Length; i++)
{
Console.WriteLine(b[i ]);
}
}
public double Max()
{
double max=a[0];
for (int i=0; i < a.Length; i++)
{
if (a[i] > max)
{ max = a[i]; }
}return max;
}
public double Getsum()
{
double sum = 0;
int i;
for (i = 0; i < 10; i++)
{
sum = sum + a[i];
}
return sum;
}
public bool ISVector(double d)
{
int i=0;
if (a[i] == d) return true;
else
return false;
}
}
}