【问题标题】:Function that sorts an array of structures alphabetically by one of their string values按其中一个字符串值按字母顺序对结构数组进行排序的函数
【发布时间】:2015-12-05 17:17:10
【问题描述】:

对于我的一项作业,我需要编写一个代码,该代码从一个文本文件中获取输入,该文本文件的顶部有许多可用的助教,之后的每一行都包含名称、日期、开始时间和结束时间的值.

现在我有读取文件的函数和打印输出的函数,它们按照我需要的方式工作,但是每当我包含我编写的排序函数时,程序就会崩溃。

我在嵌套 forloop 中使用 strcmp() 对排序函数做了什么,以便循环遍历数组并确定哪些值按字母顺序取代另一个值。然后我使用 strcpy() 和一个临时结构来存储一个值,将一个值与另一个交换,然后将另一个值与临时结构中存储的值交换。

我认为问题可能与我在结构中使用 strcpy() 的方式有关,因为我认为它的工作方式是我调用地址而不是实际值,我可能需要使用指针,但我'不完全确定。这是给我带来麻烦的功能:

void sort_data(struct Shift shift_data[],int num_shifts)
{

int x,y;


for(x = 0 ; x < num_shifts  ; x++)
{
    for(y = 0; y < num_shifts ; y++)
    {
        if(strcmp(shift_data[x].name,shift_data[y].name)<0)
        {
            strcpy(temp.name, shift_data[x].name);
            strcpy(shift_data[y].name, shift_data[x].name);
            strcpy(shift_data[y].name, temp.name);

        }
        if(strcmp(shift_data[x].name,shift_data[y].name)<0)
        {
             strcpy(temp.day_of_week, shift_data[x].day_of_week);
            strcpy(shift_data[y].day_of_week, shift_data[x].day_of_week);
            strcpy(shift_data[y].day_of_week, temp.day_of_week);
        }
       if(strcmp(shift_data[x].name,shift_data[y].name)<0)
        {
            strcpy(temp.start_hour, shift_data[x].start_hour);
            strcpy(shift_data[y].start_hour, shift_data[x].start_hour);
            strcpy(shift_data[y].start_hour, temp.start_hour);
        }
     if(strcmp(shift_data[x].name,shift_data[y].name)<0)
        {
             strcpy(temp.end_hour, shift_data[x].end_hour);
            strcpy(shift_data[x].end_hour, shift_data[y].end_hour);
            strcpy(shift_data[y].end_hour, temp.end_hour);
        }

}

}
}

这是完整的代码:

// Henry Moench
// 12/2/15
// COP3223C-15FALL 0002
// Homework 7

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

//initializing structures
struct Shift
     {
        char name[100];
        char day_of_week[100];
        int start_hour;
        int end_hour;
     };

struct Shift shift_data[100];
struct Shift temp;

int main()
{
//initializing variable and calling functions
int num_shifts;

     num_shifts= read_data(shift_data);

     sort_data(shift_data, num_shifts);

     print_data(shift_data, num_shifts);

    return 0;
}


//creating function that reads the text file indicated by the user
//and then storing the data into a structure array
int read_data(struct Shift shift_data[])
     {

     char input[100];
     int x;
     int num_shifts;
     printf("What is the name of the file? ");
        scanf("%s", &input);
        FILE *ifp;
        ifp = fopen(input, "r");
        fscanf(ifp, "%d", &num_shifts);

for(x = 0 ; x < num_shifts; x++)
        {

            fscanf(ifp, "%s", &shift_data[x].name);
            fscanf(ifp, "%s", &shift_data[x].day_of_week);
            fscanf(ifp, "%d", &shift_data[x].start_hour);
            fscanf(ifp, "%d", &shift_data[x].end_hour);
        }
        fclose(ifp);
        return num_shifts;
     }

//creates function that prints out the output in the format specified
void print_data(struct Shift shift[], int num_shifts)
    {
   int x;
   printf("TA Shifts \n");
   printf("================================================ \n");

   for(x = 0; x < num_shifts; x++)
   {
   printf(" %s",shift_data[x].name);
   printf(" %s",shift_data[x].day_of_week);


   if(shift_data[x].start_hour>12)
   {
       shift_data[x].start_hour=shift_data[x].start_hour-12;
       printf("%2d:00 pm to  ", shift_data[x].start_hour);
   }
   else if(shift_data[x].start_hour<12)
   {
       printf("%2d:00 am to  ", shift_data[x].start_hour);
   }
   else if(shift_data[x].start_hour==12)
   {
       printf("%2d:00 pm to  ", shift_data[x].start_hour);
   }


   if(shift_data[x].end_hour>12)
   {
       shift_data[x].end_hour=shift_data[x].end_hour-12;
       printf("%2d:00 pm\n", shift_data[x].end_hour);
   }
   else if(shift_data[x].end_hour<12)
   {
       printf("%2d:00 am\n", shift_data[x].end_hour);
   }
   else if(shift_data[x].end_hour==12)
   {
       printf("%2d:00 pm\n", shift_data[x].end_hour);
   }

   }
    }


void sort_data(struct Shift shift_data[],int num_shifts)
{

int x,y;


for(x = 0 ; x < num_shifts  ; x++)
{
    for(y = 0; y < num_shifts ; y++)
    {
        if(strcmp(shift_data[x].name,shift_data[y].name)<0)
        {
            strcpy(temp.name, shift_data[x].name);
            strcpy(shift_data[y].name, shift_data[x].name);
            strcpy(shift_data[y].name, temp.name);

        }
        if(strcmp(shift_data[x].name,shift_data[y].name)<0)
        {
             strcpy(temp.day_of_week, shift_data[x].day_of_week);
            strcpy(shift_data[y].day_of_week, shift_data[x].day_of_week);
            strcpy(shift_data[y].day_of_week, temp.day_of_week);
        }
       if(strcmp(shift_data[x].name,shift_data[y].name)<0)
        {
            strcpy(temp.start_hour, shift_data[x].start_hour);
            strcpy(shift_data[y].start_hour, shift_data[x].start_hour);
            strcpy(shift_data[y].start_hour, temp.start_hour);
        }
     if(strcmp(shift_data[x].name,shift_data[y].name)<0)
        {
             strcpy(temp.end_hour, shift_data[x].end_hour);
            strcpy(shift_data[x].end_hour, shift_data[y].end_hour);
            strcpy(shift_data[y].end_hour, temp.end_hour);
        }

}

}
}

【问题讨论】:

    标签: c arrays sorting struct


    【解决方案1】:

    崩溃可能是因为您尝试在 int 字段上使用 strcpy(),但它无法在该字段上运行(请阅读 strcpy() 函数的描述以找出原因)。

    我建议您在排序时以不同的方式管理数据。

    不要对结构数组进行排序,而是将数组作为索引值数组进行处理。

    所以你在同一个数组中读取了原始数据。

    但是你对一个数组进行排序,只使用该数组的索引值。

    int tmp[sizeof(shift_data)] ;
    
    int i = 0 ;
    
    for( i = 0 ; i < sizeof(tmp) ; i++ )
        tmp[i] = i ;
    

    现在,当您想要交换数组的元素 k 和 j 时,您只需这样做:

    int m ;
    
    m = tmp[j] ;
    tmp[j] = tmp[k] ;
    tmp[k] = m ;
    

    这比你的 strcpy() 东西快很多

    要和名字做比较

    int res ;
    
    res = strcmp( shift_data[ tmp[j] ].name, shift_data[ tmp[k] ].name ) ;
    

    请注意,在您的代码中,您执行以下等效操作:

    if ( A.n < B.n )
        swap( A.n, B.n ) ;
    
    if ( A.n < B.n )
        swap( A.x, B.x ) ;
    

    但是如果您需要交换因为 A.n

    您应该将所有交换操作都包含在一个 if() 语句中。如果您使用我描述的索引数组方法,您将不需要做所有这些,只需比较即可。

    当您对最终数组进行排序后,您可以使用以下命令按顺序打印它

    int i = 0 ;
    
    for( i = 0 ; i < sizeof(tmp) ; i++ )
    {
        printf( "name is %s\n", shift_data[ tmp[i] ].name ) ;
    }
    

    如果需要,您可以在排序后移动 shift_data 元素。

    最后你应该看看不同的排序算法。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-03-23
      • 2015-09-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多