Guest User

Weighted Sum vFinal

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