This repository has been archived by the owner on Dec 5, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 92
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Adding some other tasks and tinkering with better wpf for tasks screen * Updating verbiage in Pause control to make it more clear * stylecop updates * trying improved styling with the readme * really better to remove the styling on the readme * adding Telemetry service to list of things one can disable * creating signed dlls and prepping for 4.4 release * updating readme Co-authored-by: weredev <[email protected]>
- Loading branch information
Showing
15 changed files
with
563 additions
and
97 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,156 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Globalization; | ||
using System.Linq; | ||
using System.Windows.Data; | ||
|
||
namespace WereDev.Utils.Wu10Man.Converters | ||
{ | ||
// Does a math equation on the bound value. | ||
// Use @VALUE in your mathEquation as a substitute for bound value | ||
// Operator order is parenthesis first, then Left-To-Right (no operator precedence) | ||
// https://rachel53461.wordpress.com/2011/08/20/the-math-converter/ | ||
public class MathConverter : IValueConverter | ||
{ | ||
private static readonly char[] _allOperators = new[] { '+', '-', '*', '/', '%', '(', ')' }; | ||
|
||
private static readonly List<string> _grouping = new List<string> { "(", ")" }; | ||
private static readonly List<string> _operators = new List<string> { "+", "-", "*", "/", "%" }; | ||
|
||
public object Convert(object value, Type targetType, object parameter, CultureInfo culture) | ||
{ | ||
// Parse value into equation and remove spaces | ||
var mathEquation = parameter as string; | ||
mathEquation = mathEquation.Replace(" ", string.Empty); | ||
mathEquation = mathEquation.Replace("@VALUE", value?.ToString() ?? string.Empty); | ||
|
||
// Validate values and get list of numbers in equation | ||
var numbers = new List<double>(); | ||
foreach (string s in mathEquation.Split(_allOperators)) | ||
{ | ||
if (!string.IsNullOrEmpty(s)) | ||
{ | ||
if (double.TryParse(s, out var tmp)) | ||
{ | ||
numbers.Add(tmp); | ||
} | ||
else | ||
{ | ||
// Handle Error - Some non-numeric, operator, or grouping character found in string | ||
throw new InvalidCastException(); | ||
} | ||
} | ||
} | ||
|
||
// Begin parsing method | ||
EvaluateMathString(ref mathEquation, ref numbers, 0); | ||
|
||
// After parsing the numbers list should only have one value - the total | ||
return numbers[0]; | ||
} | ||
|
||
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) | ||
{ | ||
throw new NotImplementedException(); | ||
} | ||
|
||
// Evaluates a mathematical string and keeps track of the results in a List<double> of numbers | ||
private void EvaluateMathString(ref string mathEquation, ref List<double> numbers, int index) | ||
{ | ||
// Loop through each mathemtaical token in the equation | ||
string token = GetNextToken(mathEquation); | ||
|
||
while (!string.IsNullOrEmpty(token)) | ||
{ | ||
// Remove token from mathEquation | ||
mathEquation = mathEquation.Remove(0, token.Length); | ||
|
||
// If token is a grouping character, it affects program flow | ||
if (_grouping.Contains(token)) | ||
{ | ||
switch (token) | ||
{ | ||
case "(": | ||
EvaluateMathString(ref mathEquation, ref numbers, index); | ||
break; | ||
|
||
case ")": | ||
return; | ||
} | ||
} | ||
|
||
// If token is an operator, do requested operation | ||
if (_operators.Contains(token)) | ||
{ | ||
// If next token after operator is a parenthesis, call method recursively | ||
string nextToken = GetNextToken(mathEquation); | ||
if (nextToken == "(") | ||
{ | ||
EvaluateMathString(ref mathEquation, ref numbers, index + 1); | ||
} | ||
|
||
// Verify that enough numbers exist in the List<double> to complete the operation | ||
// and that the next token is either the number expected, or it was a ( meaning | ||
// that this was called recursively and that the number changed | ||
if (numbers.Count > (index + 1) && | ||
(double.Parse(nextToken) == numbers[index + 1] || nextToken == "(")) | ||
{ | ||
switch (token) | ||
{ | ||
case "+": | ||
numbers[index] = numbers[index] + numbers[index + 1]; | ||
break; | ||
case "-": | ||
numbers[index] = numbers[index] - numbers[index + 1]; | ||
break; | ||
case "*": | ||
numbers[index] = numbers[index] * numbers[index + 1]; | ||
break; | ||
case "/": | ||
numbers[index] = numbers[index] / numbers[index + 1]; | ||
break; | ||
case "%": | ||
numbers[index] = numbers[index] % numbers[index + 1]; | ||
break; | ||
} | ||
|
||
numbers.RemoveAt(index + 1); | ||
} | ||
else | ||
{ | ||
// Handle Error - Next token is not the expected number | ||
throw new FormatException("Next token is not the expected number"); | ||
} | ||
} | ||
|
||
token = GetNextToken(mathEquation); | ||
} | ||
} | ||
|
||
// Gets the next mathematical token in the equation | ||
private string GetNextToken(string mathEquation) | ||
{ | ||
// If we're at the end of the equation, return string.empty | ||
if (string.IsNullOrEmpty(mathEquation)) | ||
{ | ||
return string.Empty; | ||
} | ||
|
||
// Get next operator or numeric value in equation and return it | ||
string tmp = string.Empty; | ||
foreach (char c in mathEquation) | ||
{ | ||
if (_allOperators.Contains(c)) | ||
{ | ||
return string.IsNullOrEmpty(tmp) ? c.ToString() : tmp; | ||
} | ||
else | ||
{ | ||
tmp += c; | ||
} | ||
} | ||
|
||
return tmp; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.