Advertisement
Guest User

Untitled

a guest
Jan 23rd, 2017
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.36 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.IO;
  7.  
  8. namespace WTWProgram
  9. {
  10.     class Program
  11.     {
  12.         static void Main(string[] args)
  13.         {
  14.             //Open file and declare lists to store the data columns
  15.             var reader = new StreamReader(File.OpenRead(@"C:\Users\Harald\Documents\CVs\WTW TEST\InputData.csv"));
  16.             List<string> Products = new List<string>();
  17.             List<int> Origins = new List<int>();
  18.             List<int> Developments = new List<int>();
  19.             List<double> IncValues = new List<double>();
  20.  
  21.             //Read from file and split columns on commas
  22.             while (!reader.EndOfStream)
  23.             {
  24.                 var line = reader.ReadLine();
  25.                 var values = line.Split(',');
  26.                 int i;
  27.                 double j;
  28.  
  29.                 //TryParse is used to return values of 0 for the column headings.
  30.                 Products.Add(values[0]);
  31.                 if (int.TryParse(values[1], out i)) Origins.Add(i);
  32.                 if (int.TryParse(values[2], out i)) Developments.Add(i);
  33.                 if (double.TryParse(values[3], out j)) IncValues.Add(j);
  34.             }
  35.            
  36.             //Calculate the range of years, the starting year and the quantity of different products
  37.             int Range = Developments.Max() - Origins.Min() + 1;
  38.             int Start = Origins.Min();
  39.             int ProductCount = Products.Distinct().ToList().Count() -1;
  40.            
  41.             Console.Write(Start + "," + Range);
  42.  
  43.             //Calculate the size of the triangles to be filled
  44.             int TriangleSize=0;
  45.  
  46.             for(int i=0;i<Range;i++)
  47.             {
  48.                 TriangleSize = TriangleSize + Range - i;
  49.             }
  50.  
  51.             //Create an array to hold the triangles of cumulative values
  52.             double[,] CumuValues = new double[ProductCount,TriangleSize];
  53.  
  54.             //This index will be used to increment the row of the above array
  55.             int Row = 0;
  56.  
  57.             for (int i=1;i<Products.Count();i++)
  58.             {
  59.                 int k = i - 1; //Index for the year lists and Incvalues
  60.  
  61.                 //Calculate the appropriate starting point in the row using the triangle number equation.
  62.                 int Diff = Origins[k] - Start;
  63.                 Diff = Diff * (Diff + 1) / 2;
  64.  
  65.                 //Calculate the number of development years from the origin
  66.                 int DevYrs = Developments[k] - Origins[k];
  67.  
  68.                 //Calculate the cumulative value
  69.                 double Cumu = 0.0;
  70.  
  71.                 for (int j = 0; j <= DevYrs; j++)
  72.                 {
  73.                     Cumu = Cumu + IncValues[k - j];
  74.                 }
  75.  
  76.                 CumuValues[Row, Diff + DevYrs - 1] = Cumu;
  77.                
  78.  
  79.  
  80.                 if(Products[k]!=Products[i])
  81.                 {
  82.                     Console.Write("\n" + Products[i]);
  83.                 }
  84.  
  85.                 if (Developments[i] == Developments.Max())
  86.                 {
  87.  
  88.                     for (int x = 0; x < TriangleSize; x++)
  89.                     {
  90.                         Console.Write("," + CumuValues[Row, x]);
  91.                     }
  92.                     Row++;
  93.                 }
  94.  
  95.  
  96.             }
  97.            
  98.             Console.ReadKey();
  99.         }
  100.     }
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement