gpt4 book ai didi

来自具有特定值的文本文件的 C# 数组

转载 作者:太空宇宙 更新时间:2023-11-03 23:27:52 24 4
gpt4 key购买 nike

我有一个名为 failas.txt 的文件。它包含立陶宛语文本。我为它编码了 1257,这样它就可以阅读立陶宛语字母了。

现在我所要做的就是为该文件中使用的每个立陶宛字母创建一个数组。

所有这些字母都在字符串 p = "AĄBCČDEĘĖFGHIĮYJKLMNOPRSŠTUŲŪVZŽ";

数组应显示每个字母在文本中重复了多少次,并将这些结果写入名为 rezultatai.txt 的新 txt 文件。所以代码是这样的:

using System;
using System.Linq;
using System.Globalization;
using System.Collections.Generic;
using System.Collections;
using System.IO; skirta biblioteka
using System.Text;
using System.Threading;
class Program
{
static void Main()
{

string failas = "failas.txt";
string rodymas = File.ReadAllText(failas, Encoding.GetEncoding(1257));
Console.OutputEncoding = Encoding.UTF8;
Console.WriteLine(rodymas);
char[] masyvas = rodymas.Where(Char.IsLetter).OrderBy(Char.ToLower).ToArray();
foreach (char c in masyvas)

{
Console.Write(c + ",");
}
string p = "AĄBCČDEĘĖFGHIĮYJKLMNOPRSŠTUŲŪVZŽ";
failas = failas.ToUpper();
Dictionary<char, int> dict = new Dictionary<char, int>();

foreach (char c in p) dict.Add(c, 0);
foreach (char c in failas)
{
int val;
if (dict.TryGetValue(c, out val)) dict[c] = val + 1;
}

//write to a file..
foreach (KeyValuePair<char, int> item in dict)
{
if (item.Value > 0) Console.WriteLine("Character {0}, No of Occurences = {1}", item.Key, item.Value);
File.AppendAllText("rezultatai.txt", item.Value + Environment.NewLine);
}
Console.WriteLine("Sum = {0}", dict.Sum(x => x.Value));
Console.ReadKey();

但是,不知何故,它只返回带有字母 A、F、I、L、S、T 的输出。像这样:

Character A, No of Occurences = 2
Character F, No of Occurences = 1
Character I, No of Occurences = 1
Character L, No of Occurences = 1
Character S, No of Occurences = 1
Character T, No of Occurences = 2

正如我之前提到的,字母应该是:

AĄBCČDEĘĖFGHIĮYJKLMNOPRSŠTUŲŪVZŽ

此外,当我打开 rezultatai.txt 文件检查附加值时,它只包含一长列数字:

2
0
0
0
0
0
0
0
0
1
0
0
1
0
0
0
0
1
0
0
0
0
0
1
0
2
0
0
0
0
0
0
2
0
0
0
0
0
0
0
0
1
0
0
1
0
0
0
0
1
0
0
0
0
0
1
0
2
0

最佳答案

However, somehow it returns only output with letters A, F, I, L, S, T. Like this:

foreach (char c in failas)

您遍历文件名,即“failas.txt”,这应该是实际文件的文本。

foreach (char c in rodymas)
foreach (char c in masyvas) // Possibly the char array.. not sure which..

Also, when I open rezultatai.txt file to check appended values, it only contains a long column of numbers:

是的,您附加了 KeyValuePair 中的值,其中该值是一个整数,这大概需要与您输出到控制台的值相同。

关于来自具有特定值的文本文件的 C# 数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33253873/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com