1

我想按字母顺序对数组进行排序,如下所示:

我尝试了各种方法,但仍然没有办法。它崩溃了,还不知道为什么。你有一些提示可以解决这个问题吗?

由于我不是那么专家,因此代码旨在简单(阅读/理解)并完成工作。

谢谢,问候

/* note: datalist[...] is basically the ouput from a sort of ls (dir
read) that it unsorted. So that: datalist[0] is ".." datalist[1] is
"file2.c" datalist[2] is "file34.c" and so on.*/


    char datalist[500][2024] ; 


void sortData(int aoptiondt)  {   ///////////////////////////LOCAL
DECLARATIONS/////////   int MAX = 500 ;    int current; int walker;  
int smallestIndex;   char* temp;

  ///////////////////////////DEFINITIONS//////////////////



    for (current = 0; current < MAX - 1; current++)
    {
      smallestIndex = current;
      for (walker = current; walker < MAX ; walker ++)
      {
        if (strcmp(datalist[walker], datalist[smallestIndex]) < 0)
          smallestIndex = walker;
      } //for walker

      //Swap to position smallest at what is the current position
      strncpy( temp , datalist[current] , PATH_MAX);
      strncpy( datalist[current] , datalist[smallestIndex] , PATH_MAX);
      strncpy( datalist[smallestIndex] , temp, PATH_MAX);
    } //for current    }

  return; }


//blabla
    int  main() {



    }
4

3 回答 3

2

一如既往 -qsort是你最好的朋友:

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

#define LENGTH 6
#define STRING_SIZE 4
#define STRINGS "eed", "abd", "cde", "abc", "acd", "ade"

char strUnsorted[][STRING_SIZE] = {STRINGS};
char strSorted[][STRING_SIZE]   = {STRINGS};

int compare (const void * a, const void * b) {
  return strcmp(a, b);
}

int main () {

  qsort(strSorted, LENGTH, STRING_SIZE, compare);

  printf("Unsorted | Sorted\n");
  printf("-----------------\n");

  int i;
  for (i=0; i < LENGTH; i++)
    printf ("  %s    |   %s\n", strUnsorted[i] ,strSorted[i]);

  return 0;
}
于 2013-02-20T08:33:43.087 回答
1

您不仅需要一个字符数组,还需要一个字符数组数组。像往常一样使用 C,您需要小心内存管理,并保持分配数组的边界。请参阅下面的解决方案。我使用了一个简单的选择排序算法,它非常适合这样的应用程序。这是如何完成的:

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

#define N 5
#define MAX_STRLEN 80

void sort(char **a, int n);
char *stringArray[N];

int main(int argc, char *argv[]) 
{
   int i;

   printf("\nEnter %d names, one per line:\n",N);
   for (i = 0; i < N; i++) 
   {
      stringArray[i] = (char *)malloc(MAX_STRLEN);
      strcpy(stringArray[i],"");
      printf("> ");
      fgets(stringArray[i],MAX_STRLEN,stdin);
      *strchr(stringArray[i],'\n') = '\0';
   }
   sort(stringArray,N);
   printf("\nSorted:\n");
   for (i = 0; i < N; i++) 
   {
      puts(stringArray[i]);
      free(stringArray[i]);
   }

   return 0;
}

/* selection sort */
void sort(char **a, int n) 
{
   int min,i,j;
   char t[MAX_STRLEN];

   for (i = 0; i < n; i++) 
   {
      min = i;
      for (j = i+1; j < n; j++) 
      {
        if (strcmp(a[j],a[min]) < 0) 
          min = j;
      }
      strcpy(t,a[min]);
      strcpy(a[min],a[i]);
      strcpy(a[i],t);
   }
}
于 2013-02-20T08:03:42.993 回答
-1

http://www.asciitable.com/index/asciifull.gif这里是一个 ascii 表。如果你看这个,你会发现性格是有区别的。这意味着您可以将 char 转换为 int 并获取其 ascii 编号,然后您可以使用该编号对它们进行排序。

一个样品:

  int len =strlen(word);
  int wordWeight=0;
  for(int i=0;i<len;i++)
  {
      wordWeight+=(int)word[i];
      wordWeight*=1000;
  }

通过使用 wordWeight 你可以对它们进行排序。注意:我们将 ascii 值乘以 1000,因为 ascii 值在 0 和 255 之间变化,但如果某些单词以大写字母开头而其他单词以 lovercase 开头,您将遇到问题。但我认为你可以应付

于 2013-02-20T08:07:05.097 回答