Beispiel #1
0
        public void button1_Click(object sender, EventArgs e)
        {
            //List<Student> list = new List<Student>();
            if (textBox1.Text.Length == 0)
            {
                MessageBox.Show("请输入学生姓名");
                return;
            }

            try
            {
                if (float.Parse(textBox2.Text) < 0 || float.Parse(textBox2.Text) > 100)
                {
                    MessageBox.Show("请输入0~100的分数");
                    return;
                }
                else
                {
                    Student s = new Student();
                    s.addInfo(textBox1.Text, textBox2.Text);
                    s.outputToListView(listView1);
                    list.Add(s);
                }
                string aver = list.average(listView1).ToString ();         //求平均值
                textBox4.Text = aver;                             //显示平均值
            }
            catch (FormatException ee)
            {
                MessageBox.Show("请输入0~100的分数!");
            }
            textBox1.Clear();
            textBox2.Clear();
            MessageBox.Show(list.Count().ToString());
        }
Beispiel #2
0
        private void button2_Click_1(object sender, EventArgs e)
        {
            listView1.Items.Clear();
            list.Clear();
            OpenFileDialog lwtxt = new OpenFileDialog();
            lwtxt.Filter = "文本文件(*.txt)|*.txt";

            if (lwtxt.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                using (StreamReader sr = new StreamReader(lwtxt.FileName))
                {
                    string s = sr.ReadToEnd();
                    string[] lines = s.Split(new char[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries);
                    if (lines.Count() == 0)
                    {
                        MessageBox.Show("请选择保存学生信息的txt文件");
                        return;
                    }

                    foreach (var i in lines)
                    {
                        string[] data = i.Split(new char[] { '\t' }, StringSplitOptions.RemoveEmptyEntries);
                        try
                        {

                            if(float.Parse(data[1]) >= 0 && float.Parse(data[1]) <= 100 )
                                listView1.Items.Add(new ListViewItem(data));

                        }
                        catch (Exception e1)
                        {
                            MessageBox.Show("请选择保存学生信息的txt文件");
                            return;
                        }

                    }
                }
                int t = listView1.Items.Count;
                float sum1 = 0;
                int I = 0;
                float Average = 0;

                for (; I < t; I++)
                {
                    sum1 += Convert.ToInt32(listView1.Items[I].SubItems[1].Text);

                } Average = sum1 / I;
                string show = string.Format("{0}", Average);
                textBox4.Text = show;
                for (n = 0; n < listView1.Items.Count; n++)
                {
                    Student stu = new Student();

                    //sum += float.Parse(lv.Items[n].SubItems[1].Text);
                    stu.addInfo(listView1.Items[n].SubItems[0].Text, listView1.Items[n].SubItems[1].Text);
                    list.Add(stu);

                }

            }
        }