c# - System.FormatException 格式正确 C#
问题描述
我正在制作一个 dll 以使用 c# 中缺少的一些函数,例如 python 的 eval 函数(这绝对是 awsome )这是我的 eval 类的代码
public class EvalMath
{
public double result { get; private set; }
private delegate double Operation(double x, double y);
public EvalMath()
{
}
private static string Reverse(string s)
{
char[] charArray = s.ToCharArray();
Array.Reverse(charArray);
return new string(charArray);
}
public double Eval(string operation)
{
operation = Reverse(operation);
Operation op = null;
if (operation == null) throw new EvalArgumentException();
string integer1_r = "";
foreach (char chr in operation)
{
if(chr == '+' || chr == '-' || chr == '*' || chr == '/')
{
string integer = operation.Substring(0, operation.IndexOf(chr));
string integer1 = operation.Substring(operation.IndexOf(chr) + 1);
if(integer1.Contains("+") || integer1.Contains("-") || integer1.Contains("*") || integer1.Contains("/")) integer1_r = Eval(integer.ToString()).ToString();
switch (chr)
{
case '+':
op = (x, y) => x + y;
break;
case '-':
op = (x, y) => x - y;
break;
case '*':
op = (x, y) => x * y;
break;
case '/':
op = (x, y) => x / y;
break;
}
System.Windows.Forms.MessageBox.Show(integer + " " + integer1_r); // for debug
result = op(Convert.ToDouble(integer), Convert.ToDouble(integer1_r));
return result;
}
}
return 0.0;
}
}
我正在尝试这段代码来测试它
EvalMath e = new EvalMath();
double res = e.Eval("3+3");
Console.ReadLine();
但是每当我尝试运行代码时,它只会在这一行中给我一个 System.FormatException
double res = e.Eval("3+3");
解决方案
在您的代码中添加了一行。如果 integer1 不包含任何运算符,您忘记设置 integer1_r 值。在您的代码中添加了以下行。
else integer1_r = integer1;
以下是完整的代码。
public class EvalMath
{
public double result { get; private set; }
private delegate double Operation(double x, double y);
public EvalMath()
{
}
private static string Reverse(string s)
{
char[] charArray = s.ToCharArray();
Array.Reverse(charArray);
return new string(charArray);
}
public double Eval(string operation)
{
operation = Reverse(operation);
Operation op = null;
if (operation == null) throw new EvalArgumentException();
string integer1_r = "";
foreach (char chr in operation)
{
if(chr == '+' || chr == '-' || chr == '*' || chr == '/')
{
string integer = operation.Substring(0, operation.IndexOf(chr));
string integer1 = operation.Substring(operation.IndexOf(chr) + 1);
if(integer1.Contains("+") || integer1.Contains("-") || integer1.Contains("*") || integer1.Contains("/")) integer1_r = Eval(integer.ToString()).ToString();
else integer1_r = integer1;
switch (chr)
{
case '+':
op = (x, y) => x + y;
break;
case '-':
op = (x, y) => x - y;
break;
case '*':
op = (x, y) => x * y;
break;
case '/':
op = (x, y) => x / y;
break;
}
System.Windows.Forms.MessageBox.Show(integer + " " + integer1_r); // for debug
result = op(Convert.ToDouble(integer), Convert.ToDouble(integer1_r));
return result;
}
}
return 0.0;
}
}
推荐阅读
- gradle - 导出显式 Jacoco 它报告给 sonarqube 6.7
- java - 如何在没有登录页面的情况下使用 Shiro 进行 WEB 应用程序
- java - 如何运行一个运行多个 Selenium 类的类?
- android - 显示的启动画面标志质量很差
- javascript - 选择器表单,关于提交如何使用 jQuery 或 JS 将 WordPress 查询变量发送到 URL
- javascript - Node.js 在服务中使用 res 和 req
- sql - ID和PK用什么
- c++builder - 如何将 SQL 查询中的值存储到 ComboBox 组件中?
- typo3 - Typo3 后端图像裁剪/操作工具在从 7 LTS 升级到 8 LTS 后不再工作
- ios - 如何在按钮操作上从 UIView 移动到 UIViewcontroller