Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Globalization;
- using System.IO;
- using System.Linq;
- using System.Runtime.InteropServices;
- public class Table
- {
- List<string[]> Entries;
- public Table(string path)
- {
- Entries = new List<string[]>();
- using (FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read))
- using (StreamReader sr = new StreamReader(fs))
- {
- string line;
- while ((line = sr.ReadLine()) != null)
- {
- Entries.Add(new string[4]);
- string[] words = line.Split(',');
- for (int i = 0; i < words.Length; i++)
- {
- words[i] = words[i].Trim(' ');
- }
- Entries[Entries.Count - 1] = words;
- }
- }
- }
- public double FindAnnualFeatureValueInRegion(string request)
- {
- string[] words = request.ToLower().Split(',');
- if (words.Length != 2) throw new ArgumentException("incorrect request");
- for (int i = 0; i < words.Length; i++)
- {
- words[i] = words[i].Trim(' ');
- }
- List<string[]> result = Entries.Where(x => x[0].ToLower() == words[0] && x[1].ToLower() == words[1]).ToList();
- if (result.Count == 0) throw new ArgumentException("no rows with such data in table");
- double sum = 0;
- foreach (var entry in result)
- {
- sum += double.Parse(entry[3]);
- }
- return sum;
- }
- public double FindAverageAnnualFeatureValueInRegion(string request)
- {
- string[] words = request.ToLower().Split(',');
- if (words.Length != 2) throw new ArgumentException();
- for (int i = 0; i < words.Length; i++)
- {
- words[i] = words[i].Trim(' ');
- }
- List<string[]> result = Entries.Where(x => x[0].ToLower() == words[0] && x[1].ToLower() == words[1]).ToList();
- if (result.Count == 0) throw new ArgumentException("no rows with such data in table");
- double sum = 0;
- foreach (var entry in result)
- {
- sum += double.Parse(entry[3]);
- }
- return double.Parse(string.Format("{0:f3}", sum / result.Count));
- }
- public string FindRegionWithMonthlyMaxOrMinFeatureValue(string request)
- {
- string[] words = request.ToLower().Split(',');
- if (words.Length != 3) throw new ArgumentException("incorrect request");
- for (int i = 0; i < words.Length; i++)
- {
- words[i] = words[i].Trim(' ');
- }
- Console.WriteLine(DateTime.Parse(Entries[Entries.Count - 1][2]).Month);
- 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();
- if (result.Count == 0) throw new ArgumentException("incorrect request");
- string[] region = result[0];
- if (words[2] == "max")
- {
- foreach (var entry in result)
- {
- if (double.Parse(entry[3]) > double.Parse(region[3])) region = entry;
- }
- }
- else if (words[2] == "min")
- {
- foreach (var entry in result)
- {
- if (double.Parse(entry[3]) < double.Parse(region[3])) region = entry;
- }
- }
- else
- {
- throw new ArgumentException("no rows with such data in table");
- }
- return region[1];
- }
- public double FindMonthlyAverageFeatureValueInAllRegions(string request)
- {
- string[] words = request.ToLower().Split(',');
- if (words.Length != 2) throw new ArgumentException("incorrect request");
- for (int i = 0; i < words.Length; i++)
- {
- words[i] = words[i].Trim(' ');
- }
- 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();
- if (result.Count == 0) throw new ArgumentException("no rows with such data in table");
- double number = 0;
- foreach (var entry in result)
- {
- number += double.Parse(entry[3]);
- }
- return double.Parse(string.Format("{0:f3}", number / result.Count));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement