HyperGraph
Details
HyperGraph/Matrix.cs 60(+58 -2)
diff --git a/HyperGraph/Matrix.cs b/HyperGraph/Matrix.cs
index 444756f..51065fa 100644
--- a/HyperGraph/Matrix.cs
+++ b/HyperGraph/Matrix.cs
@@ -108,7 +108,7 @@ namespace HyperGraph
{
try
{
- matrix[posRow][posColumn] = value;
+ this.matrix[posRow][posColumn] = value;
}
catch (Exception e)
{
@@ -160,9 +160,65 @@ namespace HyperGraph
}
catch (Exception e)
{
- Console.WriteLine(e.Message);
+
+ }
+ }
+ }
+
+ static t ConvertValue<t, U>(U value) where U : IConvertible
+ {
+ return (t)Convert.ChangeType(value, typeof(t));
+ }
+
+ public static Matrix<T> LoadFromStream(System.IO.StreamReader input, int countR0w, int countCol)
+ {
+ Matrix<T> matrix = new Matrix<T>(countR0w, countCol);
+ string r0w;
+ try
+ {
+ if (input == null || input.EndOfStream)
+ throw new Exception();
+
+ for (int i = 0; i < countR0w; i++)
+ {
+ r0w = input.ReadLine();
+ for (int j = 0; j < countCol; j++)
+ {
+ string temp = r0w.Split(' ')[j];
+ T tmp = ConvertValue<T,string>(temp);
+ matrix.matrix[i][j] = tmp;
+ }
}
}
+ catch (Exception e)
+ {
+ Console.WriteLine(e.Message);
+ }
+
+ return matrix;
+ }
+
+ public static bool UnloadToStream(Matrix<T> matrix, System.IO.StreamWriter output)
+ {
+ try
+ {
+ if (output == null)
+ throw new Exception();
+ for (int i = 0; i < matrix.countRow; i++)
+ {
+ for (int j = 0; j < matrix.countColumn; j++)
+ {
+ output.Write(matrix[i][j] + " ");
+ }
+ output.WriteLine();
+ }
+ }
+ catch (Exception e)
+ {
+ Console.WriteLine(e.Message);
+ return false;
+ }
+ return true;
}
}
}
\ No newline at end of file
HyperGraph/Program.cs 10(+9 -1)
diff --git a/HyperGraph/Program.cs b/HyperGraph/Program.cs
index 447c23f..76b6fee 100644
--- a/HyperGraph/Program.cs
+++ b/HyperGraph/Program.cs
@@ -16,8 +16,16 @@ namespace HyperGraph
List<int> ex3 = new List<int>(); ex3.Add(4); ex3.Add(5); ex3.Add(6); ex3.Add(7); ex3.Add(8);
m[3] = ex3;
- List<int> ex4 = m[3];
+ List<int> ex4 = m[3];
+
+ System.IO.StreamReader input = new System.IO.StreamReader("D:\\input.txt");
+ System.IO.StreamWriter output = new System.IO.StreamWriter("D:\\output.txt");
+
+ Matrix<int> matrix = Matrix<int>.LoadFromStream(input, 6, 7);
+ Matrix<int>.UnloadToStream(matrix, output);
+
+ input.Close(); output.Close();
Console.ReadKey();
}
}