C#:使用堆栈创建方法
我在做一个C#项目。我正在尝试创建一种方法,在该方法中,我必须使用堆栈 该项目是关于在指定时间内移动一组平面。每架飞机的信息都包含在一个名为“飞行计划”的类别中,该类别包括:速度、初始位置(x,y)、最终位置(x,y)、航线 我创建了以下方法:C#:使用堆栈创建方法,c#,methods,stack,C#,Methods,Stack,我在做一个C#项目。我正在尝试创建一种方法,在该方法中,我必须使用堆栈 该项目是关于在指定时间内移动一组平面。每架飞机的信息都包含在一个名为“飞行计划”的类别中,该类别包括:速度、初始位置(x,y)、最终位置(x,y)、航线 我创建了以下方法: public void Move(double time) { double distance = (time * this.Speed) / 60; double hyp = Math.Sqrt( (this.D
public void Move(double time)
{
double distance = (time * this.Speed) / 60;
double hyp = Math.Sqrt( (this.Destination.GetX() - this.Actual.GetX())^2 + (this.Destination.GetY() - this.Actual.GetY())^2 );
if (hyp < distance)
{
this.Actual = this.Destination;
}
else
{
double cosine = (this.Destination.GetX() - this.Actual.GetX()) / hyp;
double sine = (this.Destination.GetY() - this.Actual.GetY()) / hyp;
this.Actual.SetX(this.Actual.GetX() + (distance * cosine));
this.Actual.SetY(this.Actual.GetY() + (distance * sine));
}
}
公共作废移动(双倍)
{
双倍距离=(时间*此速度)/60;
double hyp=Math.Sqrt((this.Destination.GetX()-this.Actual.GetX())^2+(this.Destination.GetY()-this.Actual.GetY())^2);
if(高度<距离)
{
this.Actual=this.Destination;
}
其他的
{
双余弦=(this.Destination.GetX()-this.Actual.GetX())/hyp;
双正弦=(this.Destination.GetY()-this.Actual.GetY())/hyp;
this.Actual.SetX(this.Actual.GetX()+(距离*余弦));
this.Actual.SetY(this.Actual.GetY()+(距离*正弦));
}
}
此方法将飞机移动到指定时间后到达的位置
现在,我的问题是:我必须创建一个方法,允许我返回到最后一个位置(即撤消Move
方法)。为此,我必须使用堆栈。我真的不习惯使用堆栈,而且在考虑太多之后,我不会使用这种方法
当飞机在每个时间周期后移动时,堆栈应该存储所有位置,然后我必须从堆栈中弹出它们,这样它到达的最后一个位置将是离开堆栈的第一个位置
你们能帮我写代码吗?尽管它并不精确,因为您不知道创建的所有变量,但我只需要一些关于如何操作的提示
我认为它应该是这样开始的(因为你现在不需要时间)publicsvoidmoveback()
事先谢谢你。我有以下建议。创建方法SetPosition而不是SetX和SetY函数。然后,该函数使用堆栈来保存更改的历史记录。像这样的
void SetPosition(double x, double y)
{
positionHistoryX.Push(x);
positionHistoryY.Push(y);
X = x;
Y = y;
}
然后可以实现一个名为MoveToLastPosition()的方法,该方法从堆栈中弹出x和y位置,并将它们放入x和y属性中
编辑:在Zee的评论之后,我只是想指出,最好有一个堆栈来存储有关某个位置的所有信息,但我没有这样做,因为我没有任何可供使用的结构。所以也许你能做的是
struct LocationInfo
{
double X;
double Y;
double Time;
}
然后,您可以将Stack类型的字段添加到类中,并开始在Move方法中使用它。我建议如下。创建方法SetPosition而不是SetX和SetY函数。然后,该函数使用堆栈来保存更改的历史记录。像这样的
void SetPosition(double x, double y)
{
positionHistoryX.Push(x);
positionHistoryY.Push(y);
X = x;
Y = y;
}
然后可以实现一个名为MoveToLastPosition()的方法,该方法从堆栈中弹出x和y位置,并将它们放入x和y属性中
编辑:在Zee的评论之后,我只是想指出,最好有一个堆栈来存储有关某个位置的所有信息,但我没有这样做,因为我没有任何可供使用的结构。所以也许你能做的是
struct LocationInfo
{
double X;
double Y;
double Time;
}
然后,您可以将Stack类型的字段添加到类中,并开始在Move方法中使用它。这应该会将您推向正确的方向
public class MainClass
{
private Stack<Coordinates> _coordinateHistory; //Stores the coordinate history
//Base class that initializes the stack of coordinate history.
public MainClass()
{
_coordinateHistory = new Stack<Coordinates>();
}
//Used to create a new coordinate object and add it to the coordinate history stack.
public void AddCoordinates(double x, double y)
{
_coordinateHistory.Push
(
new Coordinates(x, y);
)
}
//Gets the next coordinate in the history stack.
public Coordinates GetNextCoordinates()
{
return _coordinateHistory.Pop();
}
//Optional method to do all the calculations needed to get distance.
public double CalculateDistance(Coordinates c1, Coordinates c2)
{
//Do your distance calculations here and return the result;
return null;
}
}
//Coordinates object that contains the X, Y, time stamp and optionally, latitude and longitude.
public class Coordinates
{
private double _x;
public double X
{
get { return _x; }
set { _x = value; }
}
private double _y;
public double Y
{
get { return _y; }
set { _y = value; }
}
//Optional
private double _latitude;
public double Latitude
{
get { return _latitude; }
set { _latitude = value; }
}
//Optional
private double _longitude;
public double Longitude
{
get { return _longitude; }
set { _longitude = value; }
}
/*Recommend using a datetime object over a double in order to keep track of timestamps as
it offers precision as well as funcionality. Eg. Time span in ticks between time date time objects */
private DateTime _time;
public DateTime Time
{
get { return _time; }
set { _time = value; }
}
//Base constructor
public Coordinates() { }
//Overloaded constructor that accepts the X and Y coordinates as parameters.
public Coordinates(double x, double y)
{
InitializeCoordinates(x, y, DateTime.Now);
}
//Overloaded constructor that accepts the X and Y coordinates, and a date time object.
public Coordinates(double x, double y, DateTime time)
{
InitializeCoordinates(x, y, time);
}
//Private method that initializes the variables of this coordinate object. This is to reduce unnecessary replicated code.
private void InitializeCoordinates(double x, double y, DateTime time)
{
_x = x;
_y = y;
_time = time;
}
}
public类MainClass
{
私有堆栈_coordinateHistory;//存储坐标历史记录
//初始化坐标历史堆栈的基类。
公共类()
{
_coordinateHistory=新堆栈();
}
//用于创建新的坐标对象并将其添加到坐标历史堆栈中。
公共坐标(双x,双y)
{
_协调历史
(
新坐标(x,y);
)
}
//获取历史堆栈中的下一个坐标。
公共坐标GetNextCoordinates()
{
return _coordinateHistory.Pop();
}
//可选方法,用于执行获取距离所需的所有计算。
公共双计算站(坐标c1,坐标c2)
{
//在此处进行距离计算并返回结果;
返回null;
}
}
//坐标对象,该对象包含X、Y、时间戳以及可选的纬度和经度。
公共类坐标
{
私人双x;
公共双X
{
获取{return\ux;}
设置{ux=value;}
}
私人双人房;
公共双Y
{
获取{return\u y;}
设置{u y=value;}
}
//可选的
私人双纬度;
公共双纬度
{
获取{return\u latitude;}
设置{u纬度=值;}
}
//可选的
私人双经度;
公共双经度
{
获取{return}
设置{u经度=值;}
}
/*建议在double上使用datetime对象,以便随时跟踪时间戳
它提供精确性和功能性。例如,时间对象之间的时间跨度*/
私人日期时间;
公共日期时间
{
获取{return\u time;}
设置{u time=value;}
}
//基本构造函数
公共坐标(){}
//接受X和Y坐标作为参数的重载构造函数。
公共坐标(双x,双y)
{
初始化坐标(x,y,DateTime.Now);
}
//重载构造函数,该构造函数接受X和Y坐标以及日期时间对象。
公共坐标(双x,双y,日期时间)
{
初始化坐标(x,y,时间);
}
//初始化此坐标对象变量的私有方法。这是为了减少不必要的复制代码。
私有无效初始化坐标(双x,双B)