此数据库的目标是创建一个exe,允许用户使用.txt文件通过三位数代码键入机场。它以前工作过,但现在似乎没有用。我将突出显示.PolltheData
和.Dataset
周围的错误。我
首先是一个名为Engine的项目,它有两个类:Engine&地点,我没有问题
引擎
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Engine
{
public class Engine
{
public const double EarthRadiusInMiles = 3959.0;
public const double EarthRadiusInKilometers = 6371.0;
public List<Location> Route;
/// <summary>
/// The Engine constructor
/// </summary>
public Engine()
{
Route = new List<Location>();
}
public void Add(Location Loc)
{
Route.Add(Loc);
}
/// <summary>
/// GetRouteLength Calculates the length of all the Location objects in the Route Container
/// </summary>
/// <returns></returns>
public double GetRouteLength()
{
double temp = 0;
for (int i = 0; i < Route.Count; ++i)
{
if (Route.Count == 1)
{
return 0;
}
if (i != this.Route.Count - 1)
{
double view = this.Distance(this.Route[i], this.Route[i + 1], 'K');
temp = temp + this.Distance(this.Route[i], this.Route[i + 1], 'K');
}
}
return temp;
}
/// <summary>
/// Prints out all the elements in the Route.
/// </summary>
public void PrintRoute()
{
if (this.Route.Count != 0)
{
Console.WriteLine("Current Route----- {0} Locations ", this.Route.Count);
foreach (Location Loc in this.Route)
{
Console.WriteLine("{0} Lat, {1} Long,", Loc.GetLatitude(), Loc.GetLongitude());
}
Console.WriteLine("Total Distance = {0} {1}", this.GetRouteLength(), this.Route[0].Unit);
}
else
{
Console.WriteLine("Route is empty");
}
}
/// <summary>
/// Calculates the distance between two location objects
/// </summary>
/// <param name="loc1"></param>
/// <param name="loc2"></param>
/// <param name="unit"></param>
/// <returns></returns>
public double Distance(Location loc1, Location loc2, char unit)
{
double R;
if (unit == 'K')
{
R = EarthRadiusInKilometers;
}
else
{
R = EarthRadiusInMiles;
}
double dLat = deg2rad(loc2.GetLatitude()) - deg2rad(loc1.GetLatitude());
double dLon = deg2rad(loc2.GetLongitude()) - deg2rad(loc1.GetLongitude());
double a = Math.Sin(dLat / 2) * Math.Sin(dLat / 2) + Math.Cos(deg2rad(loc1.GetLatitude())) * Math.Cos(deg2rad(loc2.GetLatitude())) * Math.Sin(dLon / 2) * Math.Sin(dLon / 2);
double c = 2 * Math.Atan2(Math.Sqrt(a), Math.Sqrt(1 - a));
double distance = c * R;
return Math.Round(distance, 2);
}
/// <summary>
/// Calculates the distance between two sets of latitudes and longitudes
/// </summary>
/// <param name="lat1"></param>
/// <param name="lng1"></param>
/// <param name="lat2"></param>
/// <param name="lng2"></param>
/// <param name="unit"></param>
/// <returns></returns>
public double Distance(double lat1, double lng1, double lat2, double lng2, char unit)
{
double R;
if (unit == 'K')
{
R = EarthRadiusInKilometers;
}
else
{
R = EarthRadiusInMiles;
}
double dLat = deg2rad(lat2) - deg2rad(lat1);
double dLon = deg2rad(lng2) - deg2rad(lng1);
double a = Math.Sin(dLat / 2) * Math.Sin(dLat / 2) + Math.Cos(deg2rad(lat1)) * Math.Cos(deg2rad(lat1)) * Math.Sin(dLon / 2) * Math.Sin(dLon / 2);
double c = 2 * Math.Atan2(Math.Sqrt(a), Math.Sqrt(1 - a));
double distance = c * R;
return Math.Round(distance, 2);
}
/// <summary>
/// Calculates the bearing between two location objects
/// </summary>
/// <param name="loc1"></param>
/// <param name="loc2"></param>
/// <param name="lat1"></param>
/// <returns></returns>
public double BearingTo(Location loc1, Location loc2)
{
double dlat1 = deg2rad(loc1.GetLatitude());
double dlat2 = deg2rad(loc2.GetLatitude());
double dLon = deg2rad(loc1.GetLongitude()) - deg2rad(loc2.GetLongitude());
double y = Math.Sin(dLon) * Math.Cos(dlat2);
double x = Math.Cos(dlat1) * Math.Sin(dlat2) - Math.Sin(dlat1) * Math.Cos(dlat2) * Math.Cos(dLon);
double brng = Math.Atan2(y, x);
return (rad2deg(brng) + 360) % 360;
}
/// <summary>
/// Calculates bearing between two sets of latitudes and longitudes
/// </summary>
/// <param name="lat1"></param>
/// <param name="lon1"></param>
/// <param name="lat2"></param>
/// <param name="lon2"></param>
/// <returns></returns>
public double BearingTo(double lat1, double lon1, double lat2, double lon2)
{
double dlat1 = deg2rad(lat1);
double dlat2 = deg2rad(lat2);
double dLon = deg2rad(lon1) - deg2rad(lon2);
double y = Math.Sin(dLon) * Math.Cos(dlat2);
double x = Math.Cos(dlat1) * Math.Sin(dlat2) - Math.Sin(dlat1) * Math.Cos(dlat2) * Math.Cos(dLon);
double brng = Math.Atan2(y, x);
return (rad2deg(brng) + 360) % 360;
}
/// <summary>
/// Calculates a location object based on a location object start, a distance and a bearing
/// </summary>
/// <param name="loc1"></param>
/// <param name="dist"></param>
/// <param name="brng"></param>
/// <param name="unit"></param>
/// <returns></returns>
public Location DestinationPoint(Location loc1, double dist, double brng, char unit)
{
double R;
if (unit == 'K')
{
R = EarthRadiusInKilometers;
}
else
{
R = EarthRadiusInMiles;
}
dist = dist / R;
brng = Math.PI * brng / 180;
double lat1 = deg2rad(loc1.GetLatitude());
double lon1 = deg2rad(loc1.GetLongitude());
double lat2 = Math.Asin(Math.Sin(lat1) * Math.Cos(dist) + Math.Cos(lat1) * Math.Sin(dist) * Math.Cos(brng));
double lon2 = lon1 + Math.Atan2(Math.Sin(brng) * Math.Sin(dist) * Math.Cos(lat1), Math.Cos(dist) - Math.Sin(lat1) * Math.Sin(lat2));
lon2 = (lon2 + 3 * Math.PI) % (2 * Math.PI) - Math.PI;
lat2 = rad2deg(lat2);
lon2 = rad2deg(lon2);
Location temp = new Location(lat2, lon2);
return temp;
}
/// <summary>
/// Converts degrees to radians
/// </summary>
/// <param name="deg"></param>
/// <returns></returns>
private double deg2rad(double deg)
{
return (deg * Math.PI / 180.0);
}
/// <summary>
/// Converts radians to degrees
/// </summary>
/// <param name="rad"></param>
/// <returns></returns>
private double rad2deg(double rad)
{
return (rad / Math.PI * 180.0);
}
}
}
位置
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Diagnostics;
namespace Engine
{
public class Location
{
private double Latitude;
private double Longitude;
private double Elevation;
public char Unit;
public String AirportCode;
public double GetLongitude()
{
return Longitude;
}
public double GetLatitude()
{
return Latitude;
}
public double GetElevation()
{
return Elevation;
}
public void SetLongitude(double value)
{
Debug.Assert(value <= 180);
Debug.Assert(value >= -180);
Longitude = value;
}
public void SetLatitude(double value)
{
Debug.Assert(value <= 90);
Debug.Assert(value >= -90);
Latitude = value;
}
public Location()
{
Latitude = 0;
Longitude = 0;
Elevation = 0;
Unit = 'K';
}
public Location(double Lat, double Long, double Elevate)
{
Debug.Assert(Lat <= 90);
Debug.Assert(Lat >= -90);
Debug.Assert(Long <= 180);
Debug.Assert(Long >= -180);
Latitude = Lat;
Longitude = Long;
Elevation = Elevate;
Unit = 'K';
}
public Location(double Lat, double Long)
{
Debug.Assert(Lat <= 90);
Debug.Assert(Lat >= -90);
Debug.Assert(Long <= 180);
Debug.Assert(Long >= -180);
Latitude = Lat;
Longitude = Long;
Elevation = 0;
Unit = 'K';
}
public Location(String Code, int deglat, int minlat, int seclat, String parrallel, int deglong, int minlong, int seclong, String meridian, double Elevate)
{
//I am giving you this code and hiding it in the new version of this class to make coding the project a bit easier. Time is against us.
// Debug.Assert(meridian.ToUpper() == "E" || meridian.ToUpper() == "W");
// Debug.Assert(parrallel.ToUpper() == "N" || parrallel.ToUpper() == "S");
double Long = Convert.ToDouble(deglong) + (Convert.ToDouble(minlong) / 60.00) + (Convert.ToDouble(seclong) / 60.00);
double Lat = Convert.ToDouble(deglat) + (Convert.ToDouble(minlat) / 60.00) + (Convert.ToDouble(seclat) / 60.00);
if (meridian.ToUpper() == "E")
{
}
else if (meridian.ToUpper() == "W")
{
Long = Long * -1;
}
else
{
Code = "ERR";
}
if (parrallel.ToUpper() == "N")
{
}
else if (parrallel.ToUpper() == "S")
{
Lat = Lat * -1;
}
else
{
AirportCode = "ERR";
}
if (Lat > 90 || Lat < -90 || Long > 180 || Long < -180 || AirportCode == "ERR")
{
Lat = 0;
Long = 0;
Elevate = 0;
Code = "ERR";
}
Debug.Assert(Lat <= 90);
Debug.Assert(Lat >= -90);
Debug.Assert(Long <= 180);
Debug.Assert(Long >= -180);
AirportCode = Code;
Latitude = Lat;
Longitude = Long;
Elevation = Elevate;
Unit = 'K';
}
}
}
在飞行控制台中,我在两个课程中遇到问题。在机场,我在DataSet
this.DataSet.Add(item)
下收到错误消息
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Sql;
using System.Data.SqlClient;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Engine;
using FlightConsole;
namespace FlightConsole
{
public class Airports
{
public Airports()
{
try
{
string currentDirectory = Directory.GetCurrentDirectory();
List<string> list = File.ReadLines(currentDirectory + "\\GlobalAirportDatabase.txt").ToList();
this.DataSet = new List<Location>();
foreach (string item2 in list)
{
string[] array = item2.Split(':');
Location item = new Location(array[1].ToString(), int.Parse(array[5]), int.Parse(array[6]), int.Parse(array[7]), array[8], int.Parse(array[9]), int.Parse(array[10]), int.Parse(array[11]), array[12], double.Parse(array[13]));
this.DataSet.Add(item);
}
}
catch (FileNotFoundException)
{
Console.WriteLine("The file is missing.");
throw new FileNotFoundException();
}
catch (Exception ex2)
{
Console.WriteLine("Something went wrong.");
throw ex2;
}
finally
{
}
}
}
}
在程序中,我PollTheData
在airports.PollTheData(text)
所在的区域内遇到问题。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static System.Console;
using FlightConsole;
using Engine;
namespace FlightConsole
{
internal class Program
{
private static void RefreshConsole(Engine.Engine inputEngine)
{
Console.Clear();
Console.WriteLine("Van Moorsel Flight Computer Version 1.0");
Console.WriteLine("");
if (inputEngine.Route.Count > 0)
{
Console.WriteLine("{0,10}{1,15}{2,10}{3,10}", "Start", "Destination", "Distance", "Bearing");
Console.WriteLine("{0,10}{1,15}{2,10}{3,10}", inputEngine.Route[0].AirportCode, "", "", "");
for (int i = 1; i < inputEngine.Route.Count; i++)
{
Console.WriteLine("{0,10}{1,15}{2,10}{3,10}", inputEngine.Route[i - 1].AirportCode, inputEngine.Route[i].AirportCode, inputEngine.Distance(inputEngine.Route[i - 1], inputEngine.Route[i], 'K'), Math.Round(inputEngine.BearingTo(inputEngine.Route[i], inputEngine.Route[i - 1]), 2));
}
Console.WriteLine("Total Number of Waypoints: {0} Total Distance: {1}", inputEngine.Route.Count, inputEngine.GetRouteLength());
}
else
{
Console.WriteLine("");
}
}
private static void Main(string[] args)
{
bool flag = true;
bool flag2 = true;
try
{
Engine.Engine engine = new Engine.Engine();
Airports airports = new Airports();
while (flag)
{
Program.RefreshConsole(engine);
flag2 = true;
Console.WriteLine("Enter your next destination, or quit pressing Q");
while (flag2)
{
string text = Console.ReadLine();
if (airports.PollTheData(text) != null)
{
engine.Route.Add(airports.PollTheData(text));
flag2 = false;
}
else
{
flag2 = false;
}
if (text.ToUpper() == "Q")
{
flag2 = false;
flag = false;
}
}
}
}
catch (Exception)
{
Console.WriteLine("Whoops something went wrong");
Console.WriteLine("Press any key to end program.");
Console.ReadLine();
flag2 = false;
flag = false;
}
}
}
}
错误状态:
错误CS1061'机场'不包含'DataSet'的定义,并且没有可以找到接受类型'Airports'的第一个参数的扩展方法'DataSet'(您是否缺少using指令或程序集引用?)< / p>
FlightConsole C:\ Users \ Desktop \ COMP6034_ProgrammingBasics \ FinalPlag \ FMS Student \ FlightConsole \ Airports.cs
并且在机场和程序文件中。
答案 0 :(得分:2)
&#34; this&#34; in the line&#34; this.DataSet = new List();&#34;指含有类,即机场。正如您所列出的那样,机场内没有具有该名称的属性或字段 - 您是否意外删除了某些行?