C - 如何对数组中的错误元素进行排序

卡特琳·凯洛

将带有名称和结果的文本文件放入 ac 程序以按结果排序。如何忽略下面文件中的错误插入,例如数字 8?


.txt 代码如下所示

保罗 35.6

彼得 53.4

杰克 23.0

8

步长 0.0

阿曼达 43.5


第一个数组是 arrayName[],第二个是 arrayResult[],arrayFail[] 是 0.0 点。

#define ARRAYSIZE 15 // THIS IS THE MACRO FOR DEFINING THE ARRAY SIZE
#define STRSIZ 40 // THIS IS THE MACRO FOR DEFINING THE STRING SIZE

int main(){
    /* Declaration of variables */
    int i, j, n, f = 0;
    float a, m =0;
    char c;
    
    /* Defining the arrays for names and results */
    char arrayName[ARRAYSIZE][STRSIZ];
    char arrayTemp[ARRAYSIZE][STRSIZ];
    char arrayFail[ARRAYSIZE][STRSIZ];
    float arrayResults[ARRAYSIZE];
    
    n = ARRAYSIZE;
    
    /* Code to open file and check list for validation */   
    FILE *fp;
    
    fp = fopen("List.txt", "r"); //Opens the file
     
     
     while ((c = fgetc(fp)) != EOF){
         if (c == '\n')
         m = m +1;
     }
     n = m;
    
    
    // Close the file 
    fclose(fp);
    
    /* Validates the name list */
    while( n <= 0 || n > 15){
        printf("\nThe list is invalid. It must be atleast one name and a maximum of 15 names. Please edit List.txt and re-run the program\n");
        return 0;
    }
    
    /* Here the program scans the list from the text file */
    for(i = 0; i < n; i++){
        scanf("%s %f", arrayName[i], &arrayResults[i]);
        
    }   
    
    /* Printing out the competitors list */
    printf("The competitors: ");
    
    for(i = 0; i < n - 1; i++){
        printf("%s, ", arrayName[i]);
        
    }
    for(i = n - 1; i < n; i++){
        printf("%s.", arrayName[i]);
        
    }
    
    
    /* The array is ordered */
    for(i = 0; i < n; i++){
        for(j = i + 1; j < n; j++){
            if( arrayResults[i] < arrayResults[j]){
                a = arrayResults[i];
                strcpy(arrayTemp[i], arrayName[i]);
                arrayResults[i] = arrayResults[j];
                strcpy(arrayName[i], arrayName[j]);
                arrayResults[j] = a;
                strcpy(arrayName[j], arrayTemp[i]);
                
            }
        }
    }

    /* Printing out the Top 3 */
    printf("\n\nThe Top 3:\n");
    
    for(i = 0; i < 3; i++)
    printf("No%d: %s with %.1f\n", i+1, arrayName[i], arrayResults[i]);
    
        
    /* Failed competitors and their names are detected and stored */
    for(i = 0; i < n; i++){
        if( arrayResults[i] == 0 ){
            strcpy(arrayFail[f], arrayName[i]);
            f++;
        }
    }
    
    
    /* Printing out the failed competitors */
    printf("\nThe failed competitors were: ");
    for(i = 0; i < f - 1; i++){
        printf("%s, ", arrayFail[i]);
    }
    printf("%s.", arrayFail[f - 1]);
    printf("\n");
    
    return 0;
}
数字信号处理器

您可以在收集期间测试每个条目的有效性,并在测试失败时绕过它(您可以根据自己的目标调整测试,这里我们只测试字符串不为空):

int countEntry = 0;
for(i = 0; i < n; i++){
    scanf("%s %f", arrayName[i], &arrayResults[i]);

    // If name or score is null, decrease i to ignore that entry
    if (arrayName[i][0] == 0 || arrayResults[i][0] == 0) i--;
    // Else counts the entry
    else countEntry++;
}

那么你应该控制至少有一个有效的条目:

if(countEntry == 0) {
    printf("\nThe list is invalid. It must be atleast one name. Please edit List.txt and re-run the program\n");
    return 0;
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

对数组C中的元素进行排序

如何在C ++中对数组进行排序和检查?

如何在C中对数字数组进行排序

对数组元素进行排序以找到C中的最大和最小数字

按C#中元素的频率对数组进行排序

使用C中的Bucket Sort对数组进行排序。有关在链接列表中插入新元素的代码如何工作的问题

在C中对数组中的值进行排序

如何在 C++ 中以特定顺序仅对数组的某些部分进行排序?

Obj-c - 在字典中对数组进行排序?

我在C程序中对数组进行排序时出错

在C中对数组进行排序和分析

使用C中的指针按升序对数组进行排序

用C对数组进行冒泡排序

如何在C ++中从对数字进行排序到按字母顺序进行排序

在 C 中对数字进行排序

如何在Objective-C中对可以交换两个元素的数组进行排序

计数排序混乱。不对数组进行排序(C ++)

仅对数组的一半进行操作的个性化排序算法。(在 C 中)

用函数对数组进行排序而不使用 C 中的指针是行不通的。为什么?

仅使用绝对值对数组进行排序,并在C ++中显示实值

在C中对数组进行算术运算

如何在C / C ++中对多维数组进行排序

c#使用IComparable接口对数组进行排序

用C对数据结构数组进行排序

对数组C进行部分排序

按订单C#对数组日期进行排序

使用指针对数组进行排序-C编程

C编程:使用指针对数组进行排序的函数

根据比较函数对数组进行排序 C++