Guest User

Weighted Sum

a guest
Jul 30th, 2014
276
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.61 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.  
  7. namespace WeightedSum
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             string critRangeMinPrompt = "Critical Chance Min: ";
  14.             string critRangeMaxPrompt = "Critical Chance Max: ";
  15.             string tolerancePrompt = "Accurate to how many digits? ";
  16.             string inputError = "Input is not valid.";
  17.  
  18.             // Common math syntax used; interval is [a, b] where a = min and b = max.
  19.             double a = GetInputForm(critRangeMinPrompt, inputError, 0, 0, 1);
  20.             ClearLine(0, 0);
  21.             Console.Write("Interval: [" + a + ", ]");
  22.             double b = GetInputForm(critRangeMaxPrompt, inputError, 0, 2, 1);
  23.             ClearLine(0, 0);
  24.             Console.Write("Interval: [" + a + ", " + b + "]");
  25.  
  26.             // If interval is invalid, switch.
  27.             if (a > b)
  28.             {
  29.                 double c = b;
  30.                 b = a;
  31.                 a = c;
  32.             }
  33.  
  34.             // Tolerance level.
  35.             double tol = Math.Pow(0.1, (int)GetInputForm(tolerancePrompt, inputError, 0, 2));
  36.  
  37.             // Actually do the math!
  38.             double result = FindPercent(a, b, tol);
  39.             Console.WriteLine("\n" + result*100 + "%");
  40.             Console.ReadLine();
  41.         }
  42.  
  43.         static double FindCritChance(int x, double xIntercept, double yIntercept, double slope)
  44.         {
  45.             // Piecewise function.
  46.             return slope * 0.05 * (x - 10*xIntercept) - slope * 0.05 * (Math.Abs(x - 10*xIntercept)) + yIntercept;
  47.         }
  48.  
  49.         static double FindPercent(double min, double max, double tol)
  50.         {
  51.             // This method is a weighted average of a summed piecewise function.
  52.             // Returns the weighted average.
  53.  
  54.             double sumFactor = FindCritChance(0, (max - min), max, 1);
  55.             double nthWeight = (1 - min);
  56.             double sumWeight = 1;
  57.  
  58.             for (int n = 1; nthWeight * FindCritChance(n, (max - min), max, 1) > tol; n++)
  59.             {
  60.                 sumFactor = sumFactor + nthWeight * FindCritChance(n, (max - min), max, 1);
  61.                 sumWeight = sumWeight + nthWeight;
  62.                 nthWeight = nthWeight * (1 - FindCritChance(n, (max - min), max, 1));
  63.             }
  64.             return sumFactor/sumWeight;
  65.         }
  66.  
  67.         static void ClearLine(int lcol, int urow)
  68.         {
  69.             // lcol = left column
  70.             // urow = up row
  71.             // Think of the console window as a group of cells.  Cell (0,0) is at the top left of the screen.
  72.  
  73.             Console.SetCursorPosition(lcol, urow);
  74.             Console.Write(new string(' ', Console.WindowWidth - lcol));
  75.             Console.SetCursorPosition(lcol, urow);
  76.         } // Necessary for GetInputForm.
  77.  
  78.         static double GetInputForm(string prompt, string error, int lcol, int urow)
  79.         {
  80.             // Asks user for numerical input.
  81.             // Only accepts valid input.
  82.  
  83.             ClearLine(lcol, urow);
  84.             Console.Write(prompt);
  85.             string inputString = Console.ReadLine();
  86.             double outputDouble;
  87.             while (inputString == null || double.TryParse(inputString, out outputDouble) == false)
  88.             {
  89.                 Console.SetCursorPosition(lcol, urow + 2);
  90.                 Console.Write(error);
  91.                 Console.SetCursorPosition(lcol, urow);
  92.                 Console.Write(prompt);
  93.                 ClearLine(prompt.Length, urow);
  94.                 inputString = Console.ReadLine();
  95.             }
  96.             ClearLine(0, urow + 2);
  97.             return outputDouble;
  98.         }
  99.  
  100.         static double GetInputForm(string prompt, string error, int lcol, int urow, double maxValue)
  101.         {
  102.             // Overloaded method for GetInputForm.
  103.             // Only accepts input less than or equal to maxValue.
  104.  
  105.             ClearLine(lcol, urow);
  106.             Console.Write(prompt);
  107.             string inputString = Console.ReadLine();
  108.             double outputDouble;
  109.             while (inputString == null || double.TryParse(inputString, out outputDouble) == false || outputDouble > maxValue)
  110.             {
  111.                 Console.SetCursorPosition(lcol, urow + 2);
  112.                 Console.Write(error);
  113.                 Console.SetCursorPosition(lcol, urow);
  114.                 Console.Write(prompt);
  115.                 ClearLine(prompt.Length, urow);
  116.                 inputString = Console.ReadLine();
  117.             }
  118.             ClearLine(0, urow + 2);
  119.             return outputDouble;
  120.         }
  121.     }
  122.  
  123. }
Add Comment
Please, Sign In to add comment