从没有标题的其他c文件导入函数

时间:2014-03-20 01:25:25

标签: c import

我的任务是创建一个程序,基本上测试我在另一个c文件中创建的库。我的问题是如何在没有标题的情况下调用库。图书馆计划是:

/* 
Program: statlib.c 

A Statistical library consisting of the following 
seven functions: mean, variance, standard deviation, 
sort, minimum value median, and max value. 
*/

#include <math.h>

/* Calculates the mean value of the data in the list */
double mean(int totnum, double data[]) {
double meanval, sum;
int i;
sum = 0.0;
for(i=0; i<totnum; i++) {
    sum += data[i];
}
meanval = sum/totnum;
return meanval;
}

// Calculate the variance of the data in the list
double variance(int totnum, double data[]) {
double meanval, sum, var;
int i;

meanval = mean(totnum, data);
sum = 0.0;
for(i=0; i<totnum; i++){
    sum += (data[i] - meanval)*(data[i] - meanval);
}
var = sum/(totnum-1);
return var;
    }

/* Calculate the standard deviation of data in the list */
double stdDeviation(int totnum, double data[]) {
double var, std;
var = variance(totnum, data);
std = sqrt(var);
return std;
}

/* Sorts the array data in ascending order. */
void sort(int totnum, double data[]) {
int i, j;
double temp;

/* find the smallest value in the reaming part of the array
in each iteratio staring with data[i]. */
for(i = 0; i < totnum-1; i++) {
    /* compare with data[i] with data[j], swap their values */
    for(j = i+1; j < totnum; j++) {
        /* when data[i] > data[j], swap their values */
        if(data[i] > data[j]) {
            temp = data[i];
            data[i] = temp;
        }
    }
}
}

/* Calculates the min value of an array. */
double minValue(int totnum, double data[]) {
double minval;
int i;

/* start at the intial value */
minval = data[0];
for(i = 1; i < totnum; i++) {
    if(data[i] < minval) {
        minval = data[i];
    }
    return minval;
}
}

/* Calculate the median of an array. Must be sorted first using the sort
function.*/
double median(int totnum, double data[]) {
double medianval;

if(totnum % 2) {
    medianval = data[totnum/2];
}
else {
    medianval = (data[totnum/2 -1] + data[totnum/2]/2.0);
}
return medianval;
}

/* Returns the maximum value of an array. */
double maxValue(int totnum, double data[]) {
double maxval;
int i;
maxval = data[0];
for(i = 1; i < totnum; i++) {
    if(data[i] > maxval) {
        maxval = data[i];
    }
}
return maxval;
}

我用来测试第一个程序的程序是:

/* 
Program: teststatlib.c 
This program tests my statlib.c program in order to
verify whether or not it is in working order.
*/

#include <stdio.h>

/* add code from statlib.c */
#pragma importf <statlib.c>

/* Declare extern functions so that the can be used */
extern double mean(int totnum, double data[]);
//extern double variance(int totnum, double data[]);
//extern double stdDeviation(int totnum, double data[]);
//extern void sort(int totnum, double data[]);
//extern double minValue(int totnum, double data[]);
//extern double median(int totnum, double data[]);
//extern double maxValue(int totnum, double data[]);

int main() {
int input[] = {30,90,100,84,72,40,34,91,80,62};
int totnum = sizeof(input)/sizeof(double);

printf("The unsorted array is %d.", input);
printf("The mean is ");
mean(totnum, input);
}

1 个答案:

答案 0 :(得分:0)

我发现你的问题令人困惑,因为你没有很好地陈述你的问题。事实上,你从未问过一个问题。也许我仍然可以提供一些帮助。

您可以解决的一些问题:

  • sizeof(input)不是sizeof(input[])
  • 倒数第二行不符合您的要求。如果删除[],它将编译并打印数组的地址(这不是我想要的)。您可能想要打印数组的每个元素,您应该考虑在for循环中执行此操作。你有什么看起来非常python-esk,但不是我们在c中做事的方式。
  • 在你的最后一行,你打电话给mean(),好像你希望它打印一样。我想你想把最后两行合并到printf("The mean is %f\n", mean(totnum,input));
  • input需要声明为double类型的数组。 (另外,您需要在下面的行中将sizeof(int)更改为sizeof(double)

我设法使用命令gcc teststatlib.c statlib.c在linux中编译和运行程序。我不承认#pragma importf <statlib.c>,这让我怀疑您可能正在使用我不熟悉的开发环境。