1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
| #include<bits/stdc++.h> using namespace std;
typedef struct Node{ double data; Node*next; }*DataList;
void InitList(DataList&list) { list= new Node; list->next=NULL; cout<<"【准备就绪】"<<endl; }
void GetData(DataList&list,int DataSize) { DataList prev=list;
for(int i=0;i<DataSize;i++) { double newdata; cin>>newdata;
while(prev->next) { prev=prev->next; }
DataList newnode=new Node; newnode->data=newdata; newnode->next=NULL; prev->next=newnode; } }
void ListPrint(DataList list) { cout<<"-------------------------------------------------------------------------"<<endl; cout<<"①【样本数据如下:】"<<endl; DataList p=list->next; while(p) { cout<<p->data<<" "; p=p->next; } cout<<endl;
}
double Average(DataList&list,int DataSize) { double sum=0.0; DataList p=list->next; while(p) { sum+=p->data; p=p->next; } cout<<"②【算数平均数计算如下:】"<<endl; cout<<sum*1.0/DataSize<<endl; return sum*1.0/DataSize; }
double StandardDeviation(DataList&list,int DataSize,double ave) { double SquareSum=0.0; DataList p=list->next; while(p) { SquareSum+=((p->data)-ave)*((p->data)-ave); p=p->next; } double result = sqrt(SquareSum*1.0/(DataSize-1)); cout<<"③【标准误差计算数值如下:】"<<endl; cout<< result<<endl; return result; }
void ConfirmData(DataList list,double ave,double sd) { cout<<"④【坏值检验:】"<<endl; double left,right; left=ave-3*sd; right=ave+3*sd;
int flag=1; DataList p=list->next; while(p) { if(p->data>right||p->data<left) { flag=0; } p=p->next; }
if(flag==1) cout<<"3σ区间为 ["<<left<<","<<right<<"] 经检验数据,无坏值。"<<endl; else cout<<"有坏值,终止交易!"<<endl; }
double UncertaintyA(DataList&list,int DataSize,double sd) { cout<<"⑤【计算A类不确定度:】"<<endl; cout<<"测量次数为"<<DataSize<<"次,自由度为"<<DataSize-1<<"次。P=0.683对应的tp=1.32。"<<endl; double A =(1.32*sd)/sqrt(3); cout<<"μA="<<A<<endl; return A; }
double UncertaintyB() { cout<<"⑥【计算B类不确定度:】"<<endl; cout<<"▲ins默认为0.05,此处可修改。"<<endl; double B=0.05/sqrt(3); cout<<"μB="<<B<<endl; return B; }
double TotalUncertainty(double A,double B) { cout<<"⑦【计算总不确定度:】"<<endl; double total; total=2*sqrt(A*A+B*B); cout<<"μp(2σ)="<<total<<" (P=0.683)"<<endl; return total; }
void Finally(double total,double ave) { cout<<"⑧【结果表达式为:】"<<endl; cout<<ave<<"±"<<total<<endl; cout<<"⑨【百分差为:】"<<endl; cout<<"E="<<total/ave<<endl; cout<<"-------------------------------------------------------------------------"<<endl; cout<<"【运算完成】"<<endl; return ; } int main() { DataList list; InitList(list);
cout<<"请输入样本大小"<<endl; int DataSize; cin>>DataSize; cout<<"请输入样本数据,中间用空格隔开"<<endl;
GetData(list,DataSize);
ListPrint(list);
double ave; ave = Average(list,DataSize);
double sd; sd = StandardDeviation(list,DataSize,ave);
ConfirmData(list,ave,sd);
double A; A=UncertaintyA(list,DataSize,sd);
double B; B=UncertaintyB();
double total; total=TotalUncertainty(A,B);
Finally(total,ave); return 0; }
|