c - C语言中如何对结构体进行从小到大排序?

标签 c sorting pointers data-structures struct

  1. 程序需要根据每个结构体的 zip 元素从最小到最大对结构进行排序。
  2. 输入基于输入/输出重定向。 input.txt 文件的示例如下:
Jason Solis   
20294 Lorenzana Dr  
Woodland Hills, CA   
91364   
Robert Smith   
19831 Henshaw St   
Culver City, CA   
94023   
Bob Arum  
5142 Dumont Pl   
Azusa, CA   
91112 

代码:

struct info {

    char name[BUFF_SIZE];                                
    char stAddress[BUFF_SIZE];                                      
    char cityAndState[BUFF_SIZE]; 
    char zip[BUFF_SIZE];
};

 void selectionSort(struct info *ptrStruct);




int main(void)
{

    int count;                       
    char buffer[600];
    struct info *ptrStruct[512];

    for (count = 0; count < 18; count++)
    {
        ptrStruct[count] = (struct info*) malloc(sizeof(struct info));  
        gets(buffer);
        strcpy(ptrStruct[count]->name, buffer);         
        gets(buffer);   
        strcpy(ptrStruct[count]->stAddress, buffer);    
        gets(buffer);
        strcpy(ptrStruct[count]->cityAndState, buffer); 
        gets(buffer);
        strcpy(ptrStruct[count]->zip, buffer);          
    }

    selectionSort(ptrStruct);

    printf("\n\nLEAST TO GREATEST\n");
    for (count = 0; count < 18; count++)
    {
        printf("%s\n", ptrStruct[count]->name);
        printf("%s\n", ptrStruct[count]->stAddress);
        printf("%s\n", ptrStruct[count]->cityAndState);
        printf("%s\n", ptrStruct[count]->zip);
    }
}

void selectionSort(struct info *ptrStruct[])
{

    int count2;
    int count1;
    int minIndex;
    struct info *ptrTemporary;      

    for (count2 = 0; count2 < 18 - 1; count2++)
    {
        minIndex = count2;
        for (count1 = count2 + 1; count1 < 18; count1++)
        {
            if (strcmp(ptrStruct[count1]->zip, ptrStruct[minIndex]->zip) < 0)
                minIndex = count1;
        }
        ptrTemporary = ptrStruct[count2];
        ptrStruct[count2] = ptrStruct[minIndex];
        ptrStruct[minIndex] = ptrTemporary;
    }
}

最佳答案

像这样修复

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define BUFF_SIZE 32
#define STRUCT_SIZE 512

struct info {
    char name[BUFF_SIZE];
    char stAddress[BUFF_SIZE];
    char cityAndState[BUFF_SIZE]; 
    char zip[BUFF_SIZE];
};

void selectionSort(struct info *ptrStruct[], int size);//!

int main(void){
    int count, size;//!
    char buffer[600];
    struct info *ptrStruct[STRUCT_SIZE];

    for (count = 0; count < STRUCT_SIZE; count++){
        ptrStruct[count] = (struct info*) malloc(sizeof(struct info));
        if(EOF==scanf("%599[^\n]%*c", buffer)){//!
            free(ptrStruct[count]);
            break;
        };
        strcpy(ptrStruct[count]->name, buffer);
        scanf("%599[^\n]%*c", buffer);
        strcpy(ptrStruct[count]->stAddress, buffer);
        scanf("%599[^\n]%*c", buffer);
        strcpy(ptrStruct[count]->cityAndState, buffer);
        scanf("%599[^\n]%*c", buffer);
        strcpy(ptrStruct[count]->zip, buffer);
    }

    size = count;//!
    selectionSort(ptrStruct, size);//!

    printf("\n\nLEAST TO GREATEST\n");
    for (count = 0; count < size; count++)//!
    {
        printf("%s\n", ptrStruct[count]->name);
        printf("%s\n", ptrStruct[count]->stAddress);
        printf("%s\n", ptrStruct[count]->cityAndState);
        printf("%s\n", ptrStruct[count]->zip);
        free(ptrStruct[count]);
    }
}

void selectionSort(struct info *ptrStruct[], int size)//!
{
    int count1, count2;
    int minIndex;
    struct info *ptrTemporary;

    for (count2 = 0; count2 < size -1; count2++)//!
    {
        minIndex = count2;
        for (count1 = count2 + 1; count1 < size; count1++)//!
        {
            if (strcmp(ptrStruct[count1]->zip, ptrStruct[minIndex]->zip) < 0)//!
                minIndex = count1;
        }
        if(minIndex != count2){
            ptrTemporary = ptrStruct[count2];//!
            ptrStruct[count2] = ptrStruct[minIndex];
            ptrStruct[minIndex] = ptrTemporary;//!
        }
    }
}

关于c - C语言中如何对结构体进行从小到大排序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36965627/

相关文章:

sorting - Lodash 按值排序对象,不会丢失键

sorting - PARTITIONED BY、CLUSTERED BY 和 SORTED BY 与 BUCKETS 之间的 Hive 区别以及插入与 PARTITIONED 和 CLUSTER BY 的覆盖?

c++ - 如何创建通用函数,它将返回 C++ 中任何级别指针的值?

c++ - 数组和指针

c# - pinvoke c 函数 - System.BadImageFormatException

c - 在C中从文件到文件的读取和写入

c - 来自整数数组的随机数? uVision 4 中的 C

c - 将幂视为 C 语言中 2^2 的写法

sorting - sortedBy 参数作为变量

c - 使用 C 返回数组