List<T>的搜索和排序
2010-11-16 09:32
393 查看
原来是通过委托来实现的啊0 0...
Code:
public class Vector
{
public double? R = null;
public double? Theta = null;
public double? ThetaRadians
{
get
{
return (Theta * Math.PI / 180.0);
}
}
public Vector(double? r, double? theta)
{
if (r > 0)
{
r = -r;
theta += 180;
}
theta = theta % 360;
R = r;
Theta = theta;
}
/// <summary>
/// 重载+运算符
/// </summary>
/// <param name="op1"></param>
/// <param name="op2"></param>
/// <returns></returns>
public static Vector operator +(Vector op1, Vector op2)
{
try
{
double newX = op1.R.Value * Math.Sin(op1.ThetaRadians.Value) + op2.R.Value * Math.Sin(op2.ThetaRadians.Value);
double newY = op1.R.Value * Math.Cos(op1.ThetaRadians.Value) + op2.R.Value * Math.Cos(op2.ThetaRadians.Value);
double newR = Math.Sqrt(newX * newX + newY * newY);
double newTheta = Math.Atan2(newX, newY) * 180.0 / Math.PI;
return new Vector(newR, newTheta);
}
catch
{
return new Vector(null, null);
}
}
/// <summary>
/// 重载-运算符
/// </summary>
/// <param name="op1"></param>
/// <returns></returns>
public static Vector operator -(Vector op1)
{
return new Vector(-op1.R, op1.Theta);
}
/// <summary>
/// 重载-运算符
/// </summary>
/// <param name="op1"></param>
/// <param name="op2"></param>
/// <returns></returns>
public static Vector operator -(Vector op1, Vector op2)
{
return op1 + (-op2);
}
/// <summary>
/// 重写ToString()运算符
/// </summary>
/// <returns></returns>
public override string ToString()
{
string rString = R.HasValue ? R.ToString() : "null";
string thetaString = Theta.HasValue ? Theta.ToString() : "null";
return string.Format("({0},{1})", rString, thetaString);
}
}
class Vectors:List<Vector>
{
public Vectors()
{
}
public Vectors(IEnumerable<Vector> initiaItems)
{
foreach (Vector vector in initiaItems)
{
Add(vector);
}
}
public string Sum()
{
StringBuilder sb = new StringBuilder();
Vector currenPoint = new Vector(0.0, 0.0);
sb.Append("origin");
foreach (Vector vector in this)
{
sb.AppendFormat(" + {0}", vector);
currenPoint += vector;
}
sb.AppendFormat(" = {0}", currenPoint);
return sb.ToString();
}
}
public static class VectorDelegates
{
//排序方法
public static int Compare(Vector x,Vector y)
{
if (x.R > y.R)
{
return 1;
}
else if (x.R < y.R)
{
return -1;
}
else
{
return 0;
}
}
//搜索方法
public static bool TopRightQuadrant(Vector target)
{
if (target.Theta >= 0.0 && target.Theta <= 90.0)
{
return true;
}
else
{
return false;
}
}
}
class Program
{
static void Main(string[] args)
{
Vectors route = new Vectors();
route.Add(new Vector(2.0, 90.0));
route.Add(new Vector(1.0, 180.0));
route.Add(new Vector(0.5, 45.0));
route.Add(new Vector(2.5, 315.0));
Console.WriteLine(route.Sum());
//排序委托
Comparison<Vector> sorter = new Comparison<Vector>(VectorDelegates.Compare);
//排序
route.Sort(sorter);
Console.WriteLine(route.Sum());
//搜索委托
Predicate<Vector> searcher = new Predicate<Vector>(VectorDelegates.TopRightQuadrant);
//搜索
Vectors topRightQuadrantRoute = new Vectors(route.FindAll(searcher));
Console.WriteLine(topRightQuadrantRoute.Sum());
Console.ReadKey();
}
}
Code:
public class Vector
{
public double? R = null;
public double? Theta = null;
public double? ThetaRadians
{
get
{
return (Theta * Math.PI / 180.0);
}
}
public Vector(double? r, double? theta)
{
if (r > 0)
{
r = -r;
theta += 180;
}
theta = theta % 360;
R = r;
Theta = theta;
}
/// <summary>
/// 重载+运算符
/// </summary>
/// <param name="op1"></param>
/// <param name="op2"></param>
/// <returns></returns>
public static Vector operator +(Vector op1, Vector op2)
{
try
{
double newX = op1.R.Value * Math.Sin(op1.ThetaRadians.Value) + op2.R.Value * Math.Sin(op2.ThetaRadians.Value);
double newY = op1.R.Value * Math.Cos(op1.ThetaRadians.Value) + op2.R.Value * Math.Cos(op2.ThetaRadians.Value);
double newR = Math.Sqrt(newX * newX + newY * newY);
double newTheta = Math.Atan2(newX, newY) * 180.0 / Math.PI;
return new Vector(newR, newTheta);
}
catch
{
return new Vector(null, null);
}
}
/// <summary>
/// 重载-运算符
/// </summary>
/// <param name="op1"></param>
/// <returns></returns>
public static Vector operator -(Vector op1)
{
return new Vector(-op1.R, op1.Theta);
}
/// <summary>
/// 重载-运算符
/// </summary>
/// <param name="op1"></param>
/// <param name="op2"></param>
/// <returns></returns>
public static Vector operator -(Vector op1, Vector op2)
{
return op1 + (-op2);
}
/// <summary>
/// 重写ToString()运算符
/// </summary>
/// <returns></returns>
public override string ToString()
{
string rString = R.HasValue ? R.ToString() : "null";
string thetaString = Theta.HasValue ? Theta.ToString() : "null";
return string.Format("({0},{1})", rString, thetaString);
}
}
class Vectors:List<Vector>
{
public Vectors()
{
}
public Vectors(IEnumerable<Vector> initiaItems)
{
foreach (Vector vector in initiaItems)
{
Add(vector);
}
}
public string Sum()
{
StringBuilder sb = new StringBuilder();
Vector currenPoint = new Vector(0.0, 0.0);
sb.Append("origin");
foreach (Vector vector in this)
{
sb.AppendFormat(" + {0}", vector);
currenPoint += vector;
}
sb.AppendFormat(" = {0}", currenPoint);
return sb.ToString();
}
}
public static class VectorDelegates
{
//排序方法
public static int Compare(Vector x,Vector y)
{
if (x.R > y.R)
{
return 1;
}
else if (x.R < y.R)
{
return -1;
}
else
{
return 0;
}
}
//搜索方法
public static bool TopRightQuadrant(Vector target)
{
if (target.Theta >= 0.0 && target.Theta <= 90.0)
{
return true;
}
else
{
return false;
}
}
}
class Program
{
static void Main(string[] args)
{
Vectors route = new Vectors();
route.Add(new Vector(2.0, 90.0));
route.Add(new Vector(1.0, 180.0));
route.Add(new Vector(0.5, 45.0));
route.Add(new Vector(2.5, 315.0));
Console.WriteLine(route.Sum());
//排序委托
Comparison<Vector> sorter = new Comparison<Vector>(VectorDelegates.Compare);
//排序
route.Sort(sorter);
Console.WriteLine(route.Sum());
//搜索委托
Predicate<Vector> searcher = new Predicate<Vector>(VectorDelegates.TopRightQuadrant);
//搜索
Vectors topRightQuadrantRoute = new Vectors(route.FindAll(searcher));
Console.WriteLine(topRightQuadrantRoute.Sum());
Console.ReadKey();
}
}
相关文章推荐
- List<T>在搜索和排序时采用不同方法的性能比较
- 聊聊学习List<T>搜索与排序的心得
- List<T> 排序及筛选
- Winform中DataGridView绑定List<T>数据源时,点击DataGridView列头不能自动排序解决方法
- 对List<>中对象的属性进行排序。
- java 之Sting类型的List集合的汉字排序和String类型数组的汉字排序及List<Bean>排序
- List<Map<String, Object>> 不同key值进行排序备忘
- List<Report> list结果的排序(升序\降序)实现Compare接口
- List<T> 根据某个字段排序
- List<T> 排序的几种方式
- List<Object>集合按照Object的某个字段排序,解决取不到父类字段问题
- List<Map> 排序
- List<T> 排序问题分享
- C# 中 List.Sort运用(IComparer<T>)排序用法
- Java List<Object>根据Object中某一个属性排序
- 对List<T> 随机排序
- List<object>排序
- List&lt;T&gt;排序
- C#泛型集合List<T>自带方法之排序
- java list<Map<String,Object>>排序