public static Path3D LoadPath(string pathToFile) { Path3D path = new Path3D(); using (StreamReader sr = new StreamReader(pathToFile)) { string readText = File.ReadAllText(pathToFile); string pattern = "([a-zA-Z]+) [(]X = ([0-9.]*[0-9]+), Y = ([0-9.]*[0-9]+), Z = ([0-9.]*[0-9]+)[)]"; var regex = new Regex(pattern); var matches = regex.Matches(readText); if (matches.Count <= 0) { throw new ApplicationException("Invalid data in file " + pathToFile); } foreach (Match match in matches) { string str = match.Groups[1].Value; double x = double.Parse(match.Groups[2].Value); double y = double.Parse(match.Groups[3].Value); double z = double.Parse(match.Groups[4].Value); Point3D p = new Point3D(str, x, y, z); path.AddPoint(p); } } return(path); }
public static void SavePath(string pathToFile, Path3D path) { if (!File.Exists(pathToFile)) { using (StreamWriter sw = File.CreateText(pathToFile)) { sw.Write(path); } } else { File.AppendAllText(pathToFile, path.ToString()); } }
static void Main(string[] args) { Point3D pointA = new Point3D("A", 6, 5, 8); Point3D pointB = new Point3D("B", 1.5, 8, 2.3); Point3D pointC = new Point3D("C", 9, 3, 7.5); Point3D pointD = new Point3D("D", 3, 1, 4.5); Console.WriteLine(Point3D.StartingPoint.ToString()); Console.WriteLine(); Console.WriteLine(pointA.ToString()); Console.WriteLine(); Console.WriteLine(pointB.ToString()); Console.WriteLine(); Console.WriteLine(pointC.ToString()); Console.WriteLine(); Console.WriteLine(pointD.ToString()); Console.WriteLine(); Console.WriteLine("The Distance between point A and point B is: " + DistanceCalculator.CalculateTheDistance(pointA, pointB)); Console.WriteLine(); string pathToFile = @"D:\Documents\Software University\2nd level\OOP\02.OOP-Static-Members-and-Namespaces-Homework\trunk\Problem01Point3D\Problem01Point3D\Path3D.txt"; Path3D path = new Path3D(pointA, pointB, Point3D.StartingPoint); Console.WriteLine("Save path: {0}", path); Storage.SavePath(pathToFile, path); Console.WriteLine(); Path3D path2 = new Path3D(pointA, pointB, pointC, pointD); Console.WriteLine("Save path: {0}", path2); Storage.SavePath(pathToFile, path2); Console.WriteLine(); Path3D path3 = new Path3D(pointC, pointB, pointD); Console.WriteLine("Save path: {0}", path3); Storage.SavePath(pathToFile, path3); Console.WriteLine(); Path3D loadPath = Storage.LoadPath(pathToFile); Console.WriteLine("Load path: {0}", loadPath); }