using System;
using System.Collections.Generic;
using System.Text;
using BestCut.Classes;
using System.Windows.Forms;
using System.Windows;
using System.Text;
using System.Text.RegularExpressions;
using System.Collections;
using System.Data;
namespace BestCut
{
   public class ManagePage : System.Windows.Forms.Form
    {
        private DbAccess _db;
        public DbAccess db
        {

            get
            {
                if (_db == null)
                {
                    _db = new DbAccess();
                }
                return _db;
            }
        }
       public string CheckString(string str)
       {
           return str.Replace("'","''");
       }
       /// <summary>
       /// 检查小数格式
       /// </summary>
       /// <param name="sender"></param>
       /// <param name="e"></param>
       public void KeyUpCheckNum(object sender, KeyEventArgs e)
       {
          // MessageBox.Show("该项不是数据格式!!");
           ((TextBox)sender).Text = Regex.Replace(((TextBox)sender).Text, "[^\\d\\u002E]+", "");
           if (Regex.IsMatch(((TextBox)sender).Text, "[\\d]*[\\u002E]?[\\d]*"))
           {

           }
           else
           {
               MessageBox.Show("该项不是数据格式!!");
               ((TextBox)sender).Focus();

           }
       }
       /// <summary>
       /// 检查整数
       /// </summary>
       /// <param name="sender"></param>
       /// <param name="e"></param>
       public void KeyUpCheckInt(object sender, KeyEventArgs e)
       {
           ((TextBox)sender).Text = Regex.Replace(((TextBox)sender).Text, "[^\\d]*", "");
           if (Regex.IsMatch(((TextBox)sender).Text, "\\d*"))
           {

           }
           else
           {
               MessageBox.Show("该项不是数据格式!!");
               ((TextBox)sender).Focus();

           }
       }
       /// <summary>
       /// 迭代计算
       /// </summary>
       /// <param name="dic"></param>
       /// <param name="sums"></param>
       /// <param name="res"></param>
       /// <returns></returns>
       public Dictionary<string, int> GetResult(Dictionary<string, int> dic,double sums,Dictionary<string,int> res)
       {
           Package p = new Package();
           ArrayList needs = new ArrayList();
           #region 加载测试数据
           try
           {
               foreach (string str in dic.Keys)
               {
                   for (int i = 0; i < dic[str]; i++)
                   {
                       needs.Add(str);
                   }
               }
           }
           catch (Exception ex)
           {

               //Console.WriteLine(ex.Message);
           }

 


           #endregion

           //从大到小排序
           IComparer myComperMethod = new comperOne();
           needs.Sort(myComperMethod);


           //迭代方法
           for (int i = 0; i < needs.Count; )
           {
               string result = string.Empty;
               double temp = double.Parse(needs[i].ToString().Split('-')[0]);
               ArrayList tempResult = new ArrayList();
               tempResult.Add(needs[i]);
               needs.RemoveAt(i);

               for (int j = 0; j < needs.Count; )
               {
                   if (temp + double.Parse(needs[j].ToString().Split('-')[0]) > sums)
                   {
                       j++;
                   }
                   else
                   {
                       temp += double.Parse(needs[j].ToString().Split('-')[0]);
                       tempResult.Add(needs[j]);

                       needs.RemoveAt(j);
                   }
               }

               //记录结果
               for (int k = 0; k < tempResult.Count; k++)
               {
                   result += tempResult[k].ToString() + ",";
               }
               result = result.Trim(',');

               result = p.stringFormat(result);

               if (res.ContainsKey(result))
               {
                   res[result]++;
               }
               else
               {
                   res.Add(result, 1);
               }

             
           }
           return res;
       }
       /// <summary>
       ///
       /// </summary>
       /// <param name="str"></param>
       /// <returns></returns>
       public double Sums(string str)
       {
           string[] temp = str.Split(',');
           double sum= 0;
           try
           {
               for (int i = 0; i < temp.Length; i++)
               {
                   sum += Convert.ToDouble(temp[i].Split('-')[0]);
               }
           }
           catch
           {

               sum = 0;
           }
      
           return sum;
       }
       public DataTable getDataPart(string str)
       {
           Dictionary<string, int> dics = new Dictionary<string, int>();
           string[] temp = str.Split(',');
           foreach (string tempstr in temp)
           {
               if (dics.ContainsKey(tempstr))
               {
                   dics[tempstr]++;
               }
               else
               {
                   dics.Add(tempstr,1);
               }
           }
          
           DataTable dt = new DataTable();
           dt.Columns.Add("Gid");
           dt.Columns.Add("PLength");
           dt.Columns.Add("PCount");
           dt.Columns.Add("P45");
           dt.Columns.Add("P45Both");
           dt.Columns.Add("PWidth");
           int j = 0;
           double d = 0;
           string tempLength = "";
           string temp45 = "";
           string temp45Both = "";
           string tempWidth = "";
         foreach(string tempdic in dics.Keys)
         {
            
             DataRow dr = dt.NewRow();
             dr["Gid"] = (++j).ToString();
             tempLength = tempdic.Split('-')[0];
             temp45 = tempdic.Split('-')[1].ToLower();
             temp45Both = tempdic.Split('-')[2].ToLower();
             tempWidth = tempdic.Split('-')[3];
             double.TryParse(tempLength, out d);
             dr["PLength"] = temp45 == "true" ? (temp45Both == "true" ? (d - 5 - (double.Parse(tempWidth)*2)) : (d - 5 - double.Parse(tempWidth))) : (d - 5); ;
             dr["PCount"] = (dics[tempdic]).ToString();
             dr["P45"] = temp45 == "true"?("是"):("否");
             dr["P45Both"] = temp45Both == "true" ? ("是") : ("否");
             dr["PWidth"] = tempWidth;
             dt.Rows.Add(dr);
         }
          
         return dt;


       }
       /// <summary>
       /// 获取信息
       /// </summary>
       /// <param name="str"></param>
       /// <returns></returns>
       public string getStrDetail(string str)
       {
           string tempLength = "";
           string temp45 = "";
           string temp45Both = "";
           string tempWidth = "";
           double d = 0;
           tempLength = str.Split('-')[0];
           temp45 = str.Split('-')[1].ToLower();//=="true"?("是"):("不是");
           temp45Both = str.Split('-')[2].ToLower();//=="true"?("是"):("不是");
           tempWidth = str.Split('-')[3];
            double.TryParse(tempLength,out d);
            d = d-5;
            d=temp45 == "true" ? (temp45Both == "true" ? (d - 5 - (double.Parse(tempWidth)*2)) : (d - 5 - double.Parse(tempWidth))) : (d - 5);

            return string.Format("{0}mm{1}{2}", d.ToString(), temp45 == "true" ? (temp45Both == "true" ? ("---◢▆◣") : ("--◢▆")) : (""), tempWidth == "0" ? ("") : (string.Format("{0}{1}{2}","--" , tempWidth ,"mm角度宽")));
       }
       /// <summary>
       /// 获取型材信息
       /// </summary>
       /// <param name="str"></param>
       /// <returns></returns>
       public string getDataPartDetail(string str)
       {
           StringBuilder tempsb = new StringBuilder();
           Dictionary<string, int> dics = new Dictionary<string, int>();
           string[] temp = str.Split(',');
           foreach (string tempstr in temp)
           {
               if (dics.ContainsKey(tempstr))
               {
                   dics[tempstr]++;
               }
               else
               {
                   dics.Add(tempstr, 1);
               }
           }

      
           int j = 0;
           double d = 0;
           ArrayList al = new ArrayList();
           foreach (string tempdic in dics.Keys)
           {
               al.Add(new Model(tempdic, dics[tempdic]));
               //tempsb.AppendFormat("{0}个{1}mm\n",dics[tempdic].ToString(),tempdic);
           
           }
           al.Sort(new myCompare());
           foreach (object o in al)
           {
               Model m = (Model)o;
               tempsb.AppendFormat("{0}个{1}\n", m.Sum.ToString(), getStrDetail(m.Lists));
           }

           return tempsb.ToString();


       }
   }
    public class Model
    {
        private string _Lists;
        private double _Sum;
        public string Lists
        {
            get { return _Lists; }
            set { _Lists = value; }
        }
        public double Sum
        {
            get { return _Sum; }
            set { _Sum = value; }
        }
        public Model(string lists, double sum)
        {
            this.Lists = lists;
            this.Sum = sum;
        }
    }
    //排序方法
    public class comper : IComparer
    {
        int IComparer.Compare(object a, object b)
        {
            float flotA = 0, flotB = 0;
            bool isFloat = float.TryParse(a.ToString(), out flotA) && float.TryParse(b.ToString(), out flotB);
            if (!isFloat)
            {
                return -string.Compare(a.ToString(), b.ToString(), false);
            }
            else
            {
                return flotA == flotB ? 0 : (flotA > flotB ? -1 : 1);
            }
        }
    }
    //排序方法
    public class comperOne : IComparer
    {
        int IComparer.Compare(object a, object b)
        {
            float flotA = 0, flotB = 0;
            bool isFloat = float.TryParse(a.ToString().Split('-')[0], out flotA) && float.TryParse(b.ToString().Split('-')[0], out flotB);
            if (!isFloat)
            {
                return -string.Compare(a.ToString(), b.ToString(), false);
            }
            else
            {
                return flotA == flotB ? 0 : (flotA > flotB ? -1 : 1);
            }
        }
    }
    //排序降序方法
    public class comperSort : IComparer
    {
        int IComparer.Compare(object a, object b)
        {
            float flotA = 0, flotB = 0;

            bool isFloat = float.TryParse(((System.Windows.Forms.DataGridViewRow)a).Cells[2].Value.ToString(), out flotA) && float.TryParse(((System.Windows.Forms.DataGridViewRow)b).Cells[2].Value.ToString(), out flotB);
            return (int)(flotA - flotB);
        }
    }

    public class myCompare : IComparer
    {
        int IComparer.Compare(object x, object y)
        {
            return (int)((((Model)y).Sum - ((Model)x).Sum) * 100);
        }

    }
    public class myLenCompare : IComparer
    {
        int IComparer.Compare(object x, object y)
        {
            return x.ToString().Length - y.ToString().Length;
            // return ((Model)x).Lists.ToString().Length - ((Model)y).Lists.ToString().Length;
        }

    }
    public class Package
    {
        public double[] goods;
        public double dmin;
        double tempsum;
        // public List<Model> lists = new List<Model>();
        public ArrayList lists = new ArrayList();
        public ArrayList listsModel = new ArrayList();

        public void Init(double sum, double[] goods)
        {
            tempsum = sum;
            this.goods = goods;
            ArrayList al = new ArrayList();
            foreach (double d in goods)
            {
                al.Add(d);
            }
            al.Sort();
            dmin = (Double)al[0];
        }
        /// <summary>
        /// 用递归算出
        /// </summary>
        /// <param name="puts">目前放入背包的数</param>
        /// <param name="unPuts">可放入背包的数</param>
        /// <param name="sum">背包还剩的容量</param>
        public void ShowComposes(List<double> puts, double[] unPuts, double sum)
        {
            //输出当前放入背包符合条件的所有数
            bool exist = false;
            double sums = 0;
            string temp = "";

            foreach (double d in puts)
            {
                temp += d.ToString() + ",";
                //System.Console.Write(d + " ");
                sums += d;
                exist = true;
            }
            if (exist)
            {
                if (sums + dmin > tempsum)
                {
                    try
                    {
                        lists.Add(new Model(temp.Trim(','), sums));

                    }
                    catch (Exception ex)
                    {

                        Console.WriteLine(ex.Message);
                    }

                }
                System.Console.Write("=" + sums);
                System.Console.WriteLine();
            }

            //在可放入背包的数字中,选择可放的数并放进背包
            foreach (double d in unPuts)
            {
                if (d < sum)
                {
                    List<double> newPuts = puts.GetRange(0, puts.Count);//.ToList<double>();
                    newPuts.Add(d);
                    ShowComposes(newPuts, goods, sum - d);
                }
            }
            // System.Console.WriteLine(dmin.ToString());
        }
        /// <summary>
        ///
        /// </summary>
        public void getBest()
        {
            lists.Sort(new myCompare());
            foreach (Model m in lists)
            {

                bool state = false;
                foreach (double dtemp in goods)
                {
                    if (dtemp >= (tempsum / 2 - 1))
                    {
                        state = m.Lists.IndexOf(dtemp.ToString()) > 0;
                        if (state)
                            break;
                    }
                }
                if (state)
                {
                    listsModel.Add(new Model(m.Lists, m.Sum));
                    //  Console.WriteLine("zun={0}----sum={1}", m.Lists, m.Sum.ToString());
                }


            }
        }
        public string stringFormat(string t1)
        {
            //
            string[] tempNum = t1.Split(',');
            //ArrayList tempal = new ArrayList();
            //foreach (string t in tempNum)
            //{
            //    tempal.Add(t);
            //}
            //tempal.Sort();
            t1 = "";
            Array.Sort(tempNum);
            foreach (string t in tempNum)
            {
                t1 += t + ",";
            }
            return t1.Trim(',');
        }
        public Dictionary<ArrayList, string> sortDistinct()
        {
            ArrayList tempAl = new ArrayList();
            ArrayList al = new ArrayList();

            foreach (object o in listsModel)
            {
                Model m = (Model)o;
                m.Lists = stringFormat(m.Lists);
                tempAl.Add(m);


            }


            foreach (object o in tempAl)
            {
                if (!al.Contains(((Model)o).Lists))
                {
                    al.Add(((Model)o).Lists);
                }

            }
            al.Sort(new myLenCompare());
            ArrayList alo = new ArrayList();
            ArrayList tempal = null;
            Dictionary<ArrayList, string> dicCon = new Dictionary<ArrayList, string>();
            foreach (object o in al)
            {
                // Console.WriteLine(o.ToString() + "----");
                tempal = new ArrayList();
                string[] array = o.ToString().Split(',');
                Dictionary<string, int> dicCount = new Dictionary<string, int>();
                foreach (string s in array)
                {
                    if (!dicCount.ContainsKey(s))
                        dicCount.Add(s, 1);
                    else
                        dicCount[s]++;
                }
                foreach (string s in dicCount.Keys)
                {
                    tempal.Add(new Model(s, dicCount[s]));
                }

                dicCon.Add(tempal, o.ToString());

            }

            return dicCon;

        }
    }
}

posted on 2009-03-24 11:22 sanmao 阅读(215) 评论(0)  编辑  收藏

只有注册用户登录后才能发表评论。


网站导航:
 

常用链接

留言簿(5)

随笔分类

随笔档案

搜索

  •  

最新评论

阅读排行榜

评论排行榜