Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在C中实现文件处理的冒泡排序_C_File_Bubble Sort - Fatal编程技术网

在C中实现文件处理的冒泡排序

在C中实现文件处理的冒泡排序,c,file,bubble-sort,C,File,Bubble Sort,我们今天学习的是文件处理,存储用户输入的分数。我得到了文件处理部分涵盖,但我有一个困难的时间安排分数降序。我正在考虑使用冒泡排序,但我不知道该把它放在哪里,因为我们对文件处理这个话题还是相当陌生的。这是我的密码: #include<stdio.h> #define MAX 100 int main(){ FILE *myPtr; int score=0, choice; char name[MAX]=""; while (choice != 3) { printf("Wh

我们今天学习的是文件处理,存储用户输入的分数。我得到了文件处理部分涵盖,但我有一个困难的时间安排分数降序。我正在考虑使用冒泡排序,但我不知道该把它放在哪里,因为我们对文件处理这个话题还是相当陌生的。这是我的密码:

#include<stdio.h>
#define MAX 100

int main(){
FILE *myPtr;
int score=0, choice;
char name[MAX]="";

while (choice != 3) {
    printf("What do you want to do?\n");
    printf("\t1 - Enter new score\n");
    printf("\t2 - View all scores\n");
    printf("Choice: ");
    scanf("%d", &choice);
    switch (choice){
        case 1:
            printf("Enter name: ");
            scanf("%s", name);
            printf("Enter score: ");
            scanf("%d", &score);
            if ((myPtr = fopen("topscores.txt", "a")) == NULL)
                printf("File could not be opened. :(\n");
            else {
                fprintf(myPtr, "Name:\t%s", name);
                fprintf(myPtr, "Score:\t%d", score);
                fclose(myPtr);
                printf("Score added successfully!\n");
                system("pause");
            }
            break;
        case 2:
            system("cls");
            printf("TOP 10 SCORES:\n\n");
            if ((myPtr = fopen("topscores.txt", "r") )== NULL)
                printf("File could not be opened. :(\n");
            else {
                printf("NAME\tSCORE\n\n");
                fscanf(myPtr, "%s%d", name, &score);
                while (!feof(myPtr)){
                    printf("%s\t%d\n", name, score);
                    fscanf(myPtr, "%s%d", name, &score);
                }
                fclose(myPtr);
                printf("\n");
                system("pause");
            }
            break;
#包括
#定义最大值100
int main(){
文件*myPtr;
int分数=0,选择;
字符名称[MAX]=“”;
while(选项!=3){
printf(“您想做什么?\n”);
printf(“\t1-输入新分数\n”);
printf(“\t2-查看所有分数\n”);
printf(“选择:”);
scanf(“%d”,选择(&C);
开关(选择){
案例1:
printf(“输入名称:”);
scanf(“%s”,名称);
printf(“输入分数:”);
scanf(“%d”和分数);
if((myPtr=fopen(“topscores.txt”,“a”))==NULL)
printf(“无法打开文件::(\n”);
否则{
fprintf(myPtr,“名称:\t%s”,名称);
fprintf(myPtr,“分数:\t%d”,分数);
fclose(myPtr);
printf(“分数添加成功!\n”);
系统(“暂停”);
}
打破
案例2:
系统(“cls”);
printf(“前10名分数:\n\n”);
如果((myPtr=fopen(“topscores.txt”,“r”))==NULL)
printf(“无法打开文件::(\n”);
否则{
printf(“名称\t存储\n\n”);
fscanf(myPtr,“%s%d”、姓名和分数);
而(!feof(myPtr)){
printf(“%s\t%d\n”,名称,分数);
fscanf(myPtr,“%s%d”、姓名和分数);
}
fclose(myPtr);
printf(“\n”);
系统(“暂停”);
}
打破

我是将冒泡排序放在案例2中还是案例1中?请提供帮助。

最好将冒泡排序放在案例(2)中,因为如果将冒泡排序放在案例(1)中,则每次向文件添加项目时都必须对文件重新排序,这样效率会很低。请注意,在案例(2)中您需要首先将文件中的所有项加载到内存中的数据结构中,然后对数据结构进行排序,然后迭代数据结构以打印排序结果。输入和输出的格式不同