⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 average.c

📁 这个程序对于输入的一系列浮点数
💻 C
字号:
//---------------------------------------------------------------------------
/* 这个程序对于输入的一系列浮点数,求出其平均值 M,方差 S^2,标准差 S,其公式如下:
 *
 *      M = (x1 + x2 + x3 + ... xn) / N
 *      S^2 = (x1 - M)^2 + (x2 - M)^2 + (x3 - M)^2 + ... + (xn - M)^2 / (N - 1)
 * 其中 N >= 1
 *
 * */
#pragma hdrstop

//---------------------------------------------------------------------------

#pragma argsused

#include <stdio.h>
#include <conio.h>
#include <math.h>
#include <stdlib.h>

void format(int space);

int main(int argc, char *argv[])
{
        float x[100];
        float n = 0, temp;
        float M = 0.0f, S = 0.0f;

        printf("请输入少于或等于 100 个浮点数。\n");
        while(n < 100.0f) {
            if((temp = scanf("%f",&x[n])) == EOF) {
                if(n == 0.0f) {
                    fprintf(stderr,"没有输入数据,程序无法计算。\n");
                    getch();
                    exit(1);
                }
                break;
            }
            else if(temp == 1)
                M += x[n++];
            else {
                fprintf(stderr,"输入了错误的数据,程序无法外理。\n请输入浮点数。\n");
                fflush(stdin);
            }
        }
        M /= n;

        printf("报告计算结果:\n共输入了 %.0f 个浮点数。\n它们分别是:\n",n);
        for(temp = 0.0f; temp < n; temp++) {
            printf("%f",x[temp]);
            format(2);
            S += (x[temp] - M) * (x[temp] - M);
        }
        S /= (n - 1.0f);
        temp = S;
        S = sqrt(temp);
        printf("它们的平均值是:%f\n方差是:%f\n标准差是:%f\n",M,temp,S);

        getch();
        return 0;
}
void format(int space) {
/* 这个函数以每 (space + 1)次调用形成一个周期,其中的前 space 次每次调用输出一个空格,第
 * (space + 1)次调用输出一个换行符
 * */
     static char count;
     ++count <= space ? putchar(' ') : (count = 0, putchar('\n'));
}/* void format(void) */
//---------------------------------------------------------------------------
 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -