首页 > 解决方案 > 在C中对数据结构数组进行排序

问题描述

我的程序应该读取一个文件,存储该文件的记录,然后对每条记录进行排序。我正在使用插入排序算法对记录进行排序,但是在使其正常工作时遇到了一些问题。

例如,如果 thext 文件看起来像这样

string one; string one; string one; 1
string two; string two; string two; 2
string three; stringh three; string three; 3

我需要第三条记录是第一条,第二条是第二条,第一条是最后一条。由于 3>2>1。

我认为插入排序算法是正确的,因为我用一个简单的数组对其进行了测试并且它正在工作,但是我很难将它实现到我的程序中。我得到的错误是insertionSort’ makes pointer from integer without a cast,我认为这是因为我使用数据结构来存储从文件扫描的数据。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAXLEN 100

struct Element
   {
    char one[100];
    char two[100];
    char three[100];
    int st;
   };

void insertionSort(int arr[]);
void printArray(int arr[]);


int main() {


    int i;
    struct Element elements[MAXLEN];


    FILE * fpointer = fopen("clients.txt", "r");

    char buffer[1024]; // Define a really big string to hold the line of text in
    char *field;
    int field_number;


    while(fgets(buffer,1024,fpointer))
        {
        field_number=0;
        field=strtok(buffer,";");
        while(field)
          {
           switch(field_number)
            {
              case 0:
              strcpy(elements[i].one,field);
              break;
              case 1:
              strcpy(elements[i].two,field);
              break;
              case 2:
              strcpy(elements[i].three,field);
              break;
              case 3:
              elements[i].st=atoi(field);
              break;
            }
         field=strtok(NULL,";"); // Get next field
         field_number++;
         }
        i++; // Move the index for elements to the next one
      }


    insertionSort(elements[MAXLEN].st);
    printArray(elements[MAXLEN].st);

    fclose(fpointer);
    return 0;

}


void insertionSort(int arr[])
{  
   struct Element elements[MAXLEN];
   int i, key, j;
   for (i = 1; i < 10; i++)
    {
       key = elements[i].st;
       j = i-1;


       while (j >= 0 && elements[j].st > key)
       {
           elements[j+1].st = elements[j].st;
           j = j-1;
       }
       elements[j+1].st = key;
    }

}


void printArray(int arr[])
{
int i;
for (i=0; i < 10; i++) {
    printf("%d ", arr[i]);
    printf("\n");
  }
}

标签: carrays

解决方案


您的代码中有以下问题。

您已经声明 了作为参数的void insertionSort(int arr[]); and void printArray(int arr[]); 函数,但您 只是作为参数pointerint arrayinsertionSort(elements[MAXLEN].st); & printArray(elements[MAXLEN].st);int传递。

insertionSort还将对st字段进行 排序,struct Element elements[MAXLEN]; 但不对elements自身insertionSort进行排序,并将按您想要的升序而不是降序对数组进行排序。

因此改变这个

   while (j >= 0 && elements[j].st > key)

   while (j >= 0 && elements[j].st < key.st)

以下面的代码为例。

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #define MAXLEN 100

    struct Element
       {
        char one[100];
        char two[100];
        char three[100];
        int st;
       };

    void insertionSort(struct Element elements[]);
    void printArray(struct Element elements[]);


    int main() {


        int i;
        struct Element elements[MAXLEN];


        FILE * fpointer = fopen("clients.txt", "r");

        char buffer[1024]; // Define a really big string to hold the line of text in
        char *field;
        int field_number;


        while(fgets(buffer,1024,fpointer))
            {
            field_number=0;
            field=strtok(buffer,";");
            while(field)
              {
               switch(field_number)
                {
                  case 0:
                  strcpy(elements[i].one,field);
                  break;
                  case 1:
                  strcpy(elements[i].two,field);
                  break;
                  case 2:
                  strcpy(elements[i].three,field);
                  break;
                  case 3:
                  elements[i].st=atoi(field);
                  break;
                }
             field=strtok(NULL,";"); // Get next field
             field_number++;
             }
           i++; // Move the index for elements to the next one
          }


        insertionSort(elements);
        printArray(elements);

        fclose(fpointer);
        return 0;

    }


void insertionSort(struct Element elements[])
{
   int i, j;
  struct Element key;
   for (i = 1; i < 10; i++)
    {
       key = elements[i];
       j = i-1;


       while (j >= 0 && elements[j].st < key.st)
       {
           elements[j+1] = elements[j];
           j = j-1;
       }
       elements[j+1] = key;
    }

}


    void printArray(struct Element elements[])
    {
    int i;
    for (i=0; i < 3; i++) {
        printf("%s %s %s%d ", elements[i].one,elements[i].two,elements[i].three,elements[i].st);
        printf("\n");
      }
    }

推荐阅读