162 字
1 分钟
Average and Variance Calculator
Calc-for-Average
An MFC program for calculating the sum, average, and variance of multiple numbers.
Usage
Enter the data into the box. Decimal input is allowed; separate values with spaces.
Source Code & Download
Project GitHub address. The core code is as follows:
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++) //Calculate the sum sum += data[i]; e = sum / data.size(); //Calculate the average
for (size_t i = 0; i <= data.size(); i++) { //Calculate the variance 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); Average and Variance Calculator
https://tski.uk/blog/en/cpp-project-calc-for-average/