##数据计算出现了-nan(ind)
#希望通过代码实现物理实验中不确定度的计算
#但输入要用字符串形式,以下是我的代码
#include <iostream>
using namespace std;
#include<string>
int main()
{
string a, part[50];
double n[50], ping,sum=0;
int num = 0;//数据个数
double bei, biao, bu, t;
cin >> a;
for (int i = 0; i <= a.length(); i++) {//字符串分成小字符串
int n = 0, x = 0;
if (a[i] == ' ') {
x = 0;
n++;
num++;
}
else {
part[n][x] = a[i];
}
}
for (int i = 0; i<num; i++) {//字符串转为double存在数组中
n[i] = stod(part[i]);
}
for (int i = 0; i < num; i++) {//计算算术平均值
sum += n[i];
ping = sum / num;
}
double s = 0;
for (int i = 0; i < num; ++i) {//根据贝塞尔公式
s += (n[i]-ping)*(n[i]-ping) / (num - 1);
}
bei = sqrt(s);
biao = bei / ((double)sqrt(num));//标准偏差
switch (num - 1) {//判断t值
case 1:
t = 1.84;
break;
case 2:
t = 1.32;
break;
case 3:
t = 1.20;
break;
case 4:
t = 1.14;
break;
case 5:
t = 1.11;
break;
case 6:
t = 1.09;
break;
case 7:
t = 1.08;
break;
case 8:
t = 1.07;
break;
case 10:
t = 1.05;
break;
case 15:
t = 1.04;
break;
case 20:
t = 1.03;
break;
case 30:
t = 1.02;
break;
case 40:
t = 1.01;
break;
default:
t = 1.00;
break;
}
bu = t * biao;//不确定度
cout<< bei << ' ' << biao << ' ' << bu;
}
#期望输出结果:几个小数,无精确度要求,原始格式即可
#实际输出结果:0 -nan(ind) -nan(ind)
##另外代码中的最后一行的cout报出了“‘cout’不明确”的错误