c - 如何在通用排序函数中添加排序顺序选项

标签 c arrays function sorting generics

这是通用排序函数中使用的比较函数。 xy通用排序函数要比较的元素。

int CompInt(void *x, void *y)
{
  int a = *((int*)x);
  int b = *((int*)y);
  int choice;
  printf("How do you want to sort ? \n\t1. Ascending \n\t2. Descending\n");
  scanf(" %d ", &choice);
  if(choice == 1)
    {
        if(a > b) return -1;
        else return 1;
    }
  if(choice == 2)
    {
        if(a > b) return 1;
        else return -1;
    }
}

调用函数不断调用CompInt一次又一次地运行而不执行比较。它不断打印

how do you want to sort?`  
1. ascending 
2. descending

有什么办法可以设置升序或降序选项吗?

这就是我调用该函数的方式

if(compareFcn(*1st element*, *2nd element*) > 0);

最佳答案

  1. 我认为你应该创建两个函数。如果选择升序。 像这样。

    int Ascending(void * a, void * b)  
    {  
        int inter_a = *((int*)(a)); int inter_b = *((int*)(b));
    
        if(inter_a > inter_b) 
             return -1;
        else 
             return 1;
    }
    
    int Descending(void * a, void * b)  
    {  
        int inter_a = *((int*)(a)); int inter_b = *((int*)(b));
    
        if(inter_a < inter_b) 
             return -1;
        else 
             return 1;
    }
    

    用户可以在主功能中选择。使用升序或降序。并且您可以调用用户选择的函数。

    if(choice == 1)
    {
         Sort((void*)num, Ascending, sizeof(int), max);
    }
    else
    {
         Sort((void*)num, Descending, sizeof(int), max);
    }
    
  2. 还有 CompStr。我认为你应该使用 string.h。

CompStr 有问题。如果 a[i] == 'a',则 b[i] == 'D'。 CompStr 认为 a[i] 是 comp 首先。你应该这样使用。

    int CompStr(void *x, void *y)
    {
        char *a = (char*)x;
        char *b = (char*)y;
        int c1 = 0, c2 = 0, cmp = 0, i = 0;
        while(a[c1] != '\0') c1 += 1;
        while(b[c2] != '\0') c2 += 1;
        while((i < c1) && (i < c2))
        {  
            char compA = tolower(a[i]);
            char compB = tolower(b[i]);
            if(compA == compB)
            {
                 i++;
                 continue;
            }
            if(compA < compB)
            {
                 cmp = (-1);
                 break;
            }
            if(compA > compB)
            {
                 cmp = 1;
                 break;
            }
         }
         return cmp;
     }

和测试代码 =

int main()
{
    //first exmaple.
    char * str1 = "Hallo.";
    char * str2 = "Hello.";

    //second exmaple.
    char * str3 = "What Does Fox Say?";
    char * str4 = "IDOLM@STER";

    //third example.
    char * str5 = "Stack Overflow.";
    char * str6 = "Stack Overflow.";

    int result = CompStr((void *)str1, (void *)str2);

    int result1 = CompStr((void *)str3, (void *)str4);

    int result2 = CompStr((void*)str5, (void*)str6);

    printf("1 : %d\n", result);
    printf("2 : %d\n", result1);
    printf("3 : %d", result2);
    return 0;
}

输出为1 : -1 2: 1 3: 0代码运行良好。

这段代码有问题。

case 4:
{
    printf("How many Strings you want to sort ? ");
    scanf("%d", &max);
    printf("Enter the strings\n");
    for (i = 0; i < max; i++) scanf(" %s", str[i]);
    Sort((void*)str, CompStr, sizeof(char), max);
    printf("Sorted elements are are - \n");
    for (i = 0; i < max; i++) printf(" %s\n", str[i]);
    break;
}

你想要比较字符串。但 sizeof 元素只有 1 个字节。并且您的代码无法编译。

void *mem_copy(void *dest, const void *src, unsigned int n)
{
    int i;
    char* newsrc = (char*)src;
    char* newdest = (char*)dest;
    for (i = 0; i < n; i++) 
    newdest[i] = newsrc[i];
}

我用VS 2017编译,你写的返回类型是void *,但是函数没有返回。是正确的代码吗?请修改代码编译良好。

关于c - 如何在通用排序函数中添加排序顺序选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44092320/

相关文章:

php - 使用php更新mysql不起作用

c - 如何更改 C 函数中传递给函数的指针所指向的内容?

C++ 模板没有匹配的函数调用

c - 当全局变量被重新声明为局部变量时程序的输出

c - 在 C 中显示没有 unsigned char 的 ASCII 字符

c - 将静态结构添加到作为函数参数的数组

javascript - 在 AngularJS 中对一组对象进行分组

c - 多线程并行计数器比简单的基于并发锁的计数器慢

c - 如何在 C 中保留函数指针?

java - 尝试删除字符串数组中的值不起作用 - Java