![](/img/trans.png)
[英]In C, I am having trouble with the algorithm of sorting elements from a file from largest to smallest
[英]Sorting structure variables(3 ints) in file from smallest to largest C
我需要為我的項目排序我的 2 個文本文件。 其中一個文本文件采用學生 ID、課程 ID 和分數格式,另一個采用學生 ID 格式。 我用fprintf寫了它們兩個中的值,所以我在閱讀時使用了fscanf,但我的功能無法正常工作,你能告訴我我的錯誤在哪里嗎?
(我刪除了無效的舊代碼,因為我繼續使用不同的代碼)
編輯:我試圖根據下面的解決方案創建一個代碼片段,但是當我進入 while 循環時,fscanf 開始得到錯誤的數字。 你能找出我的錯誤在哪里嗎?
編輯:在這里編輯后,我做了 fopen 檢查。 他們沒有問題。 while 循環在運行一次后關閉,我認為,在 if fscanf 部分,fscanf 無法正確讀取數字,因此它會以 break 退出循環。
最有可能損壞的部分:
FILE *index = fopen("index.txt", "r");
FILE *record = fopen("record.txt", "r");
if (!index)
return;
if (!record)
return;
int array[n][3];
//int *array = (int *)malloc(n *3 sizeof(int));???
int count = 0;
int i=0,temp,id,course,score;
while (1)
{
if(count==n) break;
if (fscanf(record, "%d", &id) != 1) break;
if (fscanf(record, "%d", &course) != 1) break;
if (fscanf(record, "%d", &score) != 1) break;
array[count][0] = id;
array[count][1] = course;
array[count][2] = score;
count++;
}
其余功能供您瀏覽是否錯誤在其他地方:
void sort_for_bin_search(int n)
{
FILE *index = fopen("index.txt", "r");
FILE *record = fopen("record.txt", "r");
if (!index)
return;
if (!record)
return;
int array[n][3];
//int *array = (int *)malloc(n *3 sizeof(int));???
int count = 0;
int i=0,temp,id,course,score;
while (1)
{
if(count==n) break;
if (fscanf(record, "%d", &id) != 1) break;
if (fscanf(record, "%d", &course) != 1) break;
if (fscanf(record, "%d", &score) != 1) break;
array[count][0] = id;
array[count][1] = course;
array[count][2] = score;
count++;
}
for (i = 1; i < n - 1; i++)
{
for (int j = 0; j < n - 1; j++)
{
if(array[i][0] > array [j][0])
{
temp=array[j][0];
array[j][0] = array[i][0];
array[i][0] = temp;
temp=array[j][1];
array[j][1] = array[i][1];
array[i][1] = temp;
temp=array[j][2];
array[j][2] = array[i][2];
array[i][2] = temp;
}
else if((array[i][0]==array[j][0])&&(array[i][1]>array[j][1]))
{
temp=array[j][0];
array[j][0] = array[i][0];
array[i][0] = temp;
temp=array[j][1];
array[j][1] = array[i][1];
array[i][1] = temp;
temp=array[j][2];
array[j][2] = array[i][2];
array[i][2] = temp;
}
}
}
fclose(record);
fclose(index);
FILE *index2 = fopen("index.txt", "w");
FILE *record2 = fopen("record.txt", "w");
for (i = 0; i < n; i++)
{
fprintf(index2,"%d\n",array[i][0]);
fprintf(record2,"%d %d %d\n",array[i][0],array[i][1],array[i][2]);
//printf("%d %d %d\n",array[i][0],array[i][1],array[i][2]);
}
//free(array);
fclose(record2);
fclose(index2);
}
看起來您正在進行冒泡排序,並且在每次迭代中您都從磁盤讀取/寫入。 磁盤操作非常緩慢。 如果您一次讀入數組,它會更容易和更快。 然后對該單個數組進行排序。
例子:
void sort_for_bin_search(int n)
{
//assumes that `n` is the number of lines in this file
if (n < 1) return;
FILE* fin = fopen("index.txt", "r");
if (!fin)
return;
FILE* fout = fopen("record.txt", "w");
if (!fout)
return;
int* arr = malloc(n * sizeof(int));
if (!arr) return;
int count = 0;
while (1)
{
if (count == n)
break;
int id, course, score;
if (fscanf(fin, "%d", &id) != 1) break;
if (fscanf(fin, "%d", &course) != 1) break;
if (fscanf(fin, "%d", &score) != 1) break;
arr[count] = id;
count++;
}
//add code for sorting arr
for (int i = 0; i < count; i++)
fprintf(fout, "%d\n", arr[i]);
free(arr);
fclose(fin);
fclose(fout);
}
然后你可以排序,例如使用冒泡排序。
在每一步使用printf
在屏幕上打印數據,這將有助於調試。
void sort_for_bin_search(int n)
{
FILE* fin = fopen("input_file.txt", "r");
if (!fin)
{
printf("input error\n");
return;
}
int array[n][3];
int count = 0;
while (1)
{
int id, course, score;
if (count == n) break;
if (fscanf(fin, "%d", &id) != 1) break;
if (fscanf(fin, "%d", &course) != 1) break;
if (fscanf(fin, "%d", &score) != 1) break;
array[count][0] = id;
array[count][1] = course;
array[count][2] = score;
count++;
}
n = count;
printf("reading:\n");
for (int i = 0; i < n; i++)
printf("%d %d %d\n", array[i][0], array[i][1], array[i][2]);
printf("\nsort\n");
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n - 1 - i; j++)
{
if (array[j][0] > array[j + 1][0])
{
int temp;
temp = array[j][0];
array[j][0] = array[j + 1][0];
array[j + 1][0] = temp;
temp = array[j][1];
array[j][1] = array[j + 1][1];
array[j + 1][1] = temp;
temp = array[j][2];
array[j][2] = array[j + 1][2];
array[j + 1][2] = temp;
}
}
}
fclose(fin);
printf("sorted\n");
for(int i = 0; i < n; i++)
printf("%d %d %d\n", array[i][0], array[i][1], array[i][2]);
printf("write to file\n");
FILE* fout = fopen("output_file.txt", "w");
if(!fout)
{
printf("output error\n");
return;
}
for (int i = 0; i < n; i++)
fprintf(fout, "%d %d %d\n", array[i][0], array[i][1], array[i][2]);
fclose(fout);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.