C# 使用和不使用空格C进行解析#
例如,我有一个程序,它计算1+1,中间有一个空格,但是我如何在我的代码中使它也计算1+1,而不计算空格?我在考虑一个正则表达式或分割字符串,但我不能成功 有人能帮我吗 这是我的代码:C# 使用和不使用空格C进行解析#,c#,winforms,parsing,C#,Winforms,Parsing,例如,我有一个程序,它计算1+1,中间有一个空格,但是我如何在我的代码中使它也计算1+1,而不计算空格?我在考虑一个正则表达式或分割字符串,但我不能成功 有人能帮我吗 这是我的代码: private char[] SPACE = new char[] { ' ' }; private void GetAnswer(string clipboardText) { //Loop through all questions and answers// foreach (question
private char[] SPACE = new char[] { ' ' };
private 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.Split(SPACE);
var isValid = true;
Double a, b;
// Make sure it's format A # B
if (parts.Length != 3)
return;
// Parse first number
isValid = Double.TryParse(parts[0], out a);
if (!isValid)
return;
var validOperators = new char[] { '+', '-', ':', 'x', '%' };
// Parse operator
if (parts[1].Length != 1)
return;
var op = parts[1][0];
if (!validOperators.Contains(op))
return;
// Parse 2nd number
isValid = Double.TryParse(parts[2], 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);
}
提前谢谢 您可以尝试
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);
}
实际上,我建议首先尝试从剪贴簿文本中提取操作符。如果有效,则按其拆分,并在拆分时删除空条目(空格):
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个元素(希望):
这也意味着取第一个和最后一个数字:
// 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;
假设你只有两个数字,这就是解决方案
// 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
OP正在空间上拆分字符串。删除空格将强制对代码进行另一次更改。@Kazkans我在执行var parts=clipboardText.Replace(“,”)时遇到了一系列错误
因为我的代码没有将字符转换成字符串,你能帮我吗?@rrwe编辑了答案,它现在可以在任意数量的空格上工作
// 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
public static bool isOperator(char opt) {
switch (opt)
{
case '+':
return true;
case '-':
return true;
case '*':
return true;
case '/':
return true;
default:
return false;
}
}