gpt4 book ai didi

c - 从文本文件中读取整数并将它们排序到另一个文本文件和二进制文件中

转载 作者:行者123 更新时间:2023-11-30 18:01:31 25 4
gpt4 key购买 nike

我忘记包含编程语言(应该是 C 语言)。

我需要帮助来完成这个程序。示例代码将不胜感激。

程序读取包含以下内容的文件:

a.要排序的整数数量,后跟 b.要排序的整数(每行一个整数)(必须与指定的整数个数相同)。

然后它将按照从低到高的顺序整理到另一个文本文件和一个二进制文件中。

其他规范:

  1. 使用动态内存分配
  2. 终端中的格式应为:

    ./program.out  original-file.txt  output-file.txt  output-file.bin

其中program.out是程序本身,original-file.txt是文本文件,其中包含要排序的整数的数量和未排序的整数以及output-file.txtoutput-file.bin 包含排序后的整数。

错误检查:

  1. 检查 malloc() 是否成功返回

原始文件将如下所示:

3 #number of integers to be sorted
3 #the integers-separated by new line
2
1

输出文件:

3
1
2
3

提前非常感谢您:)上帝保佑!

最佳答案

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct tree {
int num, cnt;
struct tree *left, *right;
};
struct tree *add(struct tree *t, int val) {
if (!t) {
if (!(t = malloc(sizeof(struct tree))))
perror("Not enough memory"), exit(-1);
memset(t, 0, sizeof(struct tree));
t->num = val;
++t->cnt;
return t;
}
if (val < t->num)
t->left = add(t->left, val);
else if (val > t->num)
t->right = add(t->right, val);
else
++t->cnt;
return t;
}
int walk(struct tree *t, int (*f)(struct tree *, void*), void *data) {
int rc;
if (!t)
return 0;
rc = walk(t->left, f, data);
rc += f(t, data);
rc += walk(t->right, f, data);
return rc;
}
struct tree *clean(struct tree *t) {
if (!t)
return NULL;
t->left = clean(t->left);
t->right = clean(t->right);
free(t);
return NULL;
}
int save(struct tree *t, void *data) {
int i, rc = 0;
FILE *fp = (FILE *) data;
for (i = 0; i < t->cnt; ++i)
rc += (fprintf(fp, "%d\n", t->num) < 0);
return rc;
}
int saveb(struct tree *t, void *data) {
int i, rc = 0;
FILE *fp = (FILE *) data;
for (i = 0; i < t->cnt; ++i)
rc += (fwrite((void *) &t->num, sizeof t->num, 1, fp) != 1);
return rc;
}
int main(int argc, char **argv) {
int rc = 0;
struct tree *t = NULL;
char buff[0x200];
FILE *fin, *fout, *foutb;
if (argc < 4) {
fprintf(stderr, "Usage: %s SRCFILE OUTFILE.TXT OUTFILE.BIN\n", argv[0]);
exit(0);
}
if (!((fin = fopen(argv[1], "r")) && (fout = fopen(argv[2], "w"))
&& (foutb = fopen(argv[3], "wb")))) {
perror("fopen");
exit(-1);
}
while (fgets(buff, sizeof buff, fin))
t = add(t, atoi(buff));
rc += walk(t, save, (void *) fout);
rc += walk(t, saveb, (void *) foutb);
t = clean(t);
fclose(fin);
fclose(fout);
fclose(foutb);
return rc;
}

刚刚注意到“文件第一个字符串中的整数数量”规范;我想你的老师建议你把它们排列起来并排序;但无论如何你自己做:)

#include <stdio.h>
#include <stdlib.h>
int cmp(const void *a, const void *b) {
return *(int*)a > *(int*)b ? 1 : (*(int*)a < *(int*)b ? -1 : 0);
}
int main(int argc, char **argv) {
char buff[0x200];
FILE *fin, *fout, *foutb;
int i, *arr, sz = 0, rc = 0;
if (argc < 4) {
fprintf(stderr, "Usage: %s SRCFILE OUTFILE.TXT OUTFILE.BIN\n", argv[0]);
exit(0);
}
if (!((fin = fopen(argv[1], "r")) && (fout = fopen(argv[2], "w"))
&& (foutb = fopen(argv[3], "wb")))) {
perror("fopen");
exit(-1);
}
if (fgets(buff, sizeof buff, fin)) {
sz = atoi(buff);
if (!(arr = malloc(sizeof(int) * sz)))
perror("Not enough memory"), exit(-1);
for (i = 0; i < sz && fgets(buff, sizeof buff, fin); ++i)
arr[i] = atoi(buff);
}
qsort(arr, sz, sizeof(int), cmp);
for (i = 0; i < sz; ++i)
rc += (fprintf(fout, "%d\n", arr[i]) < 0);
for (i = 0; i < sz; ++i)
rc += (fwrite((void *) &arr[i], sizeof(int), 1, foutb) != 1);
fclose(fin);
fclose(fout);
fclose(foutb);
return rc;
}

关于c - 从文本文件中读取整数并将它们排序到另一个文本文件和二进制文件中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9789577/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com