简体   繁体   English

终端中带有C的文本文件

[英]Text file with C in terminal

So I calculated the mean and standard deviation from double values read within a file. 因此,我从文件中读取的双精度值计算了平均值和标准差。

My file data has 1 number per line: My data in the file is the following 我的文件数据每行有1个数字:文件中的我的数据如下

1 1个

2 2

3 3

4 4

5 5

6 6

7 7

8 8

9 9

10 10

My code is below: 我的代码如下:

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

int main(){

    FILE *inputfile;
    char name[100];

    printf("Enter the file you want to use to calculate standard deviation:\n ");
    gets(name); 
    inputfile = fopen(name, "r");

    if (inputfile == NULL)
    {
        printf("Failed to open text file.\n");
        exit(1);
    }

    double i; 
    double j=1;
    double average; 
    double stdish=0;
    double stdreal=0; 
    double x=0;
    double sum=0;
    double stdfinal;

    while(fscanf(inputfile, "%lf", &i) != EOF){
        x=x+1;
        sum = sum + i;
        j =pow(i,2);
        stdreal +=j;
    }
        average = sum/x;
        stdish = (stdreal/x)-(pow(average,2));
        stdfinal = sqrt(stdish);

    printf("The average is %.4lf\n", average);
    printf("The standard deviation is %.4lf\n", stdfinal);


return 0;
}

My standard deviation is incorrect, and I am not sure why. 我的标准偏差不正确,我不确定为什么。 In my program, I use fopen to get the text from the input file. 在我的程序中,我使用fopen从输入文件中获取文本。 Also, I am trying to make it so that I input the text file from the terminal instead of in the actual program itself. 另外,我试图做到这一点,以便我从终端而不是在实际程序本身中输入文本文件。 How to do that? 怎么做?

First you need to find out the average (mean) and then iterate through the loop to find out the variance. 首先,您需要找出平均值(均值),然后遍历循环以找出方差。 The SQRT(variance) will give you standard deviation. SQRT(方差)将为您提供标准偏差。

double CalculateMean() 
{ 
    double sum = 0;  
    for(int i = 0; i < max; i++) 
        sum += value[i]; 
    return (sum / max); 
} 

double CalculateVariane() 
{ 
    mean = CalculateMean(); 
    double temp = 0;  

    for(int i = 0; i < max; i++) 
    { 
         temp += (value[i] - mean) * (value[i] - mean) ; 
    } 
    return temp / max; 
} 

Reference: http://www.softwareandfinance.com/CPP/MeanVarianceStdDevi.html 参考: http : //www.softwareandfinance.com/CPP/MeanVarianceStdDevi.html

Declare main as int main(int argc, char *argv[]) or as int main(int argc, char **argv) . main声明为int main(int argc, char *argv[])int main(int argc, char **argv) They mean the same thing. 他们是同一回事。 In this version, argc is the number of command line arguments (+1 because the program name is argument 0). 在此版本中, argc是命令行参数的数目(+1是因为程序名称是参数0)。 argv is an array of char * s, each of which is a command line argument. argvchar * s的数组,每个数组都是一个命令行参数。

You can do something like this: 您可以执行以下操作:

int main(int argc, char *argv[])
{

    FILE *inputfile;
    char name[100];

    if(argc == 1) {
        printf("Enter the file you want to use to calculate standard deviation:\n ");
        gets(name);
    } else {
        strcpy(name, argv[1]);
    }
    inputfile = fopen(name, "r");

    if (inputfile == NULL)
    {
        printf("Failed to open text file.\n");
        exit(1);
    }

    double i; 
    double j=1;
    double average; 
    double stdish=0;
    double stdreal=0; 
    double x=0;
    double sum=0;
    double stdfinal;

    while(fscanf(inputfile, "%lf", &i) != EOF){
        x=x+1;
        sum = sum + i;
        j =pow(i,2);
        stdreal +=j;
    }
    average = sum/x;
    stdish = (stdreal/x)-(pow(average,2));
    stdfinal = sqrt(stdish);

    printf("The average is %.4lf\n", average);
    printf("The standard deviation is %.4lf\n", stdfinal);


    return 0;

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM