StudentAndPosition

来源:互联网 发布:海南网络诈骗最新新闻 编辑:程序博客网 时间:2024/05/29 04:55

//CalDistance

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace StudentLib
{
public struct Distance
{

public static double CalDistance(Position p1, Position p2)
{
return Math.Sqrt(Math.Pow(p1.X - p2.X, 2) + Math.Pow(p1.Y - p2.Y, 2));
  }

}
}

 

 

//IHasDistance<T>

using System;
using System.Collections.Generic;
using System.Linq;

 

using System.Text;

namespace StudentLib
{
public interface IHasDistance<T>


{
double CalDistance(T t);
}
}

 

// IHasPosition
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace StudentLib
{
public interface IHasPosition
{
Position ReturnCurrentPosition();
}
}


//Instructor

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace StudentLib
{
  public class Instructor : Person, IEquatable<Instructor>
{
Dictionary<string, KeyValuePair<int, int>> subjectsAndGrades;

public Instructor()
{
subjectsAndGrades = new Dictionary<string, KeyValuePair<int, int>>();
}

public Instructor(string name, string school) : base(name, school)
{
subjectsAndGrades = new Dictionary<string, KeyValuePair<int, int>>();
}

public override void addSubjectGrade(string subject, params Int32[] objs)
{
KeyValuePair<int, int> pg = new KeyValuePair<int, int>(objs[0], objs[1]);
subjectsAndGrades.Add(subject, pg);
}

public KeyValuePair<int, int> this[string subject]
{
get{

KeyValuePair<int, int> e = subjectsAndGrades[subject];
return e;
}

}

public override double Rating
{
get { throw new NotImplementedException(); }
}

public override string ToString()
{
StringBuilder str = new StringBuilder(base.ToString());
if (subjectsAndGrades.Count == 0)
return str.ToString();
str.Append("( ");
foreach (KeyValuePair<string, KeyValuePair<int, int>> entry in subjectsAndGrades)
{
str.Append(entry.Key).Append("=");
KeyValuePair<int, int> kv = entry.Value;
str.Append(kv.Key).Append(",").Append(kv.Value).Append(" ");
}
str.Append(")");
return str.ToString();
}


#region IEquatable<Instructor> 成员

public bool Equals(Instructor other)
{
return this.Name.Equals(other.Name);
}

#endregion

public override bool Equals(object obj)
{
if (null == obj)
return false;
if (GetType() != obj.GetType())
return false;
Instructor i = obj as Instructor;
return i.Name.Equals(Name) && i.School.Equals(School);
}

public override int GetHashCode()
{
return base.GetHashCode();
}
}
}


//ISepatarion


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace StudentLib
{
public interface ISeparation : IHasPosition
{
double CalDistance(IHasPosition p);
}
}

//Person


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace StudentLib
{
public abstract class Person : ISeparation
{
string name;

public string Name
{
get { return name; }
set { name = value; }
}
string school;

public string School
{
get { return school; }
set { school = value; }
}

Position position;

public Position Position
{
get { return position; }
set { position = value; }
}

//must have abstract
public abstract double Rating
{
get;
}

public Person()
{
}

public Person(string name, string school)
{
this.name = name;
this.school = school;
}

public override string ToString()
{
return name + " " + school;
}

public abstract void addSubjectGrade(string subject, params Int32[] objs);

#region IHasPosition 成员

public Position ReturnCurrentPosition()
{
return position;

}

#endregion

/*
//no public
Position IHasPosition.ReturnCurrentPosition()
{
return position;
}
* */

/* error
* #region ISeparation 成员

double ISeparation.CalDistance(IHasPosition p)
{
return Distance.CalDistance(this.position, p.ReturnCurrentPosition());
}

#endregion
* */


public double CalDistance(IHasPosition p)
{
return Distance.CalDistance(this.position, p.ReturnCurrentPosition());
}

}
}

//Position

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace StudentLib
{
public struct Position : IHasDistance<Position>
{
private int x;

public int X
{
get { return x; }
set { x = value; }
}

private int y;

public int Y
{
get { return y; }
set { y = value; }
}

public Position(int x, int y)
{
this.x = x;
this.y = y;
}

public void moveAbsolute(int x, int y)
{
this.x = x;
this.y = y;
}

public void moveRelative(int xr, int yr)
{
x += xr;
y += yr;
}

public override string ToString()
{
return "( " + x + ", " + y + ")";
}

public override bool Equals(Object o)
{
if (null == o)
return false;
if (o.GetType() != this.GetType())
return false;
//as can only be used for reference type
Position p = (Position) o;
return p.X == x && p.Y == y;
}

public override int GetHashCode()
{
return x * y;
}

public static bool operator ==(Position p1, Position p2)
{
return p1.X == p2.X && p1.Y == p2.Y;
}

public static bool operator !=(Position p1, Position p2)
{
return !(p1 == p2);
}

public static Position operator +(Position p1, Position p2)
{
return new Position(p1.X + p2.X, p1.Y + p2.Y);
}

public static Position operator -(Position p1, Position p2)
{
return new Position(p1.X - p2.X, p1.Y - p2.Y);
}


public double CalDistance(Position t)
{
return Distance.CalDistance(this, t);
}
}
}

 //Student

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace StudentLib
{
public class Student : Person, IComparable<Student>, IEquatable<Student>
{
Dictionary<string, int> subjectsAndGrades;

public Student ()
{
}

public Student (string name, string school) : base(name, school)
{
subjectsAndGrades = new Dictionary<string, int>();
}

public override void addSubjectGrade(string subject, params Int32[] objs)
{
subjectsAndGrades.Add(subject, objs[0]);
}

public override string ToString()
{
StringBuilder str = new StringBuilder(base.ToString());
if (subjectsAndGrades.Count == 0)
return str.ToString();
str.Append("( ");
foreach (KeyValuePair<string, int> entry in subjectsAndGrades)
{
str.Append(entry.Key).Append("=").Append(entry.Value).Append(" ");
}
str.Append(")");
return str.ToString();
}

public int CompareTo(Student s)
{
return Name.CompareTo(s.Name);
}


public int this[string subject]
{
get{
// Random random = new Random();
// return random.Next();
return subjectsAndGrades[subject];
}
set{
}
}

//must have override
public override double Rating
{
get
{
double sum = 0;
foreach (KeyValuePair<string, int> entry in subjectsAndGrades)
{
sum += entry.Value;
}
return sum / subjectsAndGrades.Count;
}
}

#region IEquatable<Student> 成员

public bool Equals(Student other)
{
Console.WriteLine("student Equitable equals()");
return Name.Equals(other.Name) &&
School.Equals(other.School);
}

#endregion

public override bool Equals(object obj)
{
Console.WriteLine("student object equals()");


if (null == obj)
return false;
if (GetType() != obj.GetType())
return false;
Student s = obj as Student;

Console.WriteLine("student object equals()");
return Name.Equals(s.Name) && School.Equals(s.School);
}

public override int GetHashCode()
{
return base.GetHashCode();
}


}
}

 //Utility

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace StudentLib
{
public class Utility
{
public static IHasPosition FindCloset(ISeparation source, IHasPosition[] targets)
{
IHasPosition r = targets[0];
foreach (IHasPosition t in targets) {
if (source.CalDistance(r) > source.CalDistance(t))
r = t;
}
return r;
}

public static T FindCloseGeneric<T>(T source, T[] targets)
where T : IHasDistance<T>
{
T r = targets[0];
foreach (T t in targets)
{
if (source.CalDistance(r) > source.CalDistance(t))
r = t;
}
return r;
}
}
}

 //Program

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using StudentLib;

namespace FiveOnePosition
{
class Program
{
static void Main(string[] args){

// testTwoOne();


/* string str = args[0];
Console.WriteLine(str);
testTwoTwoAndThree(str);
* */

// testSwap();

// testFourTwo();

// testFourThree();

// testFiveTwo();

// testFiveThreePosition();
// testFiveFour();

// testFindCloset();

// testEquals();

testFindClosetGeneric();

}

static void testTwoOne()
{
Student s1 = new Student("s1", "school1");
s1.addSubjectGrade("java", 66);
Console.WriteLine(s1);

Student s2 = new Student("s2", "schol2");
Console.WriteLine(s2);
}

static void testTwoTwoAndThree(string str)
{
int len = Int32.Parse(str);
Student[] ss = new Student[len];

ss[0] = new Student("s4", "school1");
ss[1] = new Student("s2", "school2");
ss[3] = new Student("s3", "school3");
ss[2] = new Student("s1", "school2");
foreach (Student s in ss)
{
Console.WriteLine(s);
}

Array.Sort(ss);
Console.WriteLine();
foreach (Student s in ss)
{
Console.WriteLine(s);
}

Console.WriteLine();
int index = Array.BinarySearch(ss, ss[1]);
Console.WriteLine(index);

Console.WriteLine();
ss[0].addSubjectGrade("java", 66);
Console.WriteLine(ss[0]["java"]);
}


static void testSwap()
{
Student s1 = new Student("s1", "school1");
Student s2 = new Student("s2", "schol2");

swap(s1, s2);
Console.WriteLine(s1);
Console.WriteLine(s2);

Console.WriteLine();
swap(ref s1, ref s2);
Console.WriteLine(s1);
Console.WriteLine(s2);
}

static void swap(Student s1, Student s2)
{
Student temp = s1;
s1 = s2;
s2 = temp;
}

static void swap(ref Student s1, ref Student s2)
{
Student temp = s1;
s1 = s2;
s2 = temp;
}

/* static void swap(out Student s1, out Student s2)
{
Student temp = s1;
s1 = s2;
s2 = temp;
}
* */

static void testFourTwo()
{
Student s1 = new Student("s1", "school1");
s1.addSubjectGrade("java", 66);
s1.addSubjectGrade("c++", 63);
s1.addSubjectGrade("c#", 76);

Console.WriteLine(s1.Rating);
}

static void testFourThree()
{
Person[] ps = new Person[3];
ps[0] = new Instructor("i1", "scholl1");
ps[0].addSubjectGrade("java", 3, 77);
ps[0].addSubjectGrade("c++", 4, 88);

ps[1] = new Student("s1", "school1");
ps[1].addSubjectGrade("java", 66);
ps[1].addSubjectGrade("c++", 63);
ps[1].addSubjectGrade("c#", 76);

ps[2] = new Student("s2", "schol2");

foreach (Person p in ps)
{
Console.WriteLine(p);
}
}

static void testFiveTwo()
{
Student s1 = new Student("s1", "school1");
s1.addSubjectGrade("java", 66);
s1.addSubjectGrade("c++", 63);
s1.addSubjectGrade("c#", 76);
s1.Position = new Position(1, 1);

Console.WriteLine(s1);
Console.WriteLine(s1.ReturnCurrentPosition());
}


static void testFiveThreePosition()
{
Position p1 = new Position(3, 3);
Position p2 = new Position(5, 5);

Console.WriteLine(p1 + p2);
Console.WriteLine(p1 - p2);
Console.WriteLine(p1 == p2);
Console.WriteLine(p1 != p2);
Console.WriteLine(p1 == p1);
}

static void testFiveFour()
{
Person[] ps = new Person[3];
ps[0] = new Instructor("i0", "scholl0");
ps[0].Position = new Position(3, 3);
ps[1] = new Student("s1", "school1");
ps[1].Position = new Position(4, 4);
ps[2] = new Student("s2", "schol2");
ps[2].Position = new Position(5, 5);

Console.WriteLine(ps[0].CalDistance(ps[1]));
Console.WriteLine(ps[0].CalDistance(ps[2]));
}

//Five Five
static void testFindCloset()
{
Person[] ps = new Person[3];
ps[0] = new Instructor("i0", "scholl0");
ps[0].Position = new Position(2, 2);
ps[1] = new Student("s1", "school1");
ps[1].Position = new Position(5, 5);
ps[2] = new Student("s2", "schol2");
ps[2].Position = new Position(8, 8);

Person p = new Student("p", "pp");
p.Position = new Position(3, 3);
IHasPosition r = Utility.FindCloset(p, ps);
Person pr = r as Person;
Console.WriteLine(pr);
}


static void testEquals()
{
Student s1 = new Student("s1", "school1");
Student s2 = new Student("s1", "school1");
Student s3 = new Student("s1", "school2");
Console.WriteLine(s1.Equals(s2));
Console.WriteLine(s1.Equals(s3));


Console.WriteLine();
Instructor i1 = new Instructor("i1", "scholl1");
Instructor i2 = new Instructor("i1", "scholl1");
Instructor i3 = new Instructor("i1", "scholl2");
Console.WriteLine(i1.Equals(i2));
Console.WriteLine(i1.Equals(i3));

}

//Five Five
static void testFindClosetGeneric()
{
Position[] ps = new Position[3];
ps[0] = new Position(2, 2);
ps[1] = new Position(5, 5);
ps[2] = new Position(8, 8);

Position p = new Position(4, 4);
Position r = Utility.FindCloseGeneric<Position>(p, ps);
Position pr = (Position)r;
Console.WriteLine(pr);
}
}

}

 

 

 

原创粉丝点击