如何对结构中的字符串进行词法排序?

时间:2021-11-03 19:20:26

I'm trying to sort strings according to their names lexicographically.

我正试图根据字符串的名称来进行词法排序。

so I have an array of structures

我有一个结构数组

typedef struct buff{
    char *name;
} structure;

and I'm copying names of files with their associated extensions. So that the content of that structure looks like s[0].name = "picture1.jpg" s[1].name = "DCP003.JPG" and stuff like that.

我用相关的扩展名复制文件的名称。因此,该结构的内容看起来像s[0].name = "picture1.jpg" s[1].name = "DCP003 "。诸如此类的东西。

and I'm trying to sort that, and I'm unable to achieve that.. what I have so far is this.

我想把它分类,但我做不到。到目前为止我得到的是这个。

void sort(structure *s, int counter){

    for (int i = 0; i < counter - 1; i++){
        for (int j = 0; j < counter - 1 - i; j++){

                if (strcmp(s[j].name, s[j+1].name) > 0){

                    structure tmp;

                    tmp = s[j];
                    s[j] = s[j+1];
                    s[j+1] = tmp;

            }
        }
    }

    for (int i = 0; i < counter; i++){
        printf("%d - %s\n", i+1, s[i].name);
    }
}

and it doesn't work as I want.. tried several versions still no good .. where am I making mistake? Any advice is greatly appreciated..

它并不能像我想的那样工作。试过几个版本仍然不行。我哪里出错了?非常感谢您的建议。

2 个解决方案

#1


2  

Try something like this:

试试这样:

typedef struct buff{
    char *name;
} structure;

structure s[5];

void sort(){
    int sz = sizeof s / sizeof s[0];
    printf("sz=%d\n",sz);
    int done = 0;

    for (int i = 0; i < sz; i++){
          for (int j = i+1; j < sz; j++){

                  if (strcmp(s[i].name, s[j].name) > 0){

                      structure tmp;

                      tmp = s[i];
                      s[i] = s[j];
                      s[j] = tmp;
              }
          }
    }
    for (int i = 0; i < sz; i++){
        printf("%d - %s\n", i+1, s[i].name);
    }
}

int main() {
  s[0].name = "dog";
  s[1].name = "ant";
  s[2].name = "cat";
  s[3].name = "man";
  s[4].name = "bear";
  sort();
  return 0;
}

Output:

输出:

---------- Capture Output ----------
> "c:\windows\system32\cmd.exe" /c c:\temp\temp.exe
sz=5
1 - ant
2 - bear
3 - cat
4 - dog
5 - man

> Terminated with exit code 0.

#2


3  

The best way to sort strings in a structure lexicographically order is using QSort (Stdlib.h)(O(nlog(n))).

按词法顺序对结构中的字符串排序的最佳方法是使用QSort (Stdlib.h)(O(nlog(n)))。

This is the Sample Code::

这是样本代码:

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


struct names
{
    char strvalues[20];
} buff[100];


int main ()
{
    int i;
    strcpy(buff[0].strvalues,"some");
    strcpy(buff[1].strvalues,"example");
    strcpy(buff[2].strvalues,"strings");
    strcpy(buff[3].strvalues,"here");

    qsort (buff, 4, 20, (int(*)(const void*,const void*)) strcmp);


    for(i=0;i<4;++i)
    {
        printf("%s\n",buff[i].strvalues);    
    }

    return 0;
}

#1


2  

Try something like this:

试试这样:

typedef struct buff{
    char *name;
} structure;

structure s[5];

void sort(){
    int sz = sizeof s / sizeof s[0];
    printf("sz=%d\n",sz);
    int done = 0;

    for (int i = 0; i < sz; i++){
          for (int j = i+1; j < sz; j++){

                  if (strcmp(s[i].name, s[j].name) > 0){

                      structure tmp;

                      tmp = s[i];
                      s[i] = s[j];
                      s[j] = tmp;
              }
          }
    }
    for (int i = 0; i < sz; i++){
        printf("%d - %s\n", i+1, s[i].name);
    }
}

int main() {
  s[0].name = "dog";
  s[1].name = "ant";
  s[2].name = "cat";
  s[3].name = "man";
  s[4].name = "bear";
  sort();
  return 0;
}

Output:

输出:

---------- Capture Output ----------
> "c:\windows\system32\cmd.exe" /c c:\temp\temp.exe
sz=5
1 - ant
2 - bear
3 - cat
4 - dog
5 - man

> Terminated with exit code 0.

#2


3  

The best way to sort strings in a structure lexicographically order is using QSort (Stdlib.h)(O(nlog(n))).

按词法顺序对结构中的字符串排序的最佳方法是使用QSort (Stdlib.h)(O(nlog(n)))。

This is the Sample Code::

这是样本代码:

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


struct names
{
    char strvalues[20];
} buff[100];


int main ()
{
    int i;
    strcpy(buff[0].strvalues,"some");
    strcpy(buff[1].strvalues,"example");
    strcpy(buff[2].strvalues,"strings");
    strcpy(buff[3].strvalues,"here");

    qsort (buff, 4, 20, (int(*)(const void*,const void*)) strcmp);


    for(i=0;i<4;++i)
    {
        printf("%s\n",buff[i].strvalues);    
    }

    return 0;
}