M
Mary Mikhail
Guest
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
namespace Map_Routing
{
/* public struct vertex
{
public int vertex_number;
private int x;
private int y;
private List<edge> edge_list;
public vertex(int v, int p1, int p2,List<edge> l)
{
this.vertex_number = v;
this.x = p1;
this.y = p2;
this.edge_list = l;
}
//because the struct must be immutable
public int Field1 { ge t { return vertex_number; } }
public int Field2 { get { return x; } }
public int Field3 { get { return y; } }
public List<edge> Field4 { get { return edge_list; } }
};
public struct edge
{
// edge between vertex v1 and vertex v2
private int v1;
private int v2;
private float road_len;
private int road_speed;
public edge(int var1 , int var2 , float var3, int var4)
{
v1 = var1;
v2 = var2;
road_len = var3;
road_speed = var4;
}
public int F1 { get { return v1; } }
public int F2 { get { return v2; } }
public float F3 { get { return road_len; } }
public int F4 { get { return road_speed; } }
};*/
class vertex
{
public int vertex_number;
public float x;
public float y;
public List<edge> edge_list;
/* public vertex(int v, float p1, float p2, List<edge> l)
{
vertex_number = v;
x = p1;
y = p2;
edge_list = l;
}*/
}
class edge
{
// edge between vertex v1 and vertex v2
public int v1;
public int v2;
public float road_len;
public int road_speed;
/* public edge(int var1, int var2, float var3, int var4)
{
v1 = var1;
v2 = var2;
road_len = var3;
road_speed = var4;
}*/
}
class Program
{
static void Main(string[] args)
{
List<vertex> graph = new List<vertex>();
StreamReader sr = new StreamReader("../../map1.txt");
// read total number of vertex only
string total_numOf_Vertex = sr.ReadLine();
// skip the first line which contain the total number of the vertex
string line = File.ReadLines("../../map1.txt").Skip(1).Take(1).First();
for (int i = 0; i < int.Parse(total_numOf_Vertex); i++)
{
//read line
line = sr.ReadLine();
//split the line at the delemiter (' ') into 3 parts vertex number , x any y and put each of them in
//the array values where vertex number at index zero , x at index 1 and so on ...
string[] values = line.Split(' ');
//adding vertex
vertex v = new vertex();
graph.Add(v);
//putting the right value at its corresponding place in the graph
graph.vertex_number = int.Parse(values[0]); // it gives an exception here " index out of range"
graph.x = float.Parse(values[1]);
graph.y = float.Parse(values[2]);
}
string total_numOf_Edges = File.ReadLines("../../map1.txt").Skip(7).Take(1).First(); ;
string line1 = File.ReadLines("../../map1.txt").Skip(1).Take(1).First();
for(int i =0; i< int.Parse(total_numOf_Vertex); i++)
{
for(int j=0; j< int.Parse(total_numOf_Edges);j++)
{
line1 = sr.ReadLine();
string[] values1 = line1.Split(' ');
edge e = new edge(); // it give me an error here saying NullReferenceException was unhandled,
graph.edge_list.Add(e); //Object Reference not set to an instance of an object
graph.edge_list[j].v1 = int.Parse(values1[0]);
graph.edge_list[j].v2 = int.Parse(values1[1]);
graph.edge_list[j].road_len = float.Parse(values1[2]);
graph.edge_list[j].road_speed = int.Parse(values1[3]);
}
}
for (int i = 0; i < int.Parse(total_numOf_Vertex); i++)
{
Console.WriteLine("for vertex number");
Console.WriteLine(graph.vertex_number);
Console.WriteLine("the edges that are connected to this vertex are");
for (int j = 0; j < int.Parse(total_numOf_Edges); j++)
{
Console.WriteLine(graph.edge_list[j].v1);
Console.WriteLine(graph.edge_list[j].v2);
Console.WriteLine(graph.edge_list[j].road_len);
Console.WriteLine(graph.edge_list[j].road_speed);
Console.WriteLine(Environment.NewLine);
}
}
}
}
}
Continue reading...
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
namespace Map_Routing
{
/* public struct vertex
{
public int vertex_number;
private int x;
private int y;
private List<edge> edge_list;
public vertex(int v, int p1, int p2,List<edge> l)
{
this.vertex_number = v;
this.x = p1;
this.y = p2;
this.edge_list = l;
}
//because the struct must be immutable
public int Field1 { ge t { return vertex_number; } }
public int Field2 { get { return x; } }
public int Field3 { get { return y; } }
public List<edge> Field4 { get { return edge_list; } }
};
public struct edge
{
// edge between vertex v1 and vertex v2
private int v1;
private int v2;
private float road_len;
private int road_speed;
public edge(int var1 , int var2 , float var3, int var4)
{
v1 = var1;
v2 = var2;
road_len = var3;
road_speed = var4;
}
public int F1 { get { return v1; } }
public int F2 { get { return v2; } }
public float F3 { get { return road_len; } }
public int F4 { get { return road_speed; } }
};*/
class vertex
{
public int vertex_number;
public float x;
public float y;
public List<edge> edge_list;
/* public vertex(int v, float p1, float p2, List<edge> l)
{
vertex_number = v;
x = p1;
y = p2;
edge_list = l;
}*/
}
class edge
{
// edge between vertex v1 and vertex v2
public int v1;
public int v2;
public float road_len;
public int road_speed;
/* public edge(int var1, int var2, float var3, int var4)
{
v1 = var1;
v2 = var2;
road_len = var3;
road_speed = var4;
}*/
}
class Program
{
static void Main(string[] args)
{
List<vertex> graph = new List<vertex>();
StreamReader sr = new StreamReader("../../map1.txt");
// read total number of vertex only
string total_numOf_Vertex = sr.ReadLine();
// skip the first line which contain the total number of the vertex
string line = File.ReadLines("../../map1.txt").Skip(1).Take(1).First();
for (int i = 0; i < int.Parse(total_numOf_Vertex); i++)
{
//read line
line = sr.ReadLine();
//split the line at the delemiter (' ') into 3 parts vertex number , x any y and put each of them in
//the array values where vertex number at index zero , x at index 1 and so on ...
string[] values = line.Split(' ');
//adding vertex
vertex v = new vertex();
graph.Add(v);
//putting the right value at its corresponding place in the graph
graph.vertex_number = int.Parse(values[0]); // it gives an exception here " index out of range"
graph.x = float.Parse(values[1]);
graph.y = float.Parse(values[2]);
}
string total_numOf_Edges = File.ReadLines("../../map1.txt").Skip(7).Take(1).First(); ;
string line1 = File.ReadLines("../../map1.txt").Skip(1).Take(1).First();
for(int i =0; i< int.Parse(total_numOf_Vertex); i++)
{
for(int j=0; j< int.Parse(total_numOf_Edges);j++)
{
line1 = sr.ReadLine();
string[] values1 = line1.Split(' ');
edge e = new edge(); // it give me an error here saying NullReferenceException was unhandled,
graph.edge_list.Add(e); //Object Reference not set to an instance of an object
graph.edge_list[j].v1 = int.Parse(values1[0]);
graph.edge_list[j].v2 = int.Parse(values1[1]);
graph.edge_list[j].road_len = float.Parse(values1[2]);
graph.edge_list[j].road_speed = int.Parse(values1[3]);
}
}
for (int i = 0; i < int.Parse(total_numOf_Vertex); i++)
{
Console.WriteLine("for vertex number");
Console.WriteLine(graph.vertex_number);
Console.WriteLine("the edges that are connected to this vertex are");
for (int j = 0; j < int.Parse(total_numOf_Edges); j++)
{
Console.WriteLine(graph.edge_list[j].v1);
Console.WriteLine(graph.edge_list[j].v2);
Console.WriteLine(graph.edge_list[j].road_len);
Console.WriteLine(graph.edge_list[j].road_speed);
Console.WriteLine(Environment.NewLine);
}
}
}
}
}
Continue reading...