将一段表达式中的数字加入c#数组有一段四则表达式 1+2*3-4把1 2 3 4 加如数字数组把 + * - 加入符号数组

来源:学生作业帮助网 编辑:作业帮 时间:2024/05/04 17:35:28
将一段表达式中的数字加入c#数组有一段四则表达式 1+2*3-4把1 2 3 4 加如数字数组把 + * - 加入符号数组

将一段表达式中的数字加入c#数组有一段四则表达式 1+2*3-4把1 2 3 4 加如数字数组把 + * - 加入符号数组
将一段表达式中的数字加入c#数组
有一段四则表达式 1+2*3-4
把1 2 3 4 加如数字数组
把 + * - 加入符号数组

将一段表达式中的数字加入c#数组有一段四则表达式 1+2*3-4把1 2 3 4 加如数字数组把 + * - 加入符号数组
可以循环读取每个字符,然后判断字符是否为运算符,如果不是则把字符连接为字符串,如果是则把连接的字符串存入数组,把运算符存入另一个数组.
private List NumberList = new List();
private List OperatorList = new List();
private void Check(string exp)
{
string numTmp = "";
for (int i = 0; i < exp.Length; i++)
{
string strtmp = exp.Substring(i, 1);
if (strtmp == "+" || strtmp == "-" || strtmp == "*" || strtmp == "/")
{
OperatorList.Add(strtmp);
if (!string.IsNullOrEmpty(numTmp))
{
NumberList.Add((double)numTmp);
numTmp = "";
}
}
else
numTmp += strtmp;
}
}
最后用NumberList.ToArray()和OperatorList.ToArray()就可以转换为数组了.