本帖最后由 oMoWuYan 于 2012-09-02 14:47:32 编辑

解决方案 »

  1.   

    这东西别问我们,你自己知道表示什么意思,那就按照你的规则拆分出来就好了,正则可以拆分,普通字符串indexof+substring都可以
      

  2.   

    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
      

  3.   

    判断前面没有=号的话
    Excel.cell[1,1] = "=" + Excel.cell[1,1]
      

  4.   


    如果string test  =“+5又很-9”;
    应该是报错,实际返回是-4;
    这个应该处理呢?
      

  5.   

    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("格式错误");
                    }
                }
      

  6.   

    定义一个函数专门来处理这个: 
    private string GetStrExpression(string str){
            
                str = str.Trim();
                string[] ops = str.Split('/');
                int op1Int = int.Parse(ops[0].Trim()),
                    op2Int = int.Parse(ops[1].Trim());//获得2个操作数(整型)
                string op2sys = op2Int > 0 ? "+" : "";
                string result = string.Format("{0}{1}{2}={3}", op1Int, op2sys, op2Int, op1Int + op2Int);
                return result;
            }
    测试 string dd=  GetStrExpression("+98/-25");
    结果 dd="98-25=85"