tokisaki-galaxy  

Calc-for-Average

用于计算多个数字总和,平均值,方差的MFC程序

使用方法

把数据输入到框里,允许小数输入,使用空格分开

源码及下载

项目Github地址
核心代码如下

UpdateData(TRUE);
string stringdata = CT2A(m_input.GetBuffer());
char* strdata=(char*)stringdata.c_str();
char* token, * next_token = NULL;
char seps[] = " ";
long double sum=0,e=0,s=0;
vector<long double> data;

token = strtok_s(strdata, seps, &next_token);

while (token != NULL) {
	if (token != NULL)
	{
		data.push_back(stold(token));
		token = strtok_s(NULL, seps, &next_token);
	}
}

for (size_t i = 0; i <= data.size(); i++) //求和
	sum += data[i];
    e = sum / data.size(); //求平均值

for (size_t i = 0; i <= data.size(); i++) { //求方差
	if (i != data.size() - 1)
	s += (data[i] - e) * (data[i] - e);
}
s = s / data.size();

string sum_ret = to_string(sum);
string e_ret = to_string(e);
string s_ret = to_string(s);

m_sum = sum_ret.c_str();
m_avg = e_ret.c_str();
m_var = s_ret.c_str();

UpdateData(FALSE);
posted on 2023-12-22 16:25  Tokisaki_Galaxy  阅读(16)  评论(0编辑  收藏  举报