using System; using System.Collections.Generic; using System.Linq; using System.Text;namespace ConsoleApplication1 { class Program { static void Main(string[] args) { double[] n = new double[100]; int i = 0; string a = "123+32-59+1*44-222/111"; char[] m = { '+', ' ', '-', '*', '/' }; foreach (string r in a.Split(m)) { n[i] = Convert.ToDouble(r);
i++; } char[] p = new char[100]; for (int j = 0; j < a.Length; j++) { if ((a[j] == '+') || (a[j] == '-') || (a[j] == '*') || (a[j] == '/')) p[j] = a[j];
弄了一个用正则来匹配的,比较容易! string str = "123+32-59+1*44-222/111"; List<double> list = new List<double>(); foreach(Match num in Regex.Matches(str,@"\d+")) list.Add(double.Parse(num.Value)); double[] nums = list.ToArray(); List<char> chList = new List<char>(); foreach( Match l in Regex.Matches(str,@"[-+*/]")) chList.Add(char.Parse(l.Value)); char[] ch = chList.ToArray();
string s = "123.2+32.5-59.7+1*44-222/111"; List<double> numList = new List<double>(); char[] operators; double[] num; operators = Regex.Replace(s, @"[\d]+(\.[\d]+)?", m => { numList.Add(Convert.ToDouble(m.Value)); return string.Empty; }).ToCharArray(); num = numList.ToArray();
稍微修改下,可以把输入中可能有的空格也过滤掉 string s = "123.2 + 32.5 - 59.7 + 1*44 - 222 / 111"; List<double> numList = new List<double>(); char[] operators; double[] num; operators = Regex.Replace(s, @"[\d]+(\.[\d]+)?", m => { numList.Add(Convert.ToDouble(m.Value)); return string.Empty; }).Replace(" ",string.Empty).ToCharArray(); num = numList.ToArray();
using System.Collections.Generic;
using System.Linq;
using System.Text;namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
double[] n = new double[100];
int i = 0;
string a = "123+32-59+1*44-222/111";
char[] m = { '+', ' ', '-', '*', '/' };
foreach (string r in a.Split(m))
{
n[i] = Convert.ToDouble(r);
i++; } char[] p = new char[100];
for (int j = 0; j < a.Length; j++)
{
if ((a[j] == '+') || (a[j] == '-') || (a[j] == '*') || (a[j] == '/'))
p[j] = a[j];
}
}
}
}
你的 str="123+32-59+1*44-222/111";
则拆分后的存在字符数组中 string [] str1=str.split(new char []{‘+’,‘-’,‘*’,‘-’,‘/’});
弄了一个用正则来匹配的,比较容易!
string str = "123+32-59+1*44-222/111"; List<double> list = new List<double>();
foreach(Match num in Regex.Matches(str,@"\d+"))
list.Add(double.Parse(num.Value)); double[] nums = list.ToArray(); List<char> chList = new List<char>(); foreach( Match l in Regex.Matches(str,@"[-+*/]"))
chList.Add(char.Parse(l.Value)); char[] ch = chList.ToArray();
string s = "123.2+32.5-59.7+1*44-222/111";
List<double> numList = new List<double>(); char[] operators;
double[] num; operators = Regex.Replace(s, @"[\d]+(\.[\d]+)?", m => { numList.Add(Convert.ToDouble(m.Value)); return string.Empty; }).ToCharArray();
num = numList.ToArray();
List<double> numList = new List<double>(); char[] operators;
double[] num; operators = Regex.Replace(s, @"[\d]+(\.[\d]+)?", m => { numList.Add(Convert.ToDouble(m.Value)); return string.Empty; }).Replace(" ",string.Empty).ToCharArray();
num = numList.ToArray();
List<String> stringarray=new List<String>(); //字符串数组string tempstr="";
int StartIndex=0;
int EndIndex=0;
bool isnum=true;
for(int i=0;i<str.length;i++)
{
EndIndex=i;
if(Char.IsNumber(str[i]) ^ isnum)
{
tempstr=str.Substring(StartIndex,EndIndex-StartIndex);
if(isnum)
{
floatarray.Add(float.Pharse(tempstr));
isnum=false;
}
else
{
stringarray.Add(tempstr);
isnum=true;
}
StartIndex=i;
}
}//别忘了最后还有一个
tempstr=str.Substring(StartIndex,EndIndex-StartIndex+1);
if(isnum)
{
floatarray.Add(float.Pharse(tempstr));
}
else
{
stringarray.Add(tempstr)
}//这样应该就能分开了。
//你没输入的运算符号不会被多余保存。
//在回复里现写的,没经过验证,仅供参考。hoho~~~
其中的 if(Char.IsNumber(str[i]) ^ isnum)考虑改成
if((Char.IsNumber(str[i]) ||str[i]=='.') ^ isnum)