Click here to Skip to main content
15,878,945 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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
Posted
Updated 10-Oct-18 2:15am
v2

1 solution

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)


C#
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
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900