[英]Reading text file and sorting two arrays
我有一个200行的“ Temp.dat”,看起来像这样:
0.060493 1 0.5 1
1.596961 0 0.1 2
0.87758 1 0.3 1.5
0.165453 1 0 3
0.07085 1 0.3 4
0.125379 1 0.2 3
0.454202 1 0.2 2
0.373227 1 0.3 1
0.131486 1 0.3 3
0.867477 0 0.5 4
0.122609 0 0.8 9
我正在尝试使用以下代码读取每一行并将数据的每一列存储到单独的数组中:
#include <stdio.h>
#include <stdlib.h>
#define MAX 200
int main(){
double x[MAX], y[MAX];
double a, b;
int m = 0;
FILE* file;
file = fopen("Temp.dat", "r");
if ( file != NULL ){
/* Read the two columns. */
while(fscanf(file, "%d %d", &a, &b) == 2)
{ x[m] = a;
y[m] = b;
m++;
}
}
printf("%d %d\n", x[4], y[1]); # Test the stored inputs of the two arrays
return(0);
}
当我尝试打印结果时,结果为1 13258992
,而不是0.165453 0
。 我不知道它在哪里配对1 13258992
,因为我认为fscanf(file, "%d %d", &a, &b) == 2
做了应做的事情:遍历文件Temp.dat
每一行Temp.dat
和读取两个double-type
整数,然后被存储在两个阵列x[MAX]
和`Y [MAX]。 因此,有人可以帮助我解决此问题吗?
另一个问题:将上面的两列存储在两个数组x[MAX]
和y[MAX]
,我想根据第一个数组x[MAX]
的值以递增的顺序对这两个数组进行排序。 看起来像:
0.060493 1
0.07085 1
0.122609 0
0.125379 1
0.131486 1
0.165453 1
0.373227 1
0.454202 1
0.867477 0
如何在C中执行此sorting
例程,因为很难将y[MAX]
的元素排列为遵循x[MAX]
中其对应元素的顺序?
首先在while
条件中添加&& m < MAX
,以免缓冲区溢出。
scanf
期望使用%d
指向int
的指针,而不是将其double
,它将内容解析为整数。 您必须使用%lf
来解析double
。 这同样适用于printf
调用:
while(fscanf(file, "%lf %lf", &a, &b) == 2 && m < MAX)
...
...
printf("%lf %lf\n", x[4], y[1]);
我懂了
0.070850 0.000000
这是dat文件的第五行(不是第四行,数组索引从0开始,而不是1)。
至于您的其他问题:
恐怕您必须编写自己的排序函数。
对于初学者,请尝试:
double a;
int b;
...
fscanf(file, "%f %d", &a, &b)
您将a
和b
初始化为双精度类型,但是您正在从copy.dat文件中读取整数( %d
)。 b
是一个int值,因此将%d
保留在fscanf
并更改初始化。
您可以使用x
和y
声明一个结构,以便将两个值绑定在一起,当在排序过程中交换数组元素时, x
和y
将保持它们的关系。 例:
struct data_t
{
double x;
int y;
};
int compare(const void *a, const void *b)
{
const struct data_t *m = a;
const struct data_t *n = b;
if(m->x == n->x)
return m->y > n->y;
return m->x > n->x;
}
int main(void)
{
struct data_t data[MAX];
FILE* file = fopen("Temp.dat", "r");
int count = 0;
while(fscanf(file, "%lf %d", &data[count].x, &data[count].y) == 2)
{
count++;
if (count == MAX)
break;
}
qsort(data, count, sizeof(struct data_t), compare);
for(int i = 0; i < count; i++)
printf("%lf %d\n", data[i].x, data[i].y);
return 0;
}
如其他答案所示,将%lf
用作double
float
值,或将%f
用作float
。
关于比较功能,假设您的数据中包含以下值:
0.060493 1
0.060493 5
0.060493 2
在这种情况下, x
从第一元素的值是相同的x
与其它元件值。 如果仅测试m->x > n->x
则不会发生排序。 因此,如果x
相同, m->y > n->y
比较m->y > n->y
。
qsort
是标准的C函数。 它不知道您的数据类型,我们必须告诉它有关数据类型的信息。 这是在compare函数内部完成的,该函数接收指针a
和b
,我们知道它们是指向data
数组中元素的指针,因此这就是转换的原因。
数据集最多可以包含200
项目,因为我们将其声明为struct data_t data[MAX];
其中MAX
是200
。
double arr[MAX + 1][8]
请注意,数组的第一个索引是从0
到MAX
行。 它是以这种方式设置的,因此可以在以后进行排序。 现在,我们可以将文件直接读取到数组中,并对数组进行排序(无需结构)。 确保不要将其与具有arr[8][MAX + 1]
的旧解决方案混合使用
int compare_2d_array(const void *pa, const void *pb) { double a = *(double*)pa; double b = *(double*)pb; return a > b; } int main(void) { //array with MAX + 1 rows, and 8 columns, initialized to zero double arr[MAX + 1][8] = { 0 }; FILE* file = fopen("temp.dat", "r"); int count = 0; while(fscanf(file, "%lf %lf", &arr[count][0], &arr[count][1]) == 2) { count++; if(count == MAX) break; } qsort(arr, count, sizeof(arr[0]), compare_2d_array); //arr[0] and arr[1] are ready, now set up the other columns: for(int i = 0; i < count; i++) { //make modifications to other columns arr[i][2] = i ? arr[i - 1][0] : 0; arr[i][3] = arr[i][0]; arr[i + 1][4] = i + 1; printf("%.6lf %.0lf %.6lf %.6lf %.0lf\\n", arr[i][0], arr[i][1], arr[i][2], arr[i][3], arr[i][4]); } return 0; }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.