0

我有如下输入:

"10+18+12+13" 
"10+18-12+13" 
"2-5"

例如数字后跟一个"+""-"
我创建的类MathOper

public class MathOper
{ 
    public int Num { get; set; }
    public string Oper { get; set; } //this display the number will be operated.
}

"10+18-12+13"我想返回MathOper 列表,如下所示

new MathOper(){Num=10,"+"}
new MathOper(){Num=18,"+"}
new MathOper(){Num=12,"-"}
new MathOper(){Num=13,"+"}

我试图通过这种方式编码:

public class MathOperCreator
{
    private readonly string _mathString;//8+2-3
    public MathOperCreator(string mathString)
    {
        this._mathString = mathString.Trim();
    }
    public List<MathOper> Create()
    {
        var lMo = new List<MathOper>();
        int l = this._mathString.Length;//5
        for (int i = 0; i < l; i = i + 2)
        {
            char n = _mathString[i];
            int n1 = int.Parse(n.ToString(CultureInfo.InvariantCulture)); 
            string o1 = "+";
            if (i > 0)
            {
                o1 = _mathString[i - 1].ToString(CultureInfo.InvariantCulture);
            }

            var mo = new MathOper { Num = n1, Oper = o1 };
            lMo.Add(mo);
        }
        return lMo;
    }
}

我发现它只适用于带有一个字符的数字,如果数字是两个字符,例如 18 ,它就不起作用。请建议如何实现描述的功能?

4

3 回答 3

2

这是一个经过测试的解决方案

//Model class
public class MathOperation
{
    public Int32 Num { get; set; }
    public String Operation { get; set; }
}

   String testData = "10+18+12-13";
   String[] GetNumbers = testData.Split(new Char[] { '+', '-' });
   String[] GetOperators = Regex.Split(testData, "[0-9]+");

   //remove empty entries in operator
   GetOperators = GetOperators.Where(x => !string.IsNullOrEmpty(x)).ToArray();

   List<MathOperation> list = new List<MathOperation>();
   MathOperation mathOperation = new MathOperation();
    for (int i = 0; i < GetNumbers.Count(); i++)
{
       mathOperation.Num = Convert.ToInt32(GetNumbers[i]);               
       mathOperation.Operation = (i>GetOperators.Length)? GetOperators[i] : null;
       list.Add(mathOperation);
}

这将给出一个类似的列表

{Num=10,"+"}
{Num=18,"+"}
{Num=12,"-"}
{Num=13,"null"} //as in my test string there is no char after 13

如果你不想要一个 null 总是一个 + 那么

mathOperation.Operation = (i>GetOperators.Length)? GetOperators[i] : "+";

那么这将给出一个类似的列表

{Num=10,"+"}
{Num=18,"+"}
{Num=12,"-"}
{Num=13,"+"} //as in my test string there is no char after 13
于 2013-10-18T06:56:27.180 回答
0

试试这个,我认为它可以按照您想要的方式工作:(易于理解的解决方案,但不是最佳的)

public class MathOper
    {
        public int Num { get; set; }
        public string Oper { get; set; } //this display the number will be operated.
    }

    public class MathOperCreator
    {
        public readonly string _mathString;//8+2-3

        public MathOperCreator(string mathString)
        {
            this._mathString = mathString.Trim();
        }
        public List<MathOper> Create()
        {
            var lMo = new List<MathOper>();
            int l = this._mathString.Length;//5
            string _mathStringTemp;

            char[] charArr = _mathString.ToCharArray();

            if (charArr[0] != '+' || charArr[0] != '-')
            {
                _mathStringTemp = "+"+_mathString;
            }               else
            {
                _mathStringTemp = _mathString;
            }

            char[] delimitersNumb = new char[] { '+', '-' };
            string[] numbers = _mathStringTemp.Split(delimitersNumb,
                     StringSplitOptions.RemoveEmptyEntries);


            string[] operators = new string[numbers.Length];
            int count = 0;

            foreach (char c in _mathStringTemp)
            {
                if (c == '+' || c == '-')
                {
                    operators[count] = c.ToString();            
                    count++;
                }
            }

            for(int i=0; i < numbers.Length; i++)
            {

                lMo.Add(new MathOper(){Num = int.Parse(numbers[i]), Oper = operators[i]});
                Console.WriteLine(operators[i]+" "+numbers[i]);
            }

            return lMo;
        }
    }
于 2013-10-18T06:25:30.383 回答
0

这对我有用。

        //you can change in to MathOper
        List<Tuple<int, char>> result = new List<Tuple<int, char>>(); 
        string _mathString = "2-2+10-13"; //Test
        char sign = '-';
        if (_mathString[0] != '-') //checking the first sign
        {
            sign = '+';
        }
        while (_mathString.Length > 0)
        {
            int nextPl = _mathString.IndexOf('+');
            int nextMn = _mathString.IndexOf('-');

            if (nextPl == -1 && nextMn == -1) //condition when _mathString contains only number
            {
                result.Add(new Tuple<int, char>(int.Parse(_mathString), sign));
                break;
            }
            else
            {
                //getting the end position of first number
                int end = nextPl == -1 ? nextMn : (nextMn == -1 ? nextPl : (Math.Min(nextPl, nextMn)));

                //retrieving first number
                result.Add(new Tuple<int, char>(int.Parse(_mathString.Substring(0, end)), sign));
                _mathString = _mathString.Remove(0, end);

                //retrieving next sign
                sign = _mathString[0];
                _mathString = _mathString.Remove(0, 1);
            }
        }
于 2013-10-18T06:29:39.033 回答