Advertisement
olegstankoptev

Untitled

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