所以我打开了一个CSV文件,并将其添加到结构中。我想在第三列中找到最高/最低值,并在第四列中找到最高/最低值。然后将出现它们的每一行打印出来。
typedef struct {
float xvalue;
float yvalue;
float uvalue;
float vvalue;
} flow_data;
此处正在添加CSV文件-
int total = 0;
flow_data d;
flow_data* dataset = NULL;
while(4 == fscanf(fp, "%f, %f, %f, %f\n", &d.xvalue, &d.yvalue, &d.uvalue, &d.vvalue))
{
dataset = realloc(dataset, sizeof(*dataset) * (total + 1));
dataset[total] = d;
total++;
}
操作方面,我知道这是一种荒谬的处理方式,但我不确定如何提高其效率。如何将所有内容添加到函数中?您可以将整个结构传递给函数吗?
float minvalueu = 100000, maxvalueu = 0;
float minvaluev = 100000, maxvaluev = 0;
int ivalueofminu = 0, ivalueofmaxu = 0;
int ivalueofminv = 0, ivalueofmaxv = 0;
for(int i = 0; i < total; i++)
{
flow_data* p = &dataset[i];
if ( p->uvalue > maxvalueu)
{
maxvalueu = p->uvalue;
ivalueofmaxu = i;
}
if ( p->uvalue < minvalueu)
{
minvalueu = p->uvalue;
ivalueofminu = i;
}
if ( p->vvalue > maxvaluev)
{
maxvaluev = p->vvalue;
ivalueofmaxv = i;
}
if ( p->vvalue < minvaluev)
{
minvaluev = p->uvalue;
ivalueofminv = i;
}
printf("%.7f, %.7f, %.7f, %.7f\n", p->xvalue, p->yvalue, p->vvalue, p->uvalue);
}
flow_data* z = &dataset[ivalueofmaxu];
printf("Max of U - %.7f, %.7f, %.7f, %.7f\n", z->xvalue, z->yvalue, z->vvalue, z->uvalue);
z = &dataset[ivalueofminu];
printf("Min of U - %.7f, %.7f, %.7f, %.7f\n", z->xvalue, z->yvalue, z->vvalue, z->uvalue);
z = &dataset[ivalueofmaxv];
printf("Max of V - %.7f, %.7f, %.7f, %.7f\n", z->xvalue, z->yvalue, z->vvalue, z->uvalue);
z = &dataset[ivalueofminu];
printf("Min of V - %.7f, %.7f, %.7f, %.7f\n", z->xvalue, z->yvalue, z->vvalue, z->uvalue);
答案 0 :(得分:1)
问题的最终代码块中的整个代码都可以移至一个函数中。
该函数的参数将是指向结构flow_data
的指针以及数组中元素的数量(total
)。
该函数将如下所示:
void findAndPrintMaxMinVals(flow_data* dataset, int total)
从main()
呼叫
findAndPrintMaxMinVals(dataset, total)
完整的代码如下:
#include <stdio.h>
#include <stdlib.h>
typedef struct {
float xvalue;
float yvalue;
float uvalue;
float vvalue;
} flow_data;
void findAndPrintMaxMinVals(flow_data* dataset, int total)
{
float minvalueu = 100000, maxvalueu = 0;
float minvaluev = 100000, maxvaluev = 0;
int ivalueofminu = 0, ivalueofmaxu = 0;
int ivalueofminv = 0, ivalueofmaxv = 0;
for(int i = 0; i < total; i++)
{
flow_data* p = &dataset[i];
if ( p->uvalue > maxvalueu)
{
maxvalueu = p->uvalue;
ivalueofmaxu = i;
}
if ( p->uvalue < minvalueu)
{
minvalueu = p->uvalue;
ivalueofminu = i;
}
if ( p->vvalue > maxvaluev)
{
maxvaluev = p->vvalue;
ivalueofmaxv = i;
}
if ( p->vvalue < minvaluev)
{
minvaluev = p->uvalue;
ivalueofminv = i;
}
printf("%.7f, %.7f, %.7f, %.7f\n", p->xvalue, p->yvalue, p->vvalue, p->uvalue);
}
flow_data* z = &dataset[ivalueofmaxu];
printf("Max of U - %.7f, %.7f, %.7f, %.7f\n", z->xvalue, z->yvalue, z->vvalue, z->uvalue);
z = &dataset[ivalueofminu];
printf("Min of U - %.7f, %.7f, %.7f, %.7f\n", z->xvalue, z->yvalue, z->vvalue, z->uvalue);
z = &dataset[ivalueofmaxv];
printf("Max of V - %.7f, %.7f, %.7f, %.7f\n", z->xvalue, z->yvalue, z->vvalue, z->uvalue);
z = &dataset[ivalueofminu];
printf("Min of V - %.7f, %.7f, %.7f, %.7f\n", z->xvalue, z->yvalue, z->vvalue, z->uvalue);
}
int main() {
int total = 4; //total no of datasets for example
int i;
flow_data d;
flow_data* dataset = NULL;
dataset = (flow_data*)malloc(sizeof(flow_data) * total); //memory for 4 elements for testing
//Generate values and fill in
for(i = 0; i < total; i++) {
dataset[i].xvalue = (i+1) * 7;
dataset[i].yvalue = (i+1) * 8;
dataset[i].uvalue = (i+1) * 9;
dataset[i].vvalue = (i+1) * 10;
}
findAndPrintMaxMinVals(dataset, total);
}