Почему не выводит матрицу на экран? - C#
Формулировка задачи:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
namespace matr
{
class Program
{
static bool GenerateFile(string NameFile, int stroka, int stolbec, int minRange, int maxRange)
{
StreamWriter proga = new StreamWriter(NameFile);
if (proga == null)
return false;
else
{
proga.WriteLine(stroka); //это мы в файл на первую и вторую строку записываем количество строк и количество столбцов
proga.WriteLine(stolbec);
Random Rnd = new Random();
if (minRange > maxRange)
{
int k = minRange;
minRange = maxRange;
maxRange = k;
}
for (int i = 0; i < stroka; i++)
{
for (int j = 0; j < stolbec; j++)
{
proga.Write(Rnd.Next(minRange, maxRange));
proga.Write(' ');
}
proga.WriteLine("\n");
}
proga.Close();
return true;
}
}
static int[,] CheckTheFile(string NameFile, out int Err)
{
StreamReader proga = new StreamReader(NameFile);
int cod = 0;
if (((cod = proga.Read()) == (-1)))
{
Err = 2;
}
proga.Close();
proga = new StreamReader(NameFile);
int stolbec = 0;
int stroka = 0;
string L;
if ((L = proga.ReadLine()) != null)
{
stroka = Convert.ToInt32(L);
stolbec = Convert.ToInt32(L);
}
int[,] Mas = new int[stroka, stolbec];
Err = 0;
if (proga == null)
Err = 1;
else
{
string str = null;
int chisla;
int i = 0;
int j = 0;
bool f = true;
for (; j < stolbec; j++)
{
for (; i < stroka; i++)
{
while ((cod = proga.Read()) != -1)
{
if (cod > 47 && cod < 58)
{
char simbol = Convert.ToChar(cod);
str = str + simbol;
f = true;
}
else
{
if (cod == 32)
{
if (f == false)
{
break;
}
chisla = Convert.ToInt32(str);
Mas[i, j] = chisla;
i++;
Err = 0;
str = null;
chisla = 0;
f = false;
}
else
{
Err = 2;
break;
}
}
}
}
}
proga.Close();
}
return Mas;
}
static void Main(string[] args)
{
string S = ("C:\\proga.txt");
int Err;
int stroka, stolbec, minRange, maxRange;
Console.WriteLine("Vvedite parametrs");
string Str;
Str = Console.ReadLine();
stroka = Convert.ToInt32(Str);
Console.Write("\n");
Str = Console.ReadLine();
stolbec = Convert.ToInt32(Str);
Console.Write("\n");
Str = Console.ReadLine();
minRange = Convert.ToInt32(Str);
Console.Write("\n");
Str = Console.ReadLine();
maxRange = Convert.ToInt32(Str);
Console.Write("\n");
if ((stolbec <= 0) || (stroka <= 0))
Console.WriteLine("Nevern parameters");
else
{
bool f = GenerateFile(S, stolbec, stroka, minRange, maxRange);
if (f == true)
{
Console.WriteLine("Open file");
int[,] Mas = CheckTheFile(S, out Err);
if (Err == 0)
{
for (int i = 0; i < stroka; i++)
{
for (int j = 0; j < stolbec; j++)
{
Console.Write("{0,3},", Mas[i, j]);
}
Console.WriteLine("\n");
}
}
}
}
}
}
}Решение задачи: «Почему не выводит матрицу на экран?»
textual
Листинг программы
using System;
using System.IO;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string pathToFile = "C:\\proga.txt";
int rows, columns, minRange, maxRange;
Console.WriteLine("Input parameters in line (rows columns minRange maxRange):");
var pString = Console.ReadLine().Split();
rows = int.Parse(pString[0]);
columns = int.Parse(pString[1]);
minRange = int.Parse(pString[2]);
maxRange = int.Parse(pString[3]);
int[,] matrix = new int[rows, columns];
FillRandom(matrix, minRange, maxRange);
WriteToFile(matrix, pathToFile);
int[,] matrixReconstructed = ReadFromFile(pathToFile);
Console.WriteLine(MatrixToString(matrixReconstructed));
}
static void FillRandom(int[,] matrix, int min, int max)
{
Random r = new Random();
for (int i = 0; i < matrix.GetLength(0); i++)
for (int j = 0; j < matrix.GetLength(1); j++)
matrix[i, j] = r.Next(min, max);
}
static string MatrixToString(int[,] matrix)
{
StringBuilder sb = new StringBuilder();
sb.AppendLine(matrix.GetLength(0) + " " + matrix.GetLength(1));
for (int i = 0; i < matrix.GetLength(0); i++)
{
for (int j = 0; j < matrix.GetLength(1); j++)
sb.Append(matrix[i, j] + " ");
sb.AppendLine();
}
return sb.ToString();
}
static void WriteToFile(int[,] matrix, string pathToFile)
{
using (StreamWriter sWriter = new StreamWriter(pathToFile))
{
sWriter.Write(MatrixToString(matrix));
}
}
static int[,] ReadFromFile(string pathToFile)
{
int[,] matrix;
int rows, columns;
using (StreamReader sReader = new StreamReader(pathToFile))
{
var dimensions = sReader.ReadLine().Split();
rows = int.Parse(dimensions[0]);
columns = int.Parse(dimensions[1]);
matrix = new int[rows, columns];
for (int i = 0; i < rows; i++)
{
var line = sReader.ReadLine().Split();
for (int j = 0; j < columns; j++)
{
matrix[i, j] = int.Parse(line[j]);
}
}
}
return matrix;
}
}
}