回 帖 发 新 帖 刷新版面

主题:以下这个程序是大学<<物理实验>>数据,可以算平均值,残差,标准偏差,和总不确定度,只要你输入数据,

学了<<物理实验>>的人都知道,物理实验最麻烦的是数据处理,现在都是信息时代了,那种含有十几位小数的数据居然还要用手来一个个地算,真是太对起自己了,以下这个程序可以算平均值,残差,标准偏差,和总不确定度,只要你输入数据,

#include<math.h>
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#define MAX 20

typedef struct wuli{
        float d[MAX];
        char name[10];
        int   LEN;
        float ccha[MAX];   /*残差数组*/
        float avg;        /*data的平均值*/
        double sx;        /*标准偏差Sx*/
        double DU;        /*总不确定度*/
}wulidata;
wulidata *InputData();
void average(wulidata *wl);
void CanCha(wulidata *wl);
void BZPianCha(wulidata *wl);/*标准偏差*/
void BQDdu(wulidata *wl);/*总不确定度*/
void output(wulidata *wl);


/*----------------------------------------------------------*/
void line()
{
        int i;
        printf("\n");
        for(i=0;i<74;i++)
                printf("=");
        printf("\n");
}
/*-------------------------------------------------------*/
wulidata *InputData()
{

        int i=0,k;
        float da;
        char Z=0;
        wulidata *wl;
        wl=(wulidata *)malloc(sizeof(wulidata));
        printf("请为你要处理的数据起一个名字:");
        scanf("%s",wl->name);
        printf("\n下面请你输入数据%s具体数值,数据不能超过MAX个\n",wl->name);
        printf("当name='#'时输入结束\n");
        do{
                printf("%s%d=",wl->name,i+1);
                scanf("%f",&da);
                wl->d[i]=da;
         i++;
        }while(wl->d[i-1]!=0.0&&i<MAX);
        wl->LEN=i-1;
        do{
             printf("你输入的数据如下:\n");
             for(i=0;i<wl->LEN;i++)
                     printf("%s%d=%f\t",wl->name,i+1,wl->d[i]);
                 printf("\n你是否要作出修改(Y/N)?");
             while( getchar()!='\n');
                 Z=getchar();

             if( Z=='y'||Z=='Y'){
                      printf("你须要修改哪一个元素,请输入其标号i=(1~%d)\n",wl->LEN);
                          while( getchar()!='\n');
              scanf("%d",&k);
                      printf("\n%s%d=",wl->name,k);
                      scanf("%f",&(wl->d[k-1]));
          }
             else if(Z=='n'||Z=='N')
                      printf("OK!下面开始计算。");

        }while(Z!='N'&&Z!='n');
        return(wl);
}
/*--------------------------------------------------------------------*/
void average(wulidata *wl)
{
        float ad,sum=0;
        int i;
        for(i=0;i<wl->LEN;i++)
        {
                sum=sum+(wl->d[i]);
        }
        ad=sum/(wl->LEN);
        wl->avg=ad;
}
/*-------------------------------------------------------------------*/
void CanCha(wulidata *wl)
{
        int i;
        for(i=0;i<wl->LEN;i++)
         wl->ccha[i]=(wl->d[i])-(wl->avg);
}
/*---------------------------------------------------------------*/
void BZPianCha(wulidata *wl)/*标准偏差*/
{
        double sum;
        int i;
        sum=0;
        for(i=0;i<wl->LEN;i++)
                sum=sum+pow(wl->ccha[i],2);
        wl->sx=sqrt(sum/(wl->LEN-1));
}
/*-------------------------------------------------------------*/
void BQDdu(wulidata *wl)/*总不确定度*/
{
        int k=0;
        double A,B,y;
        line();
        printf("\n很抱歉,求总不确定度,首先要知道仪器误差。你可以选择你所用的实验仪器或自定义\n");
    printf("1..钢尺(0.3mm) 2..游标卡尺(0.02mm)  \n3..螺旋测微计(0.004mm)\n 4..自定义. ");
    printf("\n");
        while(getchar()!='\n');
        while(k<1||k>4){
        scanf("%d",&k);

        if(k>=1&&k<=4){
                switch(k){
                case 1: y=0.3;break;
                case 2: y=0.02;break;
                case 3: y=0.004;break;
                case 4:
                        printf("请输入仪器误差\n");
                        scanf("%f",&y);
                 break;
         }
                A=pow(wl->sx,2);
                B=pow(y,2);
                wl->DU=sqrt(A+B);
        }
        else{
                printf("输入错误!");}
        }
}
/*--------------------------------------------------------------------*/
void output(wulidata *wl)
{
        int i;
        printf("\n");
        line();
        printf("你输入的数据如下:\n");
        for(i=0;i<wl->LEN;i++)
        {printf("%s%d=%f\t",wl->name,i+1,wl->d[i]);}
    printf("\n");
        printf("\n\t数据%s的平均值(A)%s=%f",wl->name,wl->name,wl->avg);
        line();
        printf("数据的残差如下:\n");
        for(i=0;i<wl->LEN;i++)
        {printf("△%s%d=%s%d-(A)%s=%f\t\t",wl->name,i+1,wl->name,i+1,wl->name,wl->ccha[i]);}
    line();
    printf("求得标准偏差Sx=√【∑(△Xi)^2/(n-1)】  \n");
        printf("Sx=%f",wl->sx);
        printf("\n");
        printf("总不确定度△=√【△A^2+△B^2】\n");
        printf("%s的总不确定度△=%f\n\n",wl->name,wl->DU);
}
/*================================================================*/
main()
{
        wulidata *Hua=NULL;

         Hua=InputData();
     average(Hua);
      CanCha(Hua);
     BZPianCha(Hua);/*标准偏差*/
      BQDdu(Hua);/*总不确定度*/
      output(Hua);
      getch();
}





 数据                  d1            d2          d3          d4           d5              
      
      


d的平均值:

(avg)d=d/n

d 的偏差:

△di=|di-(avg)d|

标准偏差Sx=√【∑(△Xi)^2/(n-1)】

回复列表 (共3个回复)

沙发

Sorry, 有一个小错误------“当name='0'时输入结束”

板凳

while( getchar()!='\n');
这条语句是做什么用的?

3 楼

这个程编了多久,我现在越来越感到要学的东西太多

我来回复

您尚未登录,请登录后再回复。点此登录或注册