数组字符串使用C中的qsort排序 [英] Array-strings sorted using qSort in C

查看:225
本文介绍了数组字符串使用C中的qsort排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

的问题很简单:有一些方法的有序阵列,它返回我的qsort,在相反的返回,即我想避免使用任何辅助阵列的使用的qsort投资所得阵列

这是我的code,它从标准输入读取字符串进行排序,并使用一个比较函数进行排序。

 的#include<&stdio.h中GT;
    #包括LT&;&stdlib.h中GT;
    #包括LT&;&unistd.h中GT;
    #包括LT&;&stdio.h中GT;
    #包括LT&;&string.h中GT;            INT cstring_cmp(常量无效*一,常量无效* B)
            {
                为const char ** IA =(为const char **)一个;
                为const char ** IB =(为const char **)B:
                返回strcasecmp(* IA,IB *);
                / * STRCMP功能的工作原理完全从预期
                比较函数* /
            }

在此先感谢您的回复,很抱歉,我的英语

  INT主(INT ARGC,CHAR *的argv [])            {
            INT编号;
            炭温度[4000];            的printf(输入号码:);
            scanf函数(%d个,&安培;号码);            字符* array_string [数字];
            INT I;
            对于(I = 0; I&下;数;我++){
            scanf函数(%[^ \\ n],温度);
            array_string [I] =(字符*)malloc的((strlen的(临时)+1)* sizeof的(炭));
            的strcpy(array_string [I]中,温度);
            }
            为size_t大= sizeof的(array_string)/ sizeof的(字符*);
            的qsort(array_string,大,的sizeof(字符*),cstring_cmp);
            的printf(\\ n);
            的printf(排序数组列表为:\\ n);
            对于(i = 0; I<大;我++)
            的printf(%S \\ n,array_string [I]);
                    返回0;
            }


解决方案

这是不是你想要做什么?

  INT cstring_cmp(常量无效*一,常量无效* B)
        {
            为const char ** IA =(为const char **)一个;
            为const char ** IB =(为const char **)B:
            返回-strcasecmp(* IA,IB *);
            / *返回正常比较的负* /
        }

the question is simple: there is some way that the ordered array that returns me the "qsort", is returned in reverse, ie I want to avoid the use of any auxiliary array to invest the resulting array using qsort.

this is my code, which reads from standard input strings to be sorted, and uses a comparison function for sorting.

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

            int cstring_cmp(const void *a, const void *b)
            {
                const char **ia = (const char **)a;
                const char **ib = (const char **)b;
                return strcasecmp(*ia, *ib);
                /* strcmp functions works exactly as expected from
                comparison function */
            }

Thanks in advance for your response, sorry for my English

            int main (int argc, char *argv [])

            {
            int number;
            char temp [4000];

            printf("input number: ");
            scanf("%d",&number);

            char* array_string [number];
            int i;
            for (i=0;i<number;i++) {
            scanf(" %[^\n]", temp);
            array_string [i] = (char*)malloc((strlen(temp)+1)*sizeof(char));
            strcpy(array_string[i], temp);
            }


            size_t large = sizeof(array_string) / sizeof(char *);
            qsort(array_string,large ,sizeof(char *) ,cstring_cmp );
            printf ("\n");
            printf ("the sorted array list is:\n");
            for (i=0;i<large;i++)
            printf("%s\n", array_string [i]);
                    return 0;
            }

解决方案

Does this do what you want?

        int cstring_cmp(const void *a, const void *b)
        {
            const char **ia = (const char **)a;
            const char **ib = (const char **)b;
            return -strcasecmp(*ia, *ib);
            /* return the negative of the normal comparison */
        }

这篇关于数组字符串使用C中的qsort排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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