简体   繁体   English

如何计算所列项目?

[英]How to count the listed items?

I have a txt file with infos about some mobile phones. 我有一个txt文件,其中包含有关某些手机的信息。

Type;Size;Color;New-OrUsed;Warrantee(month);Price(HUF) 

iPhone SE;64;Gold;New;0;95000
iPhone 6S;64;Silver;New;3;130000
iPhone 5S;16;Black;New;5;60000
iPhone 5S;32;Gold;New;6;75000
iPhone 5S;32;RoseGold;New;8;66500
iPhone 7;32;Black;Used;10;135000
iPhone X;256;Silver;New;12;400000
iPhone 6S;128;Silver;New;3;173000
iPhone 8;128;Gold;New;12;256000
iPhone 7;64;Red;Used;4;155000
iPhone 8 Plus;64;Silver;New;4;285000
iPhone 6S Plus;64;Black;Used;8;180000
iPhone 7 Plus;32;Red;Used;6;192000

I would like to list all of them, like below: 我想列出所有这些,如下所示:

Type (Only once, whatever how many of them);How many does the text have of this type; 类型(无论有多少,都只有一次);这种类型的文本有多少; how many color does the text have of this type 这种类型的文字有多少种颜色

iPhone 5S;3;3

I could list the types with a hashset, but I have no idea how to count the different colors, and the number of the type. 我可以用哈希集列出类型,但是我不知道如何计算不同的颜色以及类型的数量。

My code: 我的代码:

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

namespace nyilvantartas
{
    class Program
    {
        struct adatok
        {
            public string tipus, szin, allapot;
            public int meret, garancia, ar;
        }

        static void Main(string[] args)
        {
            StreamReader f = new StreamReader("termekek.txt", Encoding.Default);
            HashSet<string> tipusok = new HashSet<string>();
            List<int> hanyvan = new List<int>();
            string[] hanyvann = new string[20];
            string s;
            string[] ss = new string[4];
            adatok[] adatok = new adatok[1000];
            int db = 0;
            s = f.ReadLine();
            while (!f.EndOfStream)
            {
                s = f.ReadLine();
                ss = s.Split(';');
                adatok[db].tipus = ss[0];
                adatok[db].meret = int.Parse(ss[1]);
                adatok[db].szin = ss[2];
                adatok[db].allapot = ss[3];
                adatok[db].garancia = int.Parse(ss[4]);
                adatok[db].ar = int.Parse(ss[5]);
                db++;
            }
            f.Close();
            int ezustar = 0, gari=0;
            double legolcsobb = 500000,legdragabb=0;
            for (int i = 0; i < db; i++)
            {
                tipusok.Add(adatok[i].tipus);
                if (adatok[i].szin=="Silver")
                {
                    ezustar += adatok[i].ar;
                }
                if (adatok[i].ar>legdragabb)
                {
                    legdragabb = adatok[i].ar;
                }
                if (adatok[i].ar<legolcsobb)
                {
                    legolcsobb = adatok[i].ar;
                }
                gari += adatok[i].garancia;
            }
            legdragabb /= legolcsobb;
            gari /= db;
            string[] tipusokk = new string[13];
            for (int i = 0; i < db; i++)
            {
                tipusok.Add(adatok[i].tipus);

            }
            for (int i = 0; i < db; i++)
            {
                hanyvann[i] = adatok[i].tipus;

            }


            Console.WriteLine("2.Feladat: {0} db iPhone található a listában.",+db);
            Console.WriteLine("3.Feladat: Az összes ezüst színű készülék {0} Ft-ba kerülne",+ezustar);
            Console.WriteLine("4.Feladat:");
            foreach (var item in tipusok)
            {
                Console.WriteLine(item);
            }
            Console.WriteLine("5.Feladat: Átlagossan {0} hónap garanciát kapunk",gari);
            Console.WriteLine("6.Felaadat: {0}-szor kerül többe a legdrágább a legolcsóbbnál.",legdragabb);

            Console.ReadKey();
        }
    }
}
  1. Create a class to hold the phone information: 创建一个类来保存电话信息:

     class PhoneInfo { public string type; public int size; public string Color; public string newOrUsed; public int warrantyMonth; public decimal price; } 
  2. Read in the text file and create a List of objects. 读入文本文件并创建对象List I would suggest reading about String.Split . 我建议阅读有关String.Split

     List<PhoneInfo> phones; 
  3. Use LINQ to query the List . 使用LINQ查询List

     var phoneCounts = from p in phones group p by p.type into pg select new { type, countOfType = pg.Count(), countOfColors = pg.Select(p => p.color).Distinct().Count() }; 

I could not open the link you sent, you can use hashset or any other data structure, but i prefer to use anonymous data,even you can build your own class and parse your data. 我无法打开您发送的链接,可以使用哈希集或任何其他数据结构,但是我更喜欢使用匿名数据,即使您可以构建自己的类并解析数据。

        var rawData = System.IO.File.ReadAllText("aa.txt");
        var data = rawData.Split(new[] {Environment.NewLine}, StringSplitOptions.None).Select(k =>
            { var segments = k.Split(';');
           return new
            {
                Type = segments[0],
                Size = segments[1],
                Color = segments[2],
                IsNew = segments[3],
                Warranty = segments[4],
                Price = segments[5]
            };});
        var report = data.GroupBy(k => k.Type).Select(p =>
            new
            {
                Type = p.Key,
                TypesCount = p.Count(),
                ColorVarityCount = p.Select(o => o.Color).Distinct().Count()
            });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM