📄 dataprocess.cpp
字号:
/*********************************************************/
/*功能:实验数据的简单处理
/*********************************************************/
#include <iostream>
using namespace std;
#include <math.h>
struct result{
double xj;
double xb;
double xjb;
double vm;
};
double ch[200]={
0,0,0,0,0,1.65,1.73,1.79,1.86,1.92,
1.96,2.00,2.04,2.07,2.10,2.13,2.16,2.18,2.20,2.22,
2.24,2.26,2.28,2.30,2.32,2.33,2.34,2.35,2.37,2.38,
2.39,2.40,2.41,2.42,2.43,2.45,2.46,2.47,2.48,2.49,
2.50,2.51,2.52,25.3,2.54,2.54,2.55,2.56,2.56,2.57,
2.58,2.58,2.59,2.59,2.60,2.61,2.61,2.62,2.62,2.63,
2.64,2.64,2.64,2.65,2.65,2.66,2.66,2.67,2.67,2.68,
2.68,2.69,2.69,2.70,2.70,2.71,2.71,2.72,2.72,2.73,
2.74,2.74,2.74,2.75,2.75,2.75,2.76,2.76,2.76,2.77,
2.77,2.77,2.78,2.78,2.78,2.79,2.79,2.80,2.80,2.80,
2.81,2.81,2.81,2.81,2.81,2.81,2.81,2.81,2.81,2.81,
2.83,2.84,2.84,2.84,2.84,2.84,2.84,2.84,2.84,2.84,
2.86,2.86,2.86,2.86,2.86,2.86,2.86,2.86,2.86,2.86,
2.88,2.88,2.88,2.88,2.88,2.88,2.88,2.88,2.88,2.88,
2.90,2.90,2.90,2.90,2.90,2.90,2.90,2.90,2.90,2.90,
2.92,2.92,2.92,2.92,2.92,2.92,2.92,2.92,2.92,2.92,
2.96,2.96,2.96,2.96,2.96,2.96,2.96,2.96,2.96,2.96,
2.98,2.98,2.98,2.98,2.98,2.98,2.98,2.98,2.98,2.98,
3.00,3.00,3.00,3.00,3.00,3.00,3.00,3.00,3.00,3.00,
3.02,3.02,3.02,3.02,3.02,3.02,3.02,3.02,3.02,3.02
};
int prepare(int N,double* px,double* pv,result* resultx);//返回值用于控制检测坏值的循环
void abnormal(double*px,int k,int* pn );//剔除坏值子函数
void errorcheck(int N,double* pv,struct result* resultx);
int circleerror(int N,double* pv,double xb);
int ljerror(int N,double* pv,double vm);
void print1(int k,double*px);
void print2(double xj,double xb);
void print3(double xj,double xb);
void print4(double xj,double xb,double xjb);
/*******************主函数*********************/
void main()
{
//变量定以及初始化
int N;
int* pn=&N;
int abflag=1;
//int erflag=0;
//int pflag=1;
struct result result1;
struct result* resultx=&result1;
resultx->xj=0;
resultx->xb=0;
resultx->xjb=0;
resultx->vm=0;
cout<<"请输入您要处理的数据的个数:";
cin>>N;
if(N<5)
{ cout<<"您要输入的数据个数太少,处理的结果可能很不准确,";
cout<<"数据个数至少在5各以上才能获得比较满意的处理结果,";
cout<<"请重新输入您要处理的数据个数:";
cin>>N;
}
// 动态分配内存
double* x;
x=new double[N];
double* v;
v=new double[N];
//数据输入
cout<<"开始输入数据:"<<endl;
for (int i=0;i<N;i++)
{
cout<<"x["<<i<<"]=";
cin>>x[i];
}
//做好剔除坏值的准备工作
while(abflag)
{
abflag=prepare(N,x,v,resultx);
if(abflag>0)
{
abnormal(x,abflag,pn);
}
}
cout<<"坏值剔除完毕!剩余数据"<<N<<"个"<<endl;
cout<<"下面进行误差分析:"<<endl;
errorcheck( N, v,resultx);
//释放内存空间
delete []x;
delete []v;
}
/*****************子函数****************************/
//数据初步处理子函数
int prepare(int N,double* px,double*pv,result* resultx)
{
double temp1=0;
double temp2=0;
double temp3=0;
//计算均值
for(int i=0;i<N;i++)
{
temp1+=px[i];
}
temp1=temp1/N;
resultx->xj=temp1;
//获得最大残差
for(int j=0;j<N;j++)
{
pv[j]=px[j]-temp1;
}
temp2=abs(pv[0]);
for(int m=1;m<N;m++)
{
if(abs(pv[m])>temp2)
temp2=abs(pv[m]);
}
resultx->vm=temp2;
//计算标准差
for(int n=0;n<N;n++)
{
temp3+=px[n]*px[n];
}
temp3=sqrt((temp3-N*temp1*temp1)/(N-1));
resultx->xb=temp3;
double temp4=ch[N-1];
for(int l=0;l<N;l++)
{
if(abs(px[l]-temp1)>temp4*temp3)
return (l+1);
}
return 0;
}
//剔除坏值子函数
void abnormal(double*px,int k,int* pn)
{
cout<<"利用肖维纳剔除坏值:";
print1(k,px);
for(int i=k-1;i<*pn-1;i++)
{
px[i]=px[i+1];
}
*pn=*pn-1;
}
//输出剔除坏值的信息
void print1(int k,double*px)
{
cout<<"第"<<k<<"个数据"<<"x["<<k-1<<"]="<<px[k-1]<<"为坏值,";
cout<<"将予以剔除!!"<<endl;
}
//误差分析子函数
void errorcheck(int N,double* pv,struct result* resultx)
{
int ljerflag=0;
int cicleerflag=circleerror(N,pv,resultx->xb);
if(cicleerflag==1)
print2(resultx->xj,resultx->xb);
else if(cicleerflag==0)
//int ljerflag=ljerror(N,pv,resultx->vm);直接这样写,
//会出现"error C2065: 'ljerflag' : undeclared identifier"的错误
//可能原因是系统在编译是:系统在编译时对于分支程序只预编译第一个分支,故在
//第二个分支中的变量定义没有检测到。所以将变量定义语句" int ljerflag=0;"提前
ljerflag=ljerror(N,pv,resultx->vm);
else
cout<<"数据处理过程出错!!";
if(ljerflag==1)
print3(resultx->xj,resultx->xb);
else
{
resultx->xjb=resultx->xb/sqrt(N);
print4(resultx->xj,resultx->xb,resultx->xjb);
}
}
//周期误差分析
int circleerror(int N,double* pv,double xb)
{
double temp=0;
for(int i=0;i<N-1;i++)
{
temp+=pv[i]*pv[i+1];
}
temp=abs(temp);
if(temp>sqrt(N-1)*xb*xb)
{
return 1;
}
else
return 0;
}
//累进性误差分析
int ljerror(int N,double*pv, double vm)
{
double A=0,B=0;
double temp;
if(N%2)
{
for(int i=0;i<(N-1)/2;i++)
{
A+=pv[i];
}
for(int j=(N+1)/2;j<N;j++)
{
B+=pv[j];
}
}
else
{
for(int l=0;l<N/2;l++)
{
A+=pv[l];
}
for(int m=N/2+1;m<N;m++)
{
B+=pv[m];
}
}
temp=abs(A-B);
if(temp>abs(vm))
{
return 1;
}
else
{
return 0;
}
}
//输出存在周期误差的数据处理结果
void print2(double xj,double xb)
{
cout<<"您输入的数据存在周期误差:"<<endl;
cout<<"数据处理结果如下"<<endl;
cout<<"数据的平均值为:"<<xj<<endl;
cout<<"数据的标准差为:"<<xb<<endl;
}
//输出存在累进性误差的数据处理结果
void print3(double xj,double xb)
{
cout<<"您输入的数据存在累进性误差:"<<endl;
cout<<"数据处理结果如下"<<endl;
cout<<"数据的平均值为:"<<xj<<endl;
cout<<"数据的标准差为:"<<xb<<endl;
}
//输出不存在误差的数据处理结果
void print4(double xj,double xb,double xjb)
{
cout<<"您输入的数据不存在周期性误差和累进性误差:"<<endl;
cout<<"数据处理结果如下"<<endl;
cout<<"数据的平均值为:"<<xj<<endl;
cout<<"数据的标准差为:"<<xb<<endl;
cout<<"数据的平均值标准差为:"<<xjb<<endl;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -