我正在用C构建一个Basic Stat Header文件并遇到问题

时间:2017-08-23 12:51:05

标签: c struct header statistics

这是我的第一篇文章,所以如果语法不正确,我会道歉。我要做的是构建一个基本的头文件来进行一些统计分析。我宁愿在C中编写自己的代码而不是像R或SPSS这样的程序。已经有几年了,我有点生疏了。

注意:我是一个相当新手的程序员,我依赖于大学里向我展示的许多工具。例如,在学校,我的教授编写了一个名为FPT.h的编译器和头文件。我并不积极在那里,我认为它主要是图形,但无论如何我将它添加到我的所有代码中。我不认为我在下面的代码中使用它,所以你应该能够毫无问题地将它评论出来。我也使用他的编译器,acom,我将添加到问题的底部。

这是标题文件,名为rd_stat.h

#ifndef mdr_stat_stuff
#define mdr_stat_stuff 

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

typedef struct {
  double mean;
  double median;
  double mode;
  double stddev;
  double q1;
  double q3;
  double low;
  double hi;
} 
BSTAT;

void  sort_doubles  (double data[], int size);
double get_mean     (double data[], int size);
double get_median   (double data[], int size);
double get_q1 (double data[], int size);
double get_q3 (double data[], int size);
int    get_mode     (double data[], int size);
double get_stddev   (double data[], int size);

struct BSTAT init_bstat(double data[], int size);

#endif

这是它的编译器朋友rd_stat.c:

#ifndef mdr_stat_stuff
#define mdr_stat_stuff 

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

void sort_doubles(double data[],int size){
  int i, j;
  double temp;

  //Sorts the data in Ascending Order  
  for (i = 0; i < size; i++){
    for (j = 0; j < (size - i - 1); j++){
      if (data[j] > data[j + 1]){
    temp = data[j];
    data[j] = data[j + 1];
    data[j + 1] = temp;
      }
    }
  }
}

/////////////////////////////////////////////////////////////////////////

double get_mean(double data[], int size){//to get the mean we need the array and its length
  int i;
  double mean = 0;
  for(i = 0; i < size; i++){
    mean += data[i];
  }
  return mean / size;
}
/////////////////////////////////////////////////////////////////////////


double get_median(double data[], int size){
  //only returns one median even if SIZE is even
  //if SIZE is even, it finds both medians and takes the average of the two
  //DECISION because it is often done anyway when there are two Medians. Seems more practical. 
  int i, j;
  double temp;

  sort_doubles(data, size);

  /* if(size % 2 == 0){//even cases */
  /*   return (data[size / 2 - 1] + data[size / 2]) / 2; */
  /* }else{//odd cases */
  /*   return data[(size - 1) / 2]; */
  /* }  */

  printf("location for median is  %5.2lf\n", ((double)size-1) / 2);


  return (data[(int)floor(((double)size-1) / 2)] + data[(int)ceil(((double)size-1) / 2)]) / 2;
}

double get_q1(double data[], int size){
  int i, j;
  double temp;

  sort_doubles(data, size);

  printf("location for q1 is  %5.2lf\n", (double)size / 4);

  return (data[(int)floor((double)size / 4)] + data[(int)ceil((double)size / 4)]) / 2;
}

double get_q3(double data[], int size){
  int i, j;
  double temp;

  sort_doubles(data, size);

  printf("location for q3 is  %5.2lf\n", (double)(3*size-4) / 4);

  return (data[(int)floor((3*(double)size-4) / 4)] + data[(int)ceil((3*(double)size-4) / 4)]) / 2;
}


/////////////////////////////////////////////////////////////////////////

int get_mode(double data[],int size) {
  //Code augmented from other user:
  //https://www.tutorialspoint.com/learn_c_by_examples/mode_program_in_c.htm


  //Function assumes there is EXCATLY ONE Mode in the data. 
  //I don't like this

  int maxValue = 0, maxCount = 0, i, j;

   sort_doubles(data, size);

  //Finds Mode
   for (i = 0; i < size; ++i) {
      int count = 0;      
      for (j = 0; j < size ; ++j) {
         if (data[j] == data[i])
         ++count;
      }      
      if (count > maxCount) {
         maxCount = count;
         maxValue = data[i];
      }      
   } 
     return maxValue;
}
/////////////////////////////////////////////////////////////////////////
double get_stddev(double data[], int size){

  double mean = get_mean(data, size);
  int i;
  double sum=0;

  for(i = 0 ; i < size ; i++){
    sum += pow(data[i] - mean, 2);
  }
  return sqrt(sum / size);
}
/////////////////////////////////////////////////////////////////////////


BSTAT init_bstat(double data[], int size){

  BSTAT a;
  a.mean = get_mean(data, size);
  a.q1 = get_q1(data, size);
  a.median = get_median(data, size);
  a.q3 = get_q3(data, size);
  a.mode = get_mode(data, size);
  a.stddev = get_stddev(data, size);

  sort_doubles(data, size);
  a.low = data[0];
  a.hi = data[size - 1];


  return a;

}


#endif

这是我试图启动的程序,test_bstat.c: *注意:在我将函数init_bstat()放入头文件及其编译器之前,我写的是在test_bstat.c中以确保它有效。一旦我把它全部平方移开,我将它复制到rd_stat.c并将其声明添加到rd_stat.h中。那是问题开始的时候。

#include <FPT.h>
#include <rd_stat.h>



/* BSTAT init_bstat(double data[], int size){ */

/*   BSTAT a; */
/*   a.mean = get_mean(data, size);        */
/*   a.median = get_median(data, size); */
/*   a.mode = get_mode(data, size); */
/*   a.stddev = get_stddev(data, size); */

/*   sort_doubles(data, size); */
/*   a.low = data[0]; */
/*   a.hi = data[size - 1]; */
/*   a.q1 = get_q1(data, size); */
/*   a.q3 = get_q3(data, size); */

/*   return a; */
/* } */



int main(){

  int size = 10;
  int i = 0;
  double scores[10];

  for(i = 0 ; i < size ; i++){
    scores[i] = floor(15*drand48() + 80);
  }

  for(i = 0 ; i < size ; i++){
    printf("Scores[%d] = %.2lf\n", i, scores[i]);
  }


  BSTAT LEANN;

 LEANN = init_bstat(scores, size);

  printf("Mean = %5.1lf Median = %5.1lf Mode = %5.0lf\n", LEANN.mean, LEANN.median, LEANN.mode);
  printf("Low = %5.1lf Hi = %5.1lf Stddev = %5.1lf Q1 = %5.1lf Q3 = %5.1lf\n", LEANN.low, LEANN.hi, LEANN.stddev, LEANN.q1, LEANN.q3);

}

我编写这个的方式完全是通过我教授的编译器,acom:

acom test_bstat.c rd_stat.c -I. rd_stat.h

编译时,我收到以下错误:

user@OptiPlex-780:~/CS/FOOTBALL$ acom test_bstat.c rd_stat.c -I. rd_stat.h
test_bstat.c: In function ‘main’:
test_bstat.c:44:2: error: invalid use of undefined type ‘struct BSTAT’
  LEANN = init_bstat(scores, size);
  ^
rd_stat.c:126:1: error: unknown type name ‘BSTAT’
 BSTAT init_bstat(double data[], int size){
 ^
rd_stat.c: In function ‘init_bstat’:
rd_stat.c:128:3: error: unknown type name ‘BSTAT’
   BSTAT a;
   ^
rd_stat.c:129:4: error: request for member ‘mean’ in something not a structure or union
   a.mean = get_mean(data, size);
    ^
rd_stat.c:130:4: error: request for member ‘q1’ in something not a structure or union
   a.q1 = get_q1(data, size);
    ^
rd_stat.c:131:4: error: request for member ‘median’ in something not a structure or union
   a.median = get_median(data, size);
    ^
rd_stat.c:132:4: error: request for member ‘q3’ in something not a structure or union
   a.q3 = get_q3(data, size);
    ^
rd_stat.c:133:4: error: request for member ‘mode’ in something not a structure or union
   a.mode = get_mode(data, size);
    ^
rd_stat.c:134:4: error: request for member ‘stddev’ in something not a structure or union
   a.stddev = get_stddev(data, size);
    ^
rd_stat.c:137:4: error: request for member ‘low’ in something not a structure or union
   a.low = data[0];
    ^
rd_stat.c:138:4: error: request for member ‘hi’ in something not a structure or union
   a.hi = data[size - 1];
    ^

似乎它没有将结构BSTAT识别为有效的返回类型,我不确定为什么。关于为什么会出现这种情况的任何线索?

此外,如果有人知道有一些/大部分/全部这些功能可用的开源文件,那么这也是有帮助的。

1 个答案:

答案 0 :(得分:1)

  1. 使用#include "..."的正确语法。您的编译器也可以在您的主目录中搜索,但#include <...>用于在系统中搜索,并使用-I选项添加目录。

  2. .c文件中删除警卫。它会阻止您的.h文件被编译(其内容只是被忽略)。

  3. 我建议传递和返回指向结构的指针而不是结构本身;如此大的会在每次通话和返回时增加巨大的开销。