mirror of
https://github.com/microsoft/PowerToys.git
synced 2026-04-07 19:57:07 +02:00
Calculator - Human multiplication expressions (#24655)
* fixes #20187 * handles PR reviews - fix some typos - updated dev docs - added PR examples to tests - improve method naming style * Fix typo Co-authored-by: Stefan Markovic <57057282+stefansjfw@users.noreply.github.com> --------- Co-authored-by: José Javier Rodríguez Zas <jj.jobs2live@outlook.com> Co-authored-by: Stefan Markovic <57057282+stefansjfw@users.noreply.github.com>
This commit is contained in:
committed by
GitHub
parent
c72a6cb9d4
commit
cc708e7ac5
@@ -49,6 +49,8 @@ namespace Microsoft.PowerToys.Run.Plugin.Calculator
|
||||
Replace("log(", "log10(", true, CultureInfo.CurrentCulture).
|
||||
Replace("ln(", "log(", true, CultureInfo.CurrentCulture);
|
||||
|
||||
input = CalculateHelper.FixHumanMultiplicationExpressions(input);
|
||||
|
||||
var result = _magesEngine.Interpret(input);
|
||||
|
||||
// This could happen for some incorrect queries, like pi(2)
|
||||
|
||||
@@ -48,5 +48,141 @@ namespace Microsoft.PowerToys.Run.Plugin.Calculator
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public static string FixHumanMultiplicationExpressions(string input)
|
||||
{
|
||||
var output = CheckNumberOrConstantThenParenthesisExpr(input);
|
||||
output = CheckNumberOrConstantThenFunc(output);
|
||||
output = CheckParenthesisExprThenFunc(output);
|
||||
output = CheckParenthesisExprThenParenthesisExpr(output);
|
||||
output = CheckNumberThenConstant(output);
|
||||
output = CheckConstantThenConstant(output);
|
||||
return output;
|
||||
}
|
||||
|
||||
/*
|
||||
* num (exp)
|
||||
* const (exp)
|
||||
*/
|
||||
private static string CheckNumberOrConstantThenParenthesisExpr(string input)
|
||||
{
|
||||
var output = input;
|
||||
do
|
||||
{
|
||||
input = output;
|
||||
output = Regex.Replace(input, @"(\d+|pi|e)\s*(\()", m =>
|
||||
{
|
||||
if (m.Index > 0 && char.IsLetter(input[m.Index - 1]))
|
||||
{
|
||||
return m.Value;
|
||||
}
|
||||
|
||||
return $"{m.Groups[1].Value} * {m.Groups[2].Value}";
|
||||
});
|
||||
}
|
||||
while (output != input);
|
||||
|
||||
return output;
|
||||
}
|
||||
|
||||
/*
|
||||
* num func
|
||||
* const func
|
||||
*/
|
||||
private static string CheckNumberOrConstantThenFunc(string input)
|
||||
{
|
||||
var output = input;
|
||||
do
|
||||
{
|
||||
input = output;
|
||||
output = Regex.Replace(input, @"(\d+|pi|e)\s*([a-zA-Z]+[0-9]*\s*\()", m =>
|
||||
{
|
||||
if (input[m.Index] == 'e' && input[m.Index + 1] == 'x' && input[m.Index + 2] == 'p')
|
||||
{
|
||||
return m.Value;
|
||||
}
|
||||
|
||||
if (m.Index > 0 && char.IsLetter(input[m.Index - 1]))
|
||||
{
|
||||
return m.Value;
|
||||
}
|
||||
|
||||
return $"{m.Groups[1].Value} * {m.Groups[2].Value}";
|
||||
});
|
||||
}
|
||||
while (output != input);
|
||||
|
||||
return output;
|
||||
}
|
||||
|
||||
/*
|
||||
* (exp) func
|
||||
* func func
|
||||
*/
|
||||
private static string CheckParenthesisExprThenFunc(string input)
|
||||
{
|
||||
var p = @"(\))\s*([a-zA-Z]+[0-9]*\s*\()";
|
||||
var r = "$1 * $2";
|
||||
return Regex.Replace(input, p, r);
|
||||
}
|
||||
|
||||
/*
|
||||
* (exp) (exp)
|
||||
* func (exp)
|
||||
*/
|
||||
private static string CheckParenthesisExprThenParenthesisExpr(string input)
|
||||
{
|
||||
var p = @"(\))\s*(\()";
|
||||
var r = "$1 * $2";
|
||||
return Regex.Replace(input, p, r);
|
||||
}
|
||||
|
||||
/*
|
||||
* num const
|
||||
*/
|
||||
private static string CheckNumberThenConstant(string input)
|
||||
{
|
||||
var output = input;
|
||||
do
|
||||
{
|
||||
input = output;
|
||||
output = Regex.Replace(input, @"(\d+)\s*(pi|e)", m =>
|
||||
{
|
||||
if (m.Index > 0 && char.IsLetter(input[m.Index - 1]))
|
||||
{
|
||||
return m.Value;
|
||||
}
|
||||
|
||||
return $"{m.Groups[1].Value} * {m.Groups[2].Value}";
|
||||
});
|
||||
}
|
||||
while (output != input);
|
||||
|
||||
return output;
|
||||
}
|
||||
|
||||
/*
|
||||
* const const
|
||||
*/
|
||||
private static string CheckConstantThenConstant(string input)
|
||||
{
|
||||
var output = input;
|
||||
do
|
||||
{
|
||||
input = output;
|
||||
output = Regex.Replace(input, @"(pi|e)\s*(pi|e)", m =>
|
||||
{
|
||||
if (m.Index > 0 && char.IsLetter(input[m.Index - 1]))
|
||||
{
|
||||
return m.Value;
|
||||
}
|
||||
|
||||
return $"{m.Groups[1].Value} * {m.Groups[2].Value}";
|
||||
});
|
||||
}
|
||||
while (output != input);
|
||||
|
||||
return output;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user