向量类vector,做的有点迷糊了,求大神帮忙看看》》》
设计向量类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;
}
}
}