C# 从控制台读取矩阵(例如 copy/paste 从文本文件)
C# read a matrix from console (for example copy/paste from a textfile)
我想知道从文本文件中以 copy/paste 字符串形式读取直接输入到控制台的矩阵的最佳做法是什么?
这是矩阵的一个例子,但是作为一个整体,最后有新行,行之间没有换行(我不知道如何在这里表示矩阵,抱歉):
5 6 8 12 34
13 4 9 20 3
8 17 2 2 5
最好有一个 int[] 作为结果,它可以用嵌套循环之类的东西写在控制台上。
在此先感谢,对于新手问题(可能)感到抱歉!
class Program
{
static void Main(string[] args)
{
Console.CancelKeyPress += (o, e) =>
{//prevent from stopping after Ctrl+C
e.Cancel = true;
};
Console.WriteLine("\n Enter a matrix. Press Ctrl+C when you will have ended.");
string text = Console.In.ReadToEnd();
Console.WriteLine("\nParsing...\n");
int[,] res2a;
try
{
res2a = parse(text);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message + ",\n " + ex.StackTrace);
return;
}
Console.WriteLine("\nYou enter: \n");
for (int i = 0; i < res2a.GetLength(1); i++)
{
for (int j = 0; j < res2a.GetLength(0); j++)
Console.Write("0x{0:X} ", res2a[j, i]);
Console.WriteLine();
}
}
static int[,] parse(string text)
{
int max_len = 0;
List<List<int>> res2x = new List<List<int>>();
string [] tmp1 = text.Split(new char []{'\n', '\r'}, StringSplitOptions.RemoveEmptyEntries);
foreach(string s1 in tmp1)
{
string [] tmp2 = s1.Split(new char [] {' '}, StringSplitOptions.RemoveEmptyEntries);
res2x.Add(new List<int>(tmp2.ToList().ConvertAll<int>((s)=>int.Parse(s.Trim()))));
if (tmp2.Length > max_len)
max_len = tmp2.Length;
}
int[,] res2a = new int[max_len, tmp1.Length];
for (int j = 0; j < tmp1.Length; j++ )
for (int i = 0; i < res2x[j].Count; i++)
res2a[i, j] = res2x[j][i];
return res2a;
}
}
看起来有些丑陋,但我现在没有时间。也许,我可以稍后改进。
我想知道从文本文件中以 copy/paste 字符串形式读取直接输入到控制台的矩阵的最佳做法是什么?
这是矩阵的一个例子,但是作为一个整体,最后有新行,行之间没有换行(我不知道如何在这里表示矩阵,抱歉):
5 6 8 12 34
13 4 9 20 3
8 17 2 2 5
最好有一个 int[] 作为结果,它可以用嵌套循环之类的东西写在控制台上。
在此先感谢,对于新手问题(可能)感到抱歉!
class Program
{
static void Main(string[] args)
{
Console.CancelKeyPress += (o, e) =>
{//prevent from stopping after Ctrl+C
e.Cancel = true;
};
Console.WriteLine("\n Enter a matrix. Press Ctrl+C when you will have ended.");
string text = Console.In.ReadToEnd();
Console.WriteLine("\nParsing...\n");
int[,] res2a;
try
{
res2a = parse(text);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message + ",\n " + ex.StackTrace);
return;
}
Console.WriteLine("\nYou enter: \n");
for (int i = 0; i < res2a.GetLength(1); i++)
{
for (int j = 0; j < res2a.GetLength(0); j++)
Console.Write("0x{0:X} ", res2a[j, i]);
Console.WriteLine();
}
}
static int[,] parse(string text)
{
int max_len = 0;
List<List<int>> res2x = new List<List<int>>();
string [] tmp1 = text.Split(new char []{'\n', '\r'}, StringSplitOptions.RemoveEmptyEntries);
foreach(string s1 in tmp1)
{
string [] tmp2 = s1.Split(new char [] {' '}, StringSplitOptions.RemoveEmptyEntries);
res2x.Add(new List<int>(tmp2.ToList().ConvertAll<int>((s)=>int.Parse(s.Trim()))));
if (tmp2.Length > max_len)
max_len = tmp2.Length;
}
int[,] res2a = new int[max_len, tmp1.Length];
for (int j = 0; j < tmp1.Length; j++ )
for (int i = 0; i < res2x[j].Count; i++)
res2a[i, j] = res2x[j][i];
return res2a;
}
}
看起来有些丑陋,但我现在没有时间。也许,我可以稍后改进。