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;
- namespace WeightedSum
- {
- class Program
- {
- static void Main(string[] args)
- {
- string critRangeMinPrompt = "Critical Chance Min: ";
- string critRangeMaxPrompt = "Critical Chance Max: ";
- string tolerancePrompt = "Accurate to how many digits? ";
- string inputError = "Input is not valid.";
- // Common math syntax used; interval is [a, b] where a = min and b = max.
- double a = GetInputForm(critRangeMinPrompt, inputError, 0, 0, 1);
- ClearLine(0, 0);
- Console.Write("Interval: [" + a + ", ]");
- double b = GetInputForm(critRangeMaxPrompt, inputError, 0, 2, 1);
- ClearLine(0, 0);
- Console.Write("Interval: [" + a + ", " + b + "]");
- // If interval is invalid, switch.
- if (a > b)
- {
- double c = b;
- b = a;
- a = c;
- }
- // Tolerance level.
- double tol = Math.Pow(0.1, (int)GetInputForm(tolerancePrompt, inputError, 0, 2));
- // Actually do the math!
- double result = FindPercent(a, b, tol);
- Console.WriteLine("\n" + result*100 + "%");
- Console.ReadLine();
- }
- static double FindCritChance(int x, double xIntercept, double yIntercept, double slope)
- {
- // Piecewise function.
- return slope * 0.05 * (x - 10*xIntercept) - slope * 0.05 * (Math.Abs(x - 10*xIntercept)) + yIntercept;
- }
- static double FindPercent(double min, double max, double tol)
- {
- // This method is a weighted average of a summed piecewise function.
- // Returns the weighted average.
- double sumFactor = FindCritChance(0, (max - min), max, 1);
- double nthWeight = (1 - min);
- double sumWeight = 1;
- for (int n = 1; nthWeight * FindCritChance(n, (max - min), max, 1) > tol; n++)
- {
- sumFactor = sumFactor + nthWeight * FindCritChance(n, (max - min), max, 1);
- sumWeight = sumWeight + nthWeight;
- nthWeight = nthWeight * (1 - FindCritChance(n, (max - min), max, 1));
- }
- return sumFactor/sumWeight;
- }
- static void ClearLine(int lcol, int urow)
- {
- // lcol = left column
- // urow = up row
- // Think of the console window as a group of cells. Cell (0,0) is at the top left of the screen.
- Console.SetCursorPosition(lcol, urow);
- Console.Write(new string(' ', Console.WindowWidth - lcol));
- Console.SetCursorPosition(lcol, urow);
- } // Necessary for GetInputForm.
- static double GetInputForm(string prompt, string error, int lcol, int urow)
- {
- // Asks user for numerical input.
- // Only accepts valid input.
- ClearLine(lcol, urow);
- Console.Write(prompt);
- string inputString = Console.ReadLine();
- double outputDouble;
- while (inputString == null || double.TryParse(inputString, out outputDouble) == false)
- {
- Console.SetCursorPosition(lcol, urow + 2);
- Console.Write(error);
- Console.SetCursorPosition(lcol, urow);
- Console.Write(prompt);
- ClearLine(prompt.Length, urow);
- inputString = Console.ReadLine();
- }
- ClearLine(0, urow + 2);
- return outputDouble;
- }
- static double GetInputForm(string prompt, string error, int lcol, int urow, double maxValue)
- {
- // Overloaded method for GetInputForm.
- // Only accepts input less than or equal to maxValue.
- ClearLine(lcol, urow);
- Console.Write(prompt);
- string inputString = Console.ReadLine();
- double outputDouble;
- while (inputString == null || double.TryParse(inputString, out outputDouble) == false || outputDouble > maxValue)
- {
- Console.SetCursorPosition(lcol, urow + 2);
- Console.Write(error);
- Console.SetCursorPosition(lcol, urow);
- Console.Write(prompt);
- ClearLine(prompt.Length, urow);
- inputString = Console.ReadLine();
- }
- ClearLine(0, urow + 2);
- return outputDouble;
- }
- }
- }
Add Comment
Please, Sign In to add comment