110,499
社区成员
发帖
与我相关
我的任务
分享
string test = @"+5又很-9";
float chengji = 0;
float.TryParse(test, out chengji);
if (chengji == 0)
{
string pattern_match = @"^([+-]\d+/?)+$";
if (Regex.IsMatch(test, pattern_match))
{
string pattern_com = @"([+-])(\d+(\.\d+)?)";
Regex.Matches(test, pattern_com).Cast<Match>().ToList().ForEach(a =>
{
if (a.Groups[1].Value.Equals("+"))
{
chengji += float.Parse(a.Groups[2].Value);
}
else
{
chengji -= float.Parse(a.Groups[2].Value);
}
});
}
else
{
MessageBox.Show("格式错误");
}
}
string test = @"+90/-5";
float chengji=0;
float.TryParse(test,out chengji);
if (chengji == 0)
{
string pattern_com = @"([+-])(\d+(\.\d+)?)";
Regex.Matches(test, pattern_com).Cast<Match>().ToList().ForEach(a => {
if (a.Groups[1].Value.Equals("+"))
{
chengji += float.Parse(a.Groups[2].Value);
}
else
{
chengji -= float.Parse(a.Groups[2].Value);
}
});
}
//此时 chengji=85