Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.IO;
- namespace ProfitCalculator
- {
- /// ==============================================================================
- public class RangeFinder
- {
- /// <summary>
- /// Performs calculations to find the start and end of the "best" data sequence
- /// which has the greatest sum of data values over that sequence.
- /// </summary>
- /// <param name="data">the data to be examined</param>
- /// <param name="bestStart">the start point found by the search</param>
- /// <param name="bestEnd">the end point found by the search</param>
- /// <param name="bestTotal">the sum of data values over that range</param>
- /// <param name="loops">the number of executions of the inner loop</param>
- public static void MaxSum(double[] data, out int bestStart,
- out int bestEnd, out double bestTotal, out int loops)
- {
- bestTotal = 0;
- bestStart = 0;
- bestEnd = 0;
- loops = 0;
- for (int i = 0; i < data.Length; i++)
- for (int j = i+1; j < data.Length; j++)
- {
- double subTotal = 0;
- for (int k = i; k <= j; k++)
- subTotal = subTotal + data[k];
- if (subTotal > bestTotal)
- {
- bestTotal = subTotal;
- bestStart = i;
- bestEnd = j;
- }
- }
- }
- public static void MaxSum2(double[] data, out int bestStart,
- out int bestEnd, out double bestTotal, out int loops)
- {
- bestTotal = 0;
- bestStart = 0;
- bestEnd = 0;
- loops = 0;
- for (int i = 0; i < data.Length; i++)
- {
- double subTotal = 0;
- for (int j = i; j < data.Length; j++)
- {
- subTotal = subTotal + data[j];
- if (subTotal > bestTotal)
- {
- bestTotal = subTotal;
- bestStart = i;
- bestEnd = j;
- }
- }
- }
- }
- public static void MaxSum3(double[] data, out int bestStart,
- out int bestEnd, out double bestTotal, out int loops)
- {
- bestTotal = 0;
- bestStart = 0;
- bestEnd = 0;
- loops = 0;
- int i = 0;
- double subtotal = 0;
- for (int k = i; k <= ; )
- }
- }
- }
- /// ==============================================================================
- /// <summary>
- /// Tests the Profits Calculator
- /// </summary>
- class Test
- {
- /// <summary>
- /// The main entry point for the application.
- /// </summary>
- static void Main()
- {
- double[] data;
- int bestStart, bestEnd;
- double bestTotal;
- int loops;
- /// name of the file and the number of readings
- string filename = "week52.txt";
- int items = 52;
- data = new double[items]; /// create the data array
- try
- {
- TextReader textIn = new StreamReader(filename);
- for (int i = 0; i < items; i++) /// input and store the data values
- {
- string line = textIn.ReadLine();
- data[i] = double.Parse(line);
- }
- textIn.Close();
- }
- catch
- {
- Console.WriteLine("File Read Failed");
- return;
- }
- /// ---------------------------------------------------------------------
- /// call the process method to find the best profit period
- /// ---------------------------------------------------------------------
- RangeFinder.MaxSum(
- data, out bestStart, out bestEnd, out bestTotal, out loops);
- Console.WriteLine("Start : {0} End : {1} Total {2} Loops {3}",
- bestStart, bestEnd, bestTotal, loops);
- Console.ReadKey();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement