Advertisement
Guest User

SE ACW

a guest
Mar 23rd, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.38 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. namespace ProfitCalculator
  8. {
  9.     /// ==============================================================================
  10.     public class RangeFinder
  11.     {
  12.         /// <summary>
  13.         /// Performs calculations to find the start and end of the "best" data sequence
  14.         /// which has the greatest sum of data values over that sequence.
  15.         /// </summary>
  16.         /// <param name="data">the data to be examined</param>
  17.         /// <param name="bestStart">the start point found by the search</param>
  18.         /// <param name="bestEnd">the end point found by the search</param>
  19.         /// <param name="bestTotal">the sum of data values over that range</param>
  20.         /// <param name="loops">the number of executions of the inner loop</param>
  21.         public static void MaxSum(double[] data, out int bestStart,
  22.        out int bestEnd, out double bestTotal, out int loops)
  23.         {
  24.             bestTotal = 0;
  25.             bestStart = 0;
  26.             bestEnd = 0;
  27.             loops = 0;
  28.  
  29.             for (int i = 0; i < data.Length; i++)
  30.                 for (int j = i+1; j < data.Length; j++)
  31.                 {
  32.                     double subTotal = 0;
  33.                     for (int k = i; k <= j; k++)
  34.                         subTotal = subTotal + data[k];
  35.                     if (subTotal > bestTotal)
  36.                     {
  37.                         bestTotal = subTotal;
  38.                         bestStart = i;
  39.                         bestEnd = j;
  40.                     }
  41.                    
  42.                 }
  43.  
  44.         }
  45.         public static void MaxSum2(double[] data, out int bestStart,
  46.         out int bestEnd, out double bestTotal, out int loops)
  47.         {
  48.             bestTotal = 0;
  49.             bestStart = 0;
  50.             bestEnd = 0;
  51.             loops = 0;
  52.  
  53.             for (int i = 0; i < data.Length; i++)
  54.             {
  55.                double subTotal = 0;
  56.                 for (int j = i; j < data.Length; j++)
  57.                 {
  58.                     subTotal = subTotal + data[j];
  59.                     if (subTotal > bestTotal)
  60.                     {
  61.                         bestTotal = subTotal;
  62.                         bestStart = i;
  63.                         bestEnd = j;
  64.                     }
  65.                 }
  66.             }
  67.         }
  68.         public static void MaxSum3(double[] data, out int bestStart,
  69.        out int bestEnd, out double bestTotal, out int loops)
  70.         {
  71.             bestTotal = 0;
  72.             bestStart = 0;
  73.             bestEnd = 0;
  74.             loops = 0;
  75.  
  76.             int i = 0;
  77.             double subtotal = 0;
  78.             for (int k = i; k <= ;   )
  79.         }
  80.     }
  81.  
  82. }
  83.  
  84.     /// ==============================================================================
  85.     /// <summary>
  86.     /// Tests the Profits Calculator
  87.     /// </summary>
  88.     class Test
  89.     {
  90.         /// <summary>
  91.         /// The main entry point for the application.
  92.         /// </summary>
  93.         static void Main()
  94.         {
  95.             double[] data;
  96.             int bestStart, bestEnd;
  97.             double bestTotal;
  98.             int loops;
  99.             /// name of the file and the number of readings
  100.             string filename = "week52.txt";
  101.             int items = 52;
  102.             data = new double[items]; /// create the data array
  103.             try
  104.             {
  105.                 TextReader textIn = new StreamReader(filename);
  106.                 for (int i = 0; i < items; i++) /// input and store the data values
  107.                 {
  108.                     string line = textIn.ReadLine();
  109.                     data[i] = double.Parse(line);
  110.                 }
  111.                 textIn.Close();
  112.             }
  113.             catch
  114.             {
  115.                 Console.WriteLine("File Read Failed");
  116.                 return;
  117.             }
  118.             /// ---------------------------------------------------------------------
  119.             /// call the process method to find the best profit period
  120.             /// ---------------------------------------------------------------------
  121.             RangeFinder.MaxSum(
  122. data, out bestStart, out bestEnd, out bestTotal, out loops);
  123.             Console.WriteLine("Start : {0} End : {1} Total {2} Loops {3}",
  124.             bestStart, bestEnd, bestTotal, loops);
  125.  
  126.             Console.ReadKey();
  127.         }
  128.     }
  129. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement