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 關注
- 132 瀏覽
添加回答
舉報