如何在.txt文件中打印出数组的特定值? [英] How to print out specific value of an array in .txt file?

查看:87
本文介绍了如何在.txt文件中打印出数组的特定值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我得到了一个包含2个.txt文件的任务供我使用。我必须找到每个txt文件中的最大值,然后比较这些值并打印出所有这些值中最大的值(如果两个文本文件中的最大值相同,则应打印出这些值)。以下是文本值(代码的名称和部分是立陶宛语。):



注意。例如,值在每行的末尾(

 ORION; 2; 1935; 365,1; 

)值为365,1

所以这是A.txt

Lėksuvėjeliu
5
ORION; 2; 1935年; 365,1;
POLARIS; 1; 1988; 87,09;
INDUSTRIERAD; 10; 1995; 58,10;
鲍尔; 25; 2008; 1040,42;
HERREN; 15; 2012; 1040,42;





这里是B.txt

Neskubėk irbūsipirmas
6
VOLTA; 1; 1955; 290,02;
POLARIS; 2; 1990; 100,13;
BANANA; 3; 1995; 145,06;
INDUSTRIERAD; 10; 2010; 260,94;
ORION; 15; 2012; 1040,42;
POLARIS; 20; 2012; 945,07;





这是我的代码:

使用System; 
使用System.Collections.Generic;
使用System.Linq;
使用System.Text;
使用System.Threading.Tasks;
使用System.IO;

名称空间Sav_3._3
{
class Dviratis
{
private string pav;
private int kiek;
private int metai;
私人双凯纳;

public Dviratis(string pav,int kiek,int metai,double kaina)
{
this.pav = pav;
this.kiek = kiek;
this.metai = metai;
this.kaina = kaina;
}

public string get_pav(){return pav; }

public int get_kiek(){return kiek; }

public int get_metai(){return metai; }

public double get_kaina(){return kaina; }
}
class Calc
{
const int cn = 100;
const string CFd1 =... \\ ... \\A.txt;
const string CFd2 =... \\ ... \\B.txt;
const string CFd3 =... \\ ... \\\\ atd.txt;

static void Main(string [] args)
{
if(File.Exists(CFd3))
File.Delete(CFd3);

Dviratis [] D1 =新的Dviratis [cn];
int n1;
string pav1;

Dviratis [] D2 = new Dviratis [cn];
int n2;
string pav2;

Skaityti(CFd1,D1,out n1,out pav1);
Skaityti(CFd2,D2,out n2,out pav2);

Spausdinti_duomenis(CFd3,D1,n1,pav1);
Spausdinti_duomenis(CFd3,D2,n2,pav2);
Spausdinti_rez(CFd3,D1,D2,n1,n2,pav1,pav2);

}
static void Skaityti(字符串Fd,Dviratis [] D,out int n,out string pav)
{
using(StreamReader reader = new StreamReader( Fd,Encoding.GetEncoding(1257)))
{
string eil; int kiek; int metai;双凯纳;
字符串行;
line = reader.ReadLine();
string [] parts;
pav = line;
line = reader.ReadLine();
n = int.Parse(line);
for(int i = 0; i< n; i ++)
{
line = reader.ReadLine();
parts = line.Split(';');
eil = parts [0];
kiek = int.Parse(parts [1]);
metai = int.Parse(parts [2]);
kaina = double.Parse(parts [3]);
D [i] =新的Dviratis(eil,kiek,metai,kaina);
}
}
}
static void Spausdinti_duomenis(字符串fv,Dviratis [] D,int nkiek,字符串pav)
{
const string virsus =
| ----------------- | ------------ | ------------- - | --------- | \\\\ n
+| Pavadinimas | Kiekis | Pagaminimo | Kaina | \\ n
+| | | metai |(eurų)| \\ n
+| ----------------- | ----------- - | --------------- | --------- |;
using(var fr = File.AppendText(fv))
{
fr.WriteLine(Nuomos firma:{0},pav);
fr.WriteLine(virsus);
Dviratis asd;
for(int i = 0; i< nkiek; i ++)
{
asd = D [i];
fr.WriteLine(| {0,-15} | {1,8} | {2,5:d} | {3,7:F2} |,
asd.get_pav() ,asd.get_kiek(),
asd.get_metai(),asd.get_kaina());
}
fr.WriteLine(------------------------------------ ----------------------);
}
}
static void Spausdinti_rez(字符串fv,Dviratis [] D1,Dviratis [] D2,int n1,int n2,string pav1,string pav2)
{
int brang1 = get_max(D1,n1);
int brang2 = get_max(D2,n2);

使用(var fr = File.AppendText(fv))
{
Dviratis tarp;
for(int i = 0; i< get_maxCount(D1,n1,brang1); i ++)
{
tarp = D1 [get_maxElement(D1,n1,brang1,i)];
fr.WriteLine({0},kainuoja {1,8:F2}eurų,nuomos punktas {2},tarp.get_pav(),tarp.get_kaina(),pav1);
}

for(int i = 0; i< get_maxCount(D2,n2,brang2); i ++)
{
tarp = D2 [get_maxElement(D2) ,n2,brang2,i)];
fr.WriteLine({0},kainuoja {1,8:F2}eurų,nuomos punktas {2},tarp.get_pav(),tarp.get_kaina(),pav2);
}

int k = get_maxGlobal(D1,D2,n1,n2);
if(k< n1)
{
tarp = D1 [k];
}
其他
{
tarp = D2 [k-n1];
}

for(int i = 0; i< k; i ++)
{
fr.WriteLine({0},kainuoja {1,8 :F2}eurų,nuomos punktas {2},tarp.get_pav(),tarp.get_kaina(),(k< n1)?pav1:pav2);
}
}
}

static int get_maxGlobal(Dviratis [] D1,Dviratis [] D2,int n1,int n2)
{
int k = 0;
for(int i = 1; i< n1 + n2; i ++)
{
if((i< n1)?(D1 [i] .get_kaina()>( (k< n1)?D1 [k] .get_kaina():D2 [k-n1] .get_kaina())):/ ** /(D2 [i-n1] .get_kaina()>((k< ; n1)?D1 [k] .get_kaina():D2 [k -n1] .get_kaina()))/ ** /)
{
k = i;
}
}
返回k;
}
static int get_max(Dviratis [] D,int n)
{
int k = 0;
for(int i = 1; i< n; i ++)
if(D [i] .get_kaina()> D [k] .get_kaina())
k = i;

返回k;
}
static int get_maxCount(Dviratis [] D,int n,int k)
{
int c = 0;
for(int i = 0; i< n; i ++)
if(D [i] .get_kaina()== D [k] .get_kaina())
c ++;

返回c;
}
static int get_maxElement(Dviratis [] D,int n,int k,int c)
{
int which = 0;
int ret = 0;
for(int i = k; i< n; i ++)
{
if(D [i] .get_kaina()== D [k] .get_kaina())
{
if(which == c)
{
ret = i;
休息;
}
其中+ = 1;
}
}
返回ret;
}
}
}





所以我找到最大的价值,但问题是我不知道如何打印出其余部分。它只接受第一个值并将其打印到其他值。 Idk如何解释它。



kaina =价值;



我尝试过什么:



我需要快速帮助代码

解决方案

< blockquote>嗨会员,



抱歉,我不会看到那段代码 - 对我来说太可怕了(顺便说一句。为什么不用英文写代码呢?我说德语,但我的代码是英语,框架也是如此..) - 你从C端口输入了吗?也许使用一些框架方法或LINQ?



无论如何,我可以向你展示一个如何实现这一目标的例子。也许你可以找到有用的东西:我创建了一个你可以运行的Console-App。 - 只需在visual studio中创建一个新的控制台应用程序,并用以下代码替换Program.cs(并调整文件路径)





 使用系统; 
使用系统;
使用 System.Collections.Generic;
使用 System.IO;
使用 System.Linq;

命名空间比较
{
class 计划
{
静态 void Main( string [] args)
{
// 从两个文件中读取值
列表< double> listValuesFileA = ProcessFile( TextFileA.txt);
列表< double> listValuesFileB = ProcessFile( TextFileB.txt);

// 从文件中获取最大值
double dMaxFileA = listValuesFileA.Max();
double dMaxFileB = listValuesFileB.Max();

// ...并将它们写入控制台
Console.WriteLine(


最大值文件A:{dMaxFileA} );
Console.WriteLine(


最大值文件B:{ dMaxFileB});

Console.ReadKey( true );
}

// 从文件中读取值并返回一个列表所有值
静态列表< double> ProcessFile( string strFilePath)
{
List< double> listResults = new List< double>();

// 读取文件
string [] astrLines = File.ReadAllLines(strFilePath);

// 通过循环遍历所有行来查找值
foreach string strLine in astrLines)
{
string [] astParts = strLine.Split( ; .ToArray(),StringSplitOptions.RemoveEmptyEntries); // ';'是分隔符号
// 检查我们是否有其中一行带有值(这些行中总是有4个值)
如果(astParts.Length == 4
{
// 仅考虑每行的最后值
string strLastValue = astParts.Last();
// 转换考虑文化的价值!
if double .TryParse(
strLastValue,
System.Globalization.NumberStyles.Any,
System.Globalization.CultureInfo.CreateSpecificCulture( lt-LT), // 立陶宛文化
out double dResult))
{
listResults.Add(dResult);
};
}
}

return listResults;
}
}
}





如果您有任何疑问,请随时询问?



亲切的问候Johannes


So i got a task which has 2 .txt files for me to use. I have to find the biggest value in each of those txt files and then after compare those values and print out the biggest out of all of them(if the biggest values are the same in both text files it should print out those values). Here are the text values(the name and parts of the code are in lithuanian.):

Note.The values are at the end of each line for example(

ORION; 2; 1935; 365,1;

) the value is 365,1
So this is "A.txt"

Lėk su vėjeliu
5
ORION; 2; 1935; 365,1;
POLARIS; 1; 1988; 87,09;
INDUSTRIERAD; 10; 1995; 58,10;
Bauer; 25; 2008; 1040,42;
HERREN; 15; 2012; 1040,42;



And here is "B.txt"

Neskubėk ir būsi pirmas
6
VOLTA; 1; 1955; 290,02;
POLARIS; 2; 1990; 100,13;
BANANA; 3; 1995; 145,06;
INDUSTRIERAD; 10; 2010; 260,94;
ORION; 15; 2012; 1040,42;
POLARIS; 20; 2012; 945,07;



And here is my code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;

namespace Sav_3._3
{
    class Dviratis
    {
        private string pav;
        private int kiek;
        private int metai;
        private double kaina;

        public Dviratis(string pav, int kiek, int metai, double kaina)
        {
            this.pav = pav;
            this.kiek = kiek;
            this.metai = metai;
            this.kaina = kaina;
        }

        public string get_pav() { return pav; }

        public int get_kiek() { return kiek; }

        public int get_metai() { return metai; }

        public double get_kaina() { return kaina; }
    }
    class Calc
    {
        const int cn = 100;
        const string CFd1 = "...\\...\\A.txt";
        const string CFd2 = "...\\...\\B.txt";
        const string CFd3 = "...\\...\\asd.txt";

        static void Main(string[] args)
        {
            if (File.Exists(CFd3))
                File.Delete(CFd3);

            Dviratis[] D1 = new Dviratis[cn];
            int n1;
            string pav1;

            Dviratis[] D2 = new Dviratis[cn];
            int n2;
            string pav2;

            Skaityti(CFd1, D1, out n1, out pav1);
            Skaityti(CFd2, D2, out n2, out pav2);

            Spausdinti_duomenis(CFd3, D1, n1, pav1);
            Spausdinti_duomenis(CFd3, D2, n2, pav2);
            Spausdinti_rez(CFd3,D1,D2,n1,n2,pav1,pav2);

        }
        static void Skaityti(string Fd, Dviratis[] D, out int n, out string pav)
        {
            using (StreamReader reader = new StreamReader(Fd,Encoding.GetEncoding(1257)))
            {
                string eil; int kiek; int metai; double kaina;
                string line;
                line = reader.ReadLine();
                string[] parts;
                pav = line;
                line = reader.ReadLine();
                n = int.Parse(line);
                for (int i = 0; i < n; i++)
                {
                    line = reader.ReadLine();
                    parts = line.Split(';');
                    eil = parts[0];
                    kiek = int.Parse(parts[1]);
                    metai = int.Parse(parts[2]);
                    kaina = double.Parse(parts[3]);
                    D[i] = new Dviratis(eil, kiek, metai, kaina);
                }
            }
        }
        static void Spausdinti_duomenis(string fv, Dviratis[] D, int nkiek, string pav)
        {
            const string virsus =
            "|-----------------|------------|---------------|---------|\r\n"
          + "|   Pavadinimas   |   Kiekis   |   Pagaminimo  |  Kaina  | \r\n"
          + "|                 |            |      metai    | (eurų)  | \r\n"
          + "|-----------------|------------|---------------|---------|";
            using (var fr = File.AppendText(fv))
            {
                fr.WriteLine("Nuomos firma: {0}", pav);
                fr.WriteLine(virsus);
                Dviratis asd;
                for (int i = 0; i < nkiek; i++)
                {
                   asd = D[i];
                    fr.WriteLine("| {0,-15} |  {1,8}  |     {2,5:d}     | {3,7:F2} |",
                     asd.get_pav(), asd.get_kiek(),
                     asd.get_metai(),asd.get_kaina());
                }
                fr.WriteLine("----------------------------------------------------------");
            }
        }
        static void Spausdinti_rez(string fv, Dviratis[] D1, Dviratis[] D2, int n1, int n2, string pav1, string pav2)
        {
            int brang1 = get_max(D1, n1);
            int brang2 = get_max(D2, n2);

            using (var fr = File.AppendText(fv))
            {
                Dviratis tarp;
                for (int i = 0; i < get_maxCount(D1, n1, brang1); i++)
                {
                    tarp = D1[get_maxElement(D1, n1, brang1, i)];
                    fr.WriteLine(" {0}, kainuoja {1,8:F2} eurų, nuomos punktas {2}", tarp.get_pav(), tarp.get_kaina(), pav1);
                }

                for (int i = 0; i < get_maxCount(D2, n2, brang2); i++)
                {
                    tarp = D2[get_maxElement(D2, n2, brang2, i)];
                    fr.WriteLine(" {0}, kainuoja {1,8:F2} eurų, nuomos punktas {2}", tarp.get_pav(), tarp.get_kaina(), pav2);
                }

                int k = get_maxGlobal(D1, D2, n1, n2);
                if (k < n1)
                {
                    tarp = D1[k];
                }
                else
                {
                    tarp = D2[k-n1];
                }
          
                for (int i = 0; i < k; i++)
                {
                    fr.WriteLine(" {0}, kainuoja {1,8:F2} eurų, nuomos punktas {2}", tarp.get_pav(), tarp.get_kaina(), (k < n1) ? pav1 : pav2);
                }
            }
        }

        static int get_maxGlobal(Dviratis[] D1, Dviratis[] D2, int n1, int n2)
        {
            int k = 0;
            for (int i = 1; i < n1 + n2; i++)
            {
                if ((i < n1) ? (D1[i].get_kaina() > ((k < n1) ? D1[k].get_kaina() : D2[k - n1].get_kaina())) : /**/ (D2[i - n1].get_kaina() > ((k < n1) ? D1[k].get_kaina() : D2[k - n1].get_kaina())) /**/)
                {
                    k = i;
                }
            }
            return k;
        }
        static int get_max(Dviratis[] D, int n)
        {
            int k = 0;
            for (int i = 1; i < n; i++)
                if (D[i].get_kaina() > D[k].get_kaina())
                    k = i;

            return k;
        }
        static int get_maxCount(Dviratis[] D, int n, int k)
        {
            int c = 0;
            for (int i = 0; i < n; i++)
                if (D[i].get_kaina() == D[k].get_kaina())
                    c++;

            return c;
        }
        static int get_maxElement(Dviratis[] D, int n, int k, int c)
        {
            int which=0;
            int ret=0;
            for (int i = k; i < n; i++)
            {
                if (D[i].get_kaina() == D[k].get_kaina())
                {
                    if (which == c)
                    {
                        ret = i;
                        break;
                    }
                    which +=1;
                }
            }
            return ret;
        }
    }
}



So i find the biggest values, but the problem is i don't know how to print out the rest. It only takes the first value and prints it out to other values. Idk how to explain it.

kaina = the value;

What I have tried:

I need fast help with the code

解决方案

Hi member,

Sorry but I won't read that code - Looks horrible to me (btw. why not write code always in english? I speak german, but my code is english and so is the framework..) - And did you port that from C? Maybe use some of the Framework Methods or LINQ?

Anyway I can show you an example how I would aproach this. Maybe you can find something useful: I created an Console-App you can run. - just create a new console application in visual studio and replace Program.cs with the following (and adjust the filepaths)


using System;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;

namespace Compare
{
    class Program
    {
        static void Main(string[] args)
        {
            // read values from both files
            List<double> listValuesFileA = ProcessFile("TextFileA.txt");
            List<double> listValuesFileB = ProcessFile("TextFileB.txt");

            // get max value from files
            double dMaxFileA = listValuesFileA.Max();
            double dMaxFileB = listValuesFileB.Max();

            // ... and write them to the console            
            Console.WriteLine(


"Max Value File A: {dMaxFileA}"); Console.WriteLine(


"Max Value File B: {dMaxFileB}"); Console.ReadKey(true); } // reads the values from the file and returns a list of all values static List<double> ProcessFile(string strFilePath) { List<double> listResults = new List<double>(); // read the file string[] astrLines = File.ReadAllLines(strFilePath); // find the values by looping through all lines foreach (string strLine in astrLines) { string[] astParts = strLine.Split(";".ToArray(), StringSplitOptions.RemoveEmptyEntries); // ';' is the separator-sign // check if we got one of the lines with the values (always 4 values in such lines) if (astParts.Length == 4) { // only consider the last values of each line string strLastValue = astParts.Last(); // convert the value considering culture! if (double.TryParse( strLastValue, System.Globalization.NumberStyles.Any, System.Globalization.CultureInfo.CreateSpecificCulture("lt-LT"), // Lithuanian culture out double dResult)) { listResults.Add(dResult); }; } } return listResults; } } }



Feel free to ask if you have any questions?

kind regards Johannes


这篇关于如何在.txt文件中打印出数组的特定值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆