均值和标准差
mean and standard deviation
我正在尝试使用 windows 形式在 visual studio 上制作均值和标准差计算器。它用于 class,我必须使用 windows 表格,您必须能够手动输入数据或 select .txt 文件以及其中的数据。
很难解释我需要做什么,因为我是新手。基本上我需要 button1_Click 做什么,但对 button2_Click 做同样的事情。问题是,它读取文件而不是从用户那里获取输入。它将值存储(不确定这是否是正确的词)列表而不是我认为的数组。我不知道我是否可以将它转换为数组或将值作为数组或什么添加到列表中。我真的迷路了!
这是我的教授对这项作业的准确措辞:
"Write a program, using C#, that will find the mean and standard deviation of a number of data points. The program should allow the user to either enter data manually or via a text file."
如果有任何帮助,我将不胜感激。我是新手,我的老师几乎没有提供任何帮助,让我们在互联网上搜索答案。
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();
}
}
}
}
我尽力解读你写的内容。我考虑到你是一个新程序员。我避免了主要的重构,包括添加一个非常需要的方法。开始学习 methods/functions,它可以让你重用代码。一种方法可以防止您在这里遇到的问题。问题是您正在以两种不同的方式计算同一件事。
您说 "manual" 方法(不是文件读取)工作正常,所以我将其作为事实来源。
相关代码修改为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");
}
仅供参考,我没有检查你的数学。
我正在尝试使用 windows 形式在 visual studio 上制作均值和标准差计算器。它用于 class,我必须使用 windows 表格,您必须能够手动输入数据或 select .txt 文件以及其中的数据。
很难解释我需要做什么,因为我是新手。基本上我需要 button1_Click 做什么,但对 button2_Click 做同样的事情。问题是,它读取文件而不是从用户那里获取输入。它将值存储(不确定这是否是正确的词)列表而不是我认为的数组。我不知道我是否可以将它转换为数组或将值作为数组或什么添加到列表中。我真的迷路了!
这是我的教授对这项作业的准确措辞:
"Write a program, using C#, that will find the mean and standard deviation of a number of data points. The program should allow the user to either enter data manually or via a text file."
如果有任何帮助,我将不胜感激。我是新手,我的老师几乎没有提供任何帮助,让我们在互联网上搜索答案。
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();
}
}
}
}
我尽力解读你写的内容。我考虑到你是一个新程序员。我避免了主要的重构,包括添加一个非常需要的方法。开始学习 methods/functions,它可以让你重用代码。一种方法可以防止您在这里遇到的问题。问题是您正在以两种不同的方式计算同一件事。
您说 "manual" 方法(不是文件读取)工作正常,所以我将其作为事实来源。
相关代码修改为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");
}
仅供参考,我没有检查你的数学。