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)
- {
- ConsoleKeyInfo cki = new ConsoleKeyInfo();
- do
- {
- Console.Clear();
- WeightedSum();
- Console.WriteLine("\nPress escape to exit, otherwise press any key to restart.");
- Console.ReadKey();
- } while (cki.Key != ConsoleKey.Escape);
- }
- static void WeightedSum()
- {
- double a = 0;
- string critRangeMinPrompt = "Critical Chance Min in [0, 1]: ";
- string critRangeMaxPrompt = "Critical Chance Max in (" + a + ", 1]: ";
- string tolerancePrompt = "Accurate to how many digits? ";
- string inputError = "Input is not valid.";
- string zeroError = "Interval [0, 0] is not permitted.";
- // Common math syntax used; interval is [a, b] where a = min and b = max.
- double b = 0;
- while (b == 0 && a == 0)
- {
- a = GetInputForm(critRangeMinPrompt, inputError, 0, 0, 1);
- ClearLine(0, 0);
- Console.Write("Interval: [" + a + ", ]");
- b = GetInputForm(critRangeMaxPrompt, inputError, 0, 2, 1);
- ClearLine(0, 0);
- Console.Write("Interval: [" + a + ", " + b + "]");
- if (b == 0 && a == 0)
- {
- Console.Clear();
- Console.SetCursorPosition(0, 4);
- Console.WriteLine(zeroError);
- }
- }
- ClearLine(0, 4);
- Console.SetCursorPosition(0, 3);
- // If interval is invalid, switch.
- if (a > b)
- {
- double c = b;
- b = a;
- a = c;
- }
- // Tolerance level.
- int accuracy = (int)GetInputForm(tolerancePrompt, inputError, 0, 2);
- double tol = Math.Pow(0.1, accuracy);
- // Actually do the math!
- double result = FindPercent(a, b, tol);
- Console.WriteLine("Answer: " + Math.Round(result * 100, accuracy - 2) + "%");
- }
- 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; 10 * 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