平均值和标准差

时间:2018-04-18 00:10:50

标签: c# standards mean deviation

我试图使用Windows窗体在visual studio上制作均值和标准差计算器。它是一个类,我必须使用Windows窗体,你必须能够手动输入数据或选择.txt文件与其中的数据。

我很难解释我需要做什么,因为我对此很陌生。基本上我需要button1_Click做什么,但对button2_Click做同样的事情。问题是,它是读取文件而不是从用户那里获取输入。它存储(不确定这是否正确的单词)列表中的值而不是我认为的数组。我不知道我是否可以将其转换为数组或将值作为数组或列表添加到列表中。我真的输了!

以下是我的教授对这项任务的准确措辞:

"使用C#编写一个程序,它将找到许多数据点的平均值和标准差。该程序应允许用户手动或通过文本文件输入数据。"

任何帮助都会很棒我是新手,我的老师几乎没有帮助,让我们去网上寻找答案。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace BsweeneyCsharp3_1_
{
    public partial class Form1 : Form
    {
        List<double> values;

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            values = new List<double>();
        }

        void ShowValues()
        {
            listBox1.Items.Clear();

            for (int i = 0; i < values.Count; i++)
                listBox1.Items.Add(values[i]);
        }

        private void button1_Click(object sender, EventArgs e)
        {

            double value = 0.00;
            double sum = 0.00, sumSquares = 0.00, squareSums;
            double stdDev = 0.00, mean = 0.00;

            if (textBox1.Text.Length == 0)
            {
                MessageBox.Show("You must enter a value.", "Standard Deviation");
                return;
            }

            try
            {               
                value = double.Parse(textBox1.Text);              
                values.Add(value);
                ShowValues();
                textBox1.Text = "";
                textBox1.Focus();
            }
            catch (FormatException)
            {
                MessageBox.Show("The value you entered is invalid.",
                                "");
            }

            for (int i = 0; i < values.Count; i++)
            {
                sum += values[i];
            }
            mean = sum / values.Count;
            squareSums = sum * sum;

            for (int i = 0; i < values.Count; i++)
                sumSquares += (values[i] * values[i]);

            double numerator = values.Count * sumSquares - squareSums;
            double denominator = values.Count * (values.Count - 1);
            stdDev = Math.Sqrt(numerator / denominator);

            textBox2.Text = mean.ToString();

            textBox3.Text = stdDev.ToString("F");

        }

        private void button2_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Filter = "TXT|*.txt";
            double mean = 0.00, stdDev = 0.00, sum = 0.00;
            double sumSquares = 0.00, squareSums = 0.00;
            int counter = 0;

            if (ofd.ShowDialog() == DialogResult.OK)
            {
                textBox4.Text = ofd.FileName;
            }

            string line;
            System.IO.StreamReader file = new System.IO.StreamReader(textBox4.Text);
            List<string> list = new List<string>();           

            while ((line = file.ReadLine()) != null)
            {                
                listBox2.Items.Add(line);
                var dbl = Convert.ToDouble(line);

                sum += dbl;                
                counter++;
            }
            if (counter > 0)
            {
                mean = sum / counter;
                squareSums += sum * sum;
            }
            if(counter > 0)
            {

                sumSquares += Math.Pow((sum - mean), 2);

                double numerator = counter * sumSquares - squareSums;
                double denominator = counter * (counter - 1);
                stdDev = Math.Sqrt(numerator / denominator);

                textBox2.Text = mean.ToString();

                textBox3.Text = numerator.ToString();
            }
        }
    }

}

1 个答案:

答案 0 :(得分:0)

我尽力解释你所写的内容。我考虑到你是一个新的程序员。我避免了重大的重构,包括添加一个非常需要的方法。开始学习methods /函数,它们可以让您重复使用代码。一种方法可以防止您遇到的问题。问题是你以两种不同的方式计算同样的事情。

你说“手动”方法(不是文件阅读)正常工作,所以我把它作为事实的来源。

相关代码更改为button2_Click

    if (counter > 0)//you don't need the same check 2x
    {
        mean = sum / counter;
        for (int i = 0; i < listBox2.Items.Count; i++)
            sumSquares += (Convert.ToDouble(listBox2.Items[i]) * Convert.ToDouble(listBox2.Items[i]));
        squareSums += sum * sum;

        //sumSquares += Math.Pow((sum - mean), 2);//You changed the formula again here

        double numerator = counter * sumSquares - squareSums;
        double denominator = counter * (counter - 1);
        stdDev = Math.Sqrt(numerator / denominator);

        textBox2.Text = mean.ToString();

        //textBox3.Text = numerator.ToString();//You changed how you did things again
        textBox3.Text = stdDev.ToString("F");
    }

仅供参考,我没有检查你的数学。