如何从至少在C最大的结构进行排序? [英] How to sort structs from least to greatest in C?

查看:109
本文介绍了如何从至少在C最大的结构进行排序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


  1. 程序需要的结构顺序至少要基于最大各个结构的拉链元素。

  2. 输入基于输入/输出重定向。一个input.txt的文件的一个例子是如下:


 贾森·索利斯
20294洛伦扎纳博士
伍德兰希尔斯,CA
91364
罗伯特·史密斯
19831亨肖圣
卡尔弗城,CA
94023
阿鲁姆
5142杜蒙PL
梓,CA
91112


code:

 结构信息{    焦炭名[BUFF_SIZE]
    焦炭stAddress [BUFF_SIZE]
    焦炭cityAndState [BUFF_SIZE]
    焦炭拉链[BUFF_SIZE]
}; 无效选择排序(结构信息* ptrStruct);
INT主要(无效)
{    诠释计数;
    字符缓冲区[600];
    结构信息* ptrStruct [512];    为(计数= 0; COUNT< 18;算++)
    {
        ptrStruct [统计] =(结构信息*)malloc的(的sizeof(结构信息));
        得到(缓冲);
        的strcpy(ptrStruct [计数] - >名,缓冲区);
        得到(缓冲);
        的strcpy(ptrStruct [计数] - > stAddress,缓冲区);
        得到(缓冲);
        的strcpy(ptrStruct [计数] - > cityAndState,缓冲区);
        得到(缓冲);
        的strcpy(ptrStruct [计数] - >拉链,缓冲区);
    }    选择排序(ptrStruct);    的printf(\\ n \\ nLEAST TO GREATEST \\ n);
    为(计数= 0; COUNT< 18;算++)
    {
        的printf(%S \\ n,ptrStruct [统计] - >名);
        的printf(%S \\ n,ptrStruct [统计] - > stAddress);
        的printf(%S \\ n,ptrStruct [统计] - > cityAndState);
        的printf(%S \\ n,ptrStruct [统计] - > ZIP);
    }
}无效选择排序(结构信息* ptrStruct [])
{    INT COUNT2;
    INT COUNT1;
    INT minIndex;
    结构信息* ptrTemporary;    为(COUNT2 = 0; COUNT2&下; 18 - 1; COUNT2 ++)
    {
        minIndex = COUNT2;
        对于(COUNT1 = COUNT2 + 1; COUNT1< 18; COUNT1 ++)
        {
            如果(STRCMP(ptrStruct [COUNT1] - > ZIP,ptrStruct [minIndex] - > ZIP)℃下)
                minIndex = COUNT1;
        }
        ptrTemporary = ptrStruct [COUNT2]
        ptrStruct [COUNT2] = ptrStruct [minIndex]
        ptrStruct [minIndex] = ptrTemporary;
    }
}


解决方案

解决这样的

 的#include<&stdio.h中GT;
#包括LT&;&stdlib.h中GT;
#包括LT&;&string.h中GT;#定义BUFF_SIZE 32
#定义STRUCT_SIZE 512结构信息{
    焦炭名[BUFF_SIZE]
    焦炭stAddress [BUFF_SIZE]
    焦炭cityAndState [BUFF_SIZE]
    焦炭拉链[BUFF_SIZE]
};无效选择排序(结构信息* ptrStruct [],INT大小); //!诠释主要(无效){
    诠释计数,大小; //!
    字符缓冲区[600];
    结构信息* ptrStruct [STRUCT_SIZE]    为(计数= 0; COUNT< STRUCT_SIZE;计数++){
        ptrStruct [统计] =(结构信息*)malloc的(的sizeof(结构信息));
        如果(EOF == scanf函数(599%[^ \\ n]的%* C,缓冲剂)){//!
            免费(ptrStruct [计数]);
            打破;
        };
        的strcpy(ptrStruct [计数] - >名,缓冲区);
        scanf函数(599%[^ \\ n]的%* C,缓冲区);
        的strcpy(ptrStruct [计数] - > stAddress,缓冲区);
        scanf函数(599%[^ \\ n]的%* C,缓冲区);
        的strcpy(ptrStruct [计数] - > cityAndState,缓冲区);
        scanf函数(599%[^ \\ n]的%* C,缓冲区);
        的strcpy(ptrStruct [计数] - >拉链,缓冲区);
    }    大小=计数; //!
    选择排序(ptrStruct,大小); //!    的printf(\\ n \\ nLEAST TO GREATEST \\ n);
    为(计数= 0; COUNT<大小;计数++)//!
    {
        的printf(%S \\ n,ptrStruct [统计] - >名);
        的printf(%S \\ n,ptrStruct [统计] - > stAddress);
        的printf(%S \\ n,ptrStruct [统计] - > cityAndState);
        的printf(%S \\ n,ptrStruct [统计] - > ZIP);
        免费(ptrStruct [计数]);
    }
}无效选择排序(结构信息* ptrStruct [],INT大小)//!
{
    INT COUNT1,COUNT2;
    INT minIndex;
    结构信息* ptrTemporary;    对于(COUNT2 = 0;&COUNT2 LT;大小-1; COUNT2 ++)//!
    {
        minIndex = COUNT2;
        对于(COUNT1 = COUNT2 + 1; COUNT1<大小; COUNT1 ++)//!
        {
            如果(STRCMP(ptrStruct [COUNT1] - >拉链,ptrStruct [minIndex] - > ZIP)℃下)//!
                minIndex = COUNT1;
        }
        如果(minIndex!= COUNT2){
            ptrTemporary = ptrStruct [COUNT2]; //!
            ptrStruct [COUNT2] = ptrStruct [minIndex]
            ptrStruct [minIndex] = ptrTemporary; //!
        }
    }
}

  1. The program needs to order the structures from least to greatest based on the zip element of each individual struct.
  2. The input is based on input/output redirection. An example of an input.txt file is as follows:

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 

code:

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;
    }
}

解决方案

fix like this

#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最大的结构进行排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆