【问题标题】:How to put option for Sorting order in generic sort function [closed]如何在通用排序功能中放置排序顺序选项[关闭]
【发布时间】:2017-05-21 01:25:26
【问题描述】:

这是在通用排序函数中使用的比较函数。 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);

【问题讨论】:

    标签: c arrays function sorting generics


    【解决方案1】:
    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 个字节。你re code cant 编译。

    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 *,但函数是dosen`t return。是正确的代码吗?请修改代码编译好。

    【讨论】:

    • 它不起作用。这是输入hey how are you surjt,这是输出ehy how are you surjit
    • 哪里不工作?
    • 我认为 compStr 没有问题。你能写出你的排序算法吗?
    • 我添加了测试代码。请看。
    • 当我测试你的 CompStr 函数时,我得到了 this error
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-12
    • 1970-01-01
    • 1970-01-01
    • 2010-12-28
    • 1970-01-01
    相关资源
    最近更新 更多