Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- 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]);
- Entries[Entries.Count - 1] = line.Split(',');
- }
- }
- }
- public double FindAnnualFeatureValueInRegion(string request)
- {
- string[] words = request.ToLower().Split(',');
- if (words.Length != 2) throw new ArgumentException();
- for (int i = 0; i < words.Length; i++)
- {
- if (words[i][0] == ' ') words[i] = words[i].Substring(1, words[i].Length - 1);
- }
- List<string[]> result = Entries.Where(x => x[0] == words[0] && x[1] == words[1]).ToList();
- if (result.Count == 0) throw new ArgumentException();
- 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++)
- {
- if (words[i][0] == ' ') words[i] = words[i].Substring(1, words[i].Length - 1);
- }
- List<string[]> result = Entries.Where(x => x[0] == words[0] && x[1] == words[1]).ToList();
- if (result.Count == 0) throw new ArgumentException();
- 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();
- for (int i = 0; i < words.Length; i++)
- {
- if (words[i][0] == ' ') words[i] = words[i].Substring(1, words[i].Length - 1);
- }
- List<string[]> result = Entries.Where(x => x[0] == words[0] && DateTime.Parse(x[2]).Month == int.Parse(words[1])).ToList();
- if (result.Count == 0) throw new ArgumentException();
- 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();
- }
- return region[1];
- }
- public double FindMonthlyAverageFeatureValueInAllRegions(string request)
- {
- string[] words = request.ToLower().Split(',');
- if (words.Length != 2) throw new ArgumentException();
- for (int i = 0; i < words.Length; i++)
- {
- if (words[i][0] == ' ') words[i] = words[i].Substring(1, words[i].Length - 1);
- }
- List<string[]> result = Entries.Where(x => x[0] == words[0] && DateTime.Parse(x[2]).Month == int.Parse(words[1])).ToList();
- if (result.Count == 0) throw new ArgumentException();
- double number = 0;
- foreach (var entry in result)
- {
- number += double.Parse(entry[3]);
- }
- return double.Parse(string.Format("{0:f2}", number / result.Count));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement