namespace Kmeansgui
{
    public partial class kmeans : Form
    {
        public kmeans()
        {
            InitializeComponent();
        }        private static int k = 2;
        private static int total = 9;
        private static int[] type = new int[total];             //每个点暂时的类
        public static Point[] z = new Point[k];                 //保存新的聚类中心
        public static Point[] z0 = new Point[k];                //保存上一次的聚类中心
        //private static bool flag = true;
        private static int l = 0;          //迭代次数
        int test = 0;        public class Point
        {
            public double human, interfac, computer, user, system,
                         response, time, EPS, survey, trees, graph, minors;
            public Point(double human, double interfac, double computer, double user, double system,
                         double response, double time, double EPS, double survey, double trees,
                         double graph, double minors)
            {
                this.human = human;
                this.interfac = interfac;
                this.computer = computer;
                this.user = user;
                this.system = system;
                this.response = response;
                this.EPS = EPS;
                this.time = time;
                this.survey = survey;
                this.trees = trees;
                this.graph = graph;
                this.minors = minors;
                
            }
           
        }
        public static Point[] p = {
                                    new Point(1,1,1,0,0,0,0,0,0,0,0,0),
                                    new Point(0,0,1,1,1,1,1,0,1,0,0,0),
                                    new Point(0,1,0,1,1,0,0,1,0,0,0,0),
                                    new Point(1,0,0,0,2,0,0,1,0,0,0,0),
                                    new Point(0,0,0,1,0,1,1,0,0,0,0,0), 
                                    new Point(0,0,0,0,0,0,0,0,0,2,0,0), 
                                    new Point(0,0,0,0,0,0,0,0,0,2,2,0),
                                    new Point(0,0,0,0,0,0,0,0,0,2,2,2),
                                    new Point(0,0,0,0,0,0,0,0,2,0,2,2),
                                  };
        public static Point sum = new Point(0,0,0,0,0,0,0,0,0,0,0,0);
        //计算新的聚类中心
        public static Point newCenter(int m)
        {
            int N = 0;
            sum.human = 0;
            sum.interfac = 0;
            sum.response = 0;
            sum.computer = 0;
            sum.user = 0;
            sum.system = 0;
            sum.EPS = 0;
            sum.time = 0;
            sum.survey = 0;
            sum.trees = 0;
            sum.graph = 0;
            sum.minors = 0;
            for (int i = 0; i < total; i++)
            {
                if (type[i] == m)
                {
                    sum.human = p[i].human + sum.human;
                    sum.interfac = p[i].interfac + sum.interfac;
                    sum.response = p[i].response + sum.response;
                    sum.computer = p[i].computer + sum.computer;
                    sum.user = p[i].user + sum.user;
                    sum.system = p[i].system + sum.system;
                    sum.EPS = p[i].EPS + sum.EPS;
                    sum.time = p[i].time + sum.time;
                    sum.survey = p[i].survey + sum.survey;
                    sum.trees = p[i].trees + sum.trees;
                    sum.graph = p[i].graph + sum.graph;
                    sum.minors = p[i].minors + sum.minors;                    N += 1;
                }
            }
            sum.human = sum.human / N;
            sum.interfac = sum.interfac / N;
            sum.response = sum.response / N;
            sum.computer = sum.computer / N;
            sum.user = sum.user / N;
            sum.system = sum.system / N;
            sum.EPS = sum.EPS / N;
            sum.time = sum.time / N;
            sum.survey = sum.survey / N;
            sum.trees = sum.trees / N;
            sum.graph = sum.graph / N;
            sum.minors = sum.minors / N;
            return sum;
        }        //比较两个聚类中心的是否相等
        private static bool compare(Point a, Point b)
        {  }        //欧式距离计算方法
        private static double distance(Point p1, Point p2)
        {。。}        //int temp = 0;//记录p[i]暂时在哪个类中        //进行迭代,对total个样本根据聚类中心进行分类
        private void order()
        {
            int temp = 0 ;//= 0;//记录p[i]暂时在哪个类中
            for (int i = 0; i < total; i++)
            {
                for (int j = 0; j < k; j++)
                {
                    if (distance(p[i], z[temp]) > distance(p[i], z[j]))
                         temp = j;
                }
                type[i] = temp;
            }
        }        private void button1_Click(object sender, EventArgs e)
        {
            //选k个初始聚类中心z[i]     
            for (int i = 0; i < k; i++)
                z[i] = p[i];
            for (int i = 0; i < k; i++)
                z0[i] = p[i]           
            for (int i = 0; i < total; i++)
                type[i] = 0;
         //核心代码                     //while (flag)
            //{
                order();                for (int i = 0; i < k; i++)
                {                    z[i] = newCenter(i);             
                    if (compare(z[i], z0[i]))
                          test = test +1;       // flag = false;
                    else
                        z0[i] = z[i];
                }
                l = l + 1;
                
            //}//while                while (test != k)
                {
                    order();
                    for (int i = 0; i < k; i++)
                    {                        if (compare(z[i], z0[i]))
                            test = test + 1;
                        else
                            z0[i] = z[i];
                    }                }//while
        }
    }
}
运行时会发现所有的文本都被归为第0类了,将order方法中int temp= 0放到for循环外面发现所有文本却全归为1类了,求解!