3 回答
TA贡献1841条经验 获得超3个赞
假设您只有两个数字,如问题中所述,这是解决方案
// this code will work for all of the text mentioned in comments
//string text = "1 + 1";
//string text = "1+1";
//string text = "100.998+ 2000.7";
//string text = "10 + 2000.7";
string text = "100+ 2000";
text = Regex.Replace(text, @"\s+", "");
double num1 = 0;
double num2 = 0;
char operand ;
int startIndex = 0;
for (int i = 0; i < text.Length; i++)
{
// look for first operator
if (isOperator(text[i]) || (i+1) == text.Length) {
if (startIndex == 0) {
double.TryParse(text.Substring(startIndex, i), out num1);
operand = text[i];
startIndex = i + 1;
}
else
{
double.TryParse(text.Substring(startIndex), out num2);
break;
}
}
}
// calculate(num1,operand,num2) // Implement this method with a simple switch case and this will calculate the final answer for you
isOperator 方法的实现:
public static bool isOperator(char opt) {
switch (opt)
{
case '+':
return true;
case '-':
return true;
case '*':
return true;
case '/':
return true;
default:
return false;
}
}
TA贡献1772条经验 获得超5个赞
实际上我建议首先尝试从clipboardText. 如果有效,则按其拆分并在拆分时删除空条目(空格):
var validOperators = new char[] { '+', '-', ':', 'x', '%' };
char op = validOperators.FirstOrDefault(o => clipboardText.Contains(o));
if (op == default(char))
return;
var parts = clipboardText.Split(new char[] { op}, StringSplitOptions.RemoveEmptyEntries);
最后一件事是现在您的格式将只是数字!零件将只有 2 个元素(希望如此):
// Make sure it's format A # B
if (parts.Length != 2)
return;
这也意味着取第一个和最后一个数字:
// Parse first number
isValid = Double.TryParse(parts.First(), out a);
if (!isValid)
return;
// Parse last number
isValid = Double.TryParse(parts.Last(), out b);
if (!isValid)
return;
不,您现在可以摆脱运算符转换和检查:
// Parse operator
if (parts[1].Length != 1)
return;
var op = parts[1][0];
if (!validOperators.Contains(op))
return;
TA贡献1839条经验 获得超15个赞
您可以尝试var parts = clipboardText.Replace(" ", "");这样做,这应该使您的输出始终没有空间。
private static void GetAnswer(string clipboardText)
{
//Loop through all questions and answers//
foreach (question q in questionList)
{
//If we have found an answer that is exactly the same show an Notification//
//Startwith zoekt naar alle vragen die matchen vanaf het begin van de zin en Endwith alle vragen die matchen vanaf het eind van de zin//
if (q._question.StartsWith(clipboardText) || q._question.EndsWith(clipboardText))
{
ShowNotification(q._question, q._answer);
break;
}
}
var parts = clipboardText.Replace(" ", "");
var isValid = true;
Double a, b;
// Make sure it's format A # B
char? op = null;
int end;
var validOperators = new char[] { '+', '-', ':', 'x', '%' };
// find operator
foreach (char oper in validOperators)
{
if (parts.Contains(oper))
{
end = parts.IndexOf(oper);
op = oper;
}
}
if (!op.HasValue)
return;
// split to argument with op
var arguments = parts.Split(op.Value);
// Parse first number
isValid = Double.TryParse(arguments[0], out a);
if (!isValid)
return;
// Parse 2nd number
isValid = Double.TryParse(arguments[1], out b);
if (!isValid)
return;
// Now calculate the answer
string answer = null;
switch (op)
{
case '+':
answer = (a + b).ToString();
break;
case '-':
answer = (a - b).ToString();
break;
case ':':
if (b == 0)
answer = "NaN";
else
answer = (a / b).ToString();
break;
case 'x':
answer = (a * b).ToString();
break;
// rekent percentage van een bedrag
case '%':
answer = (a / b * 100).ToString();
break;
default:
throw new InvalidOperationException();
}
// Show the answer
ShowNotification(clipboardText,answer);
}
- 3 回答
- 0 关注
- 99 浏览
添加回答
举报