Advertisement
olegstankoptev

Untitled

Apr 13th, 2020
233
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.40 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Globalization;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Runtime.InteropServices;
  7.  
  8. public class Table
  9. {
  10.     List<string[]> Entries;
  11.     public Table(string path)
  12.     {
  13.         Entries = new List<string[]>();
  14.        
  15.         using (FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read))
  16.         using (StreamReader sr = new StreamReader(fs))
  17.         {
  18.             string line;
  19.             while ((line = sr.ReadLine()) != null)
  20.             {
  21.                 Entries.Add(new string[4]);
  22.                 string[] words = line.Split(',');
  23.                 for (int i = 0; i < words.Length; i++)
  24.                 {
  25.                     words[i] = words[i].Trim(' ');
  26.                 }
  27.                 Entries[Entries.Count - 1] = words;
  28.             }
  29.         }
  30.        
  31.     }
  32.    
  33.     public double FindAnnualFeatureValueInRegion(string request)
  34.     {
  35.         string[] words = request.ToLower().Split(',');
  36.         if (words.Length != 2) throw new ArgumentException("incorrect request");
  37.         for (int i = 0; i < words.Length; i++)
  38.         {
  39.             words[i] = words[i].Trim(' ');
  40.         }
  41.         List<string[]> result = Entries.Where(x => x[0].ToLower() == words[0] && x[1].ToLower() == words[1]).ToList();
  42.         if (result.Count == 0) throw new ArgumentException("no rows with such data in table");
  43.         double sum = 0;
  44.         foreach (var entry in result)
  45.         {
  46.             sum += double.Parse(entry[3]);
  47.         }
  48.         return sum;
  49.     }
  50.    
  51.     public double FindAverageAnnualFeatureValueInRegion(string request)
  52.     {
  53.         string[] words = request.ToLower().Split(',');
  54.         if (words.Length != 2) throw new ArgumentException();
  55.         for (int i = 0; i < words.Length; i++)
  56.         {
  57.             words[i] = words[i].Trim(' ');
  58.         }
  59.         List<string[]> result = Entries.Where(x => x[0].ToLower() == words[0] && x[1].ToLower() == words[1]).ToList();
  60.         if (result.Count == 0) throw new ArgumentException("no rows with such data in table");
  61.         double sum = 0;
  62.         foreach (var entry in result)
  63.         {
  64.             sum += double.Parse(entry[3]);
  65.         }
  66.         return double.Parse(string.Format("{0:f3}", sum / result.Count));
  67.     }
  68.    
  69.     public string FindRegionWithMonthlyMaxOrMinFeatureValue(string request)
  70.     {
  71.         string[] words = request.ToLower().Split(',');
  72.         if (words.Length != 3) throw new ArgumentException("incorrect request");
  73.         for (int i = 0; i < words.Length; i++)
  74.         {
  75.             words[i] = words[i].Trim(' ');
  76.         }
  77.  
  78.         Console.WriteLine(DateTime.Parse(Entries[Entries.Count - 1][2]).Month);
  79.         List<string[]> result = Entries.Where(x => x[0].ToLower() == words[0] && DateTime.ParseExact(x[2], "d.M.yyyy", CultureInfo.InvariantCulture).Month == int.Parse(words[1])).ToList();
  80.         if (result.Count == 0) throw new ArgumentException("incorrect request");
  81.         string[] region = result[0];
  82.         if (words[2] == "max")
  83.         {
  84.             foreach (var entry in result)
  85.             {
  86.                 if (double.Parse(entry[3]) > double.Parse(region[3])) region = entry;
  87.             }
  88.         }
  89.         else if (words[2] == "min")
  90.         {
  91.             foreach (var entry in result)
  92.             {
  93.                 if (double.Parse(entry[3]) < double.Parse(region[3])) region = entry;
  94.             }
  95.         }
  96.         else
  97.         {
  98.             throw new ArgumentException("no rows with such data in table");
  99.         }
  100.  
  101.         return region[1];
  102.     }
  103.    
  104.     public double FindMonthlyAverageFeatureValueInAllRegions(string request)
  105.     {
  106.         string[] words = request.ToLower().Split(',');
  107.         if (words.Length != 2) throw new ArgumentException("incorrect request");
  108.         for (int i = 0; i < words.Length; i++)
  109.         {
  110.             words[i] = words[i].Trim(' ');
  111.         }
  112.         List<string[]> result = Entries.Where(x => x[0].ToLower() == words[0] && DateTime.ParseExact(x[2], "d.M.yyyy", CultureInfo.InvariantCulture).Month == int.Parse(words[1])).ToList();
  113.         if (result.Count == 0) throw new ArgumentException("no rows with such data in table");
  114.         double number = 0;
  115.         foreach (var entry in result)
  116.         {
  117.             number += double.Parse(entry[3]);
  118.         }
  119.         return double.Parse(string.Format("{0:f3}", number / result.Count));
  120.     }
  121. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement