0

我有一个包含以下文本的文件 gg.txt:

L = 4
H = 5
T = E
 #  ##   ## ##  ### ###  ## # # ###  ## # # #   # # ###  #  ##   #  ##   ## ### # # # # # # # # # # ### ### 
# # # # #   # # #   #   #   # #  #    # # # #   ### # # # # # # # # # # #    #  # # # # # # # # # #   #   # 
### ##  #   # # ##  ##  # # ###  #    # ##  #   ### # # # # ##  # # ##   #   #  # # # # ###  #   #   #   ## 
# # # # #   # # #   #   # # # #  #  # # # # #   # # # # # # #    ## # #   #  #  # # # # ### # #  #  #       
# # ##   ## ##  ### #    ## # # ###  #  # # ### # # # #  #  #     # # # ##   #  ###  #  # # # #  #  ###  #  

如您所见,它是一个字母表。L 为字符长度;H是字符高度。(它们将始终保持 4 和 5。) T 是我要打印的字符。因此,如果 T=E,我只需要在控制台中打印 E 的图像。

### 
#   
##  
#   
### 

如果我将 txt 中的 T 更改为 B,程序将打印 B。我不知道如何执行此操作或开始,这是我的起始代码:

class Program
{
    static void Main(string[] args)
    {
        string[] fileContent = File.ReadAllLines("gg.txt");

        Console.WriteLine(string.Join("", fileContent));
        Console.ReadKey();
    }
}
4

3 回答 3

1

请注意,上面的代码根本没有错误检查。

using (var sr = new StreamReader(@"c:\gg.txt"))
{
    var l = int.Parse(sr.ReadLine().Split('=')[1]);
    var h = int.Parse(sr.ReadLine().Split('=')[1]);
    var t = sr.ReadLine().Split('=')[1].Trim()[0];
    int pos = t - 'A';

    for (int i = 0; i < h; i++)
    {
        var line = sr.ReadLine();
        Console.WriteLine(line.Substring(l*pos,l));
    }
}

在此处查看完整代码:https ://dotnetfiddle.net/FuM5ci

于 2018-02-19T12:34:08.917 回答
0

如果我理解正确,您只想保留给定字符的子字符串部分,因此如果 T 是 char 并且文件中除了您显示的内容之外没有其他内容:

int charIndex = (int)T - (int)'A';
int startPos = charIndex * L;

foreach (string line in File.ReadLines("gg.txt"))
{
    Console.WriteLine(line.Substring(startPos, L));
}

请注意,您可能希望在执行此操作之前验证 T 和 L。

于 2018-02-19T12:23:04.900 回答
0

基于之前的解决方案,我设法扩展到多个字母(不是最优雅的方式,但它有效)

在此处输入图像描述

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json;
using System.Net;
using System.IO;
using System.Threading;
using System.Globalization;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            string line;
            int rowCount = 0;


            using (StreamReader sr = new StreamReader(@"gg.txt"))
            {
                var charLenght = int.Parse(sr.ReadLine().Split('=')[1]);
                var charHeight = int.Parse(sr.ReadLine().Split('=')[1]);
                var whatToPrint = sr.ReadLine().Split('=')[1].Trim();
                //foreach (var letter in whatToPrint)
                for (int i = 0; i < whatToPrint.Length; i++)

                {
                    int pos = whatToPrint[i] - 'A';

                    for (int j = 0; j < charHeight; j++)
                    {

                        line = sr.ReadLine();
                        Console.SetCursorPosition(charLenght * i, j);
                        int left = Console.CursorLeft, top = Console.CursorTop; left = left; top = top;
                        if (whatToPrint[i] == (char)32)
                            break;

                        else
                            Console.WriteLine(line.Substring(charLenght * pos, charLenght)); 


                    }
                    // back to start of the text file and skip the first 3 rows
                    sr.DiscardBufferedData();
                    sr.BaseStream.Seek(0, SeekOrigin.Begin);


                    for (int l = 0; l < 3; l++)
                    {
                        sr.ReadLine();
                    }

                }
            }

            Console.ReadLine();

        }
    }
于 2018-02-19T13:07:19.273 回答