[英]How to sort numbers in an ascending order without using an array?
我正在努力編寫一個 C 程序,該程序按升序對從多個文件中讀取的數字進行排序。
要求:
注意:該程序必須僅使用文件來對數字進行排序,並且不應使用任何數組。
我為此工作了幾個小時,但現在有時它可以工作,有時它不能。 我真的不知道出了什么問題。
有關解決此問題的任何想法或建議?
#include <conio.h>
#include <stdio.h>
// to save the position of the smallest number
int pos;
// to get numbers from the user and save them in file1
void getNumbers(FILE *file) {
char x[255];
printf("Enter numbers: ");
scanf("%[^\n]s", &x);
fprintf(file, "%s", x);
}
// to find the smallest number and copy it to file3
void findSmallestNo(FILE *file1, FILE *file3) {
int x, temp;
rewind(file1);
fscanf(file1, "%d", &temp);
pos = ftell(file1);
while (!(feof(file1))) {
fscanf(file1, "%d", &x);
if (x < temp) {
temp = x;
pos = ftell(file1);
}
}
fprintf(file3, "%d ", temp);
}
// to copy numbers from file1 to file2 except the smallest number that we copied
// in the file3
void copyToFile2(FILE *file1, FILE *file2) {
int cur_pos, x;
rewind(file1);
rewind(file2);
while (!(feof(file1))) {
fscanf(file1, "%d", &x);
cur_pos = ftell(file1);
if (cur_pos != pos) {
fprintf(file2, "%d ", x);
}
}
}
// to copy from file2 to file1 (it should delete the old data in file1)
void copyToFile1(FILE *file1, FILE *file2) {
int x, count = 0;
rewind(file2);
while (!feof(file2)) {
count++;
fscanf(file2, "%d", &x);
fprintf(file1, "%d ", x);
}
}
// to print numbers on the screen
void print_file(FILE *file1) {
int x;
rewind(file1);
printf("\nSorted Numbers: ");
while ((fscanf(file1, "%d", &x)) == 1) {
printf("%d ", x);
}
printf("\n");
}
int main() {
int len, count = 1;
FILE *pFile1, *pFile2, *pFile3;
pFile1 = fopen("file1.txt", "w+");
pFile2 = fopen("file2.txt", "w+");
pFile3 = fopen("file3.txt", "a+");
if (pFile1 == NULL) {
printf("Couldn't open the file\n");
exit(1);
}
// to get the count of numbers in file1
getNumbers(pFile1);
int getLength(FILE * file) {
int nn, counting = 1;
rewind(file);
while ((fscanf(file, "%d ", &nn) == 1)) {
counting++;
}
return counting;
}
len = getLength(pFile1);
// while loop until var count is equal to the count of numbers in file1
while (count < len) {
++count;
findSmallestNo(pFile1, pFile3);
copyToFile2(pFile1, pFile2);
fclose(pFile1);
pFile1 = fopen("file1.txt", "w+");
copyToFile1(pFile1, pFile2);
}
fclose(pFile1);
fclose(pFile3);
pFile1 = fopen("file1.txt", "w+");
pFile3 = fopen("file3.txt", "r");
int number;
// to copy numbers from file3 to file1
while (count != 1) {
fscanf(pFile3, "%d ", &number);
fprintf(pFile1, "%d ", number);
--count;
}
// to print numbers on the screen in file1
print_file(pFile1);
fclose(pFile1);
fclose(pFile2);
fclose(pFile3);
return 0;
}
//#include <conio.h>
#include <stdio.h>
// to save the position of the smallest number
// to get numbers from the user and save them in file1
void getNumbers(char *filename) {
char x[255];
FILE *file;
file = fopen(filename, "w");
if (file == NULL) {
printf("Couldn't open the file\n");
return;
}
printf("Enter numbers: ");
scanf("%[^\n]s", &x);
fprintf(file, "%s", x);
fclose(file);
}
// to find the smallest number and copy it to file3
int findSmallestNo(char *filename1, char *filename3) {
int x, temp = 100000;
FILE *file1, *file3;
file1 = fopen(filename1, "r");
file3 = fopen(filename3, "a");
fscanf(file1, "%d", &temp);
while (fscanf(file1, "%d", &x) == 1) {
if (x < temp) {
temp = x;
}
}
fprintf(file3, "%d ", temp);
fclose(file1);
fclose(file3);
return temp;
}
// to copy numbers from file1 to file2 except the smallest number that we copied
// in the file3
void copyToFile2(char *filename1, char *filename2, int min_value) {
int flag = 0, x;
FILE *file1, *file2;
file1 = fopen(filename1, "r");
file2 = fopen(filename2, "w");
while (fscanf(file1, "%d", &x) == 1) {
if (x != min_value || flag == 1) {
fprintf(file2, "%d ", x);
} else {
flag = 1;
}
}
fclose(file1);
fclose(file2);
}
// to copy from file2 to file1 (it should delete the old data in file1)
void copyToFile(char *filename1, char *filename2) {
int x;
FILE *file1, *file2;
file1 = fopen(filename1, "w");
file2 = fopen(filename2, "r");
rewind(file2);
while (fscanf(file2, "%d", &x) == 1) {
fprintf(file1, "%d ", x);
}
fclose(file1);
fclose(file2);
}
// to print numbers on the screen
void print_file(char *filename) {
int x;
FILE *file;
file = fopen(filename, "r");
printf("\nSorted Numbers: ");
while ((fscanf(file, "%d", &x)) == 1) {
printf("%d ", x);
}
printf("\n");
fclose(file);
}
int getLength(char *filename) {
int nn, counting = 1;
FILE *file;
file = fopen(filename, "r");
rewind(file);
while ((fscanf(file, "%d ", &nn) == 1)) {
counting++;
}
fclose(file);
return counting;
}
void clearFile (char *filename) {
FILE *file;
file = fopen(filename, "w");
fclose(file);
}
int main() {
int len, count = 1;
char *filename1 = "file1.txt";
char *filename2 = "file2.txt";
char *filename3 = "file3.txt";
clearFile(filename3);
// to get the count of numbers in file1
getNumbers(filename1);
len = getLength(filename1);
// while loop until var count is equal to the count of numbers in file1
while (count < len) {
++count;
int cur_min = findSmallestNo(filename1, filename3);
copyToFile2(filename1, filename2, cur_min);
copyToFile(filename1, filename2);
}
int number;
// to copy numbers from file3 to file1
copyToFile(filename1, filename3);
clearFile(filename3);
// to print numbers on the screen in file1
print_file(filename1);
return 0;
}
我修復了你的代碼,它似乎工作正常。
您的算法有效,但我認為就像@Bananenkönig 所說的“在文件之間復制效率很低。”。 您的代碼的問題只是處理文件。
您使用 fscanf 讀取文件末尾的方式將多讀 1 次才能找到 eof,這就是為什么您會在結果中得到一些不需要的數字的原因。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.