在c中按字母顺序升序和降序对用户输入名称数组进行排序

时间:2014-04-10 21:13:48

标签: c arrays sorting

我是C的新手并且有一个班级的作业。我需要//构建一个程序,使用一维数组来存储用户输入的10个名字。 //输入名称后,用户应看到一个菜单,其中包含两个选项,可按升序或降序对10个名称进行排序和打印。

到目前为止我在这里

`

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

int main()
{
    char names[10][10];
    char temp[10],i,j;
    int count;
    int sort;
    count=1;
    for ((i=0);i<10;i++)
    {
        while (count<11)
        {
            printf("Please enter name %i\n",count);
            scanf("%s",names);
            count=count+1;
        }
    }
    printf("Would you like to print in Ascending (press 1) or Descending (press 2) order?\n\n");
    scanf("%d",&sort);
    switch (sort) {
        case 1:
            printf("\n Ascending order:\n ");


       printf("%s\n",names);
        qsort(temp, 10, 10, names);
        break;
    default:
    case 2:
        printf("\n Descending order:\n ");
        printf("%s\n",names);
        break;
}
        return 0;

我需要帮助弄清楚如何进行排序。

3 个答案:

答案 0 :(得分:0)

试试这段代码。我使用了泡泡sort,因为它很容易实现。您可以查看compare函数如何与注释代码一起使用。

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

int asc = -1; // -1 for desc, 1 for asc

int compare(char *c1, char *c2){
    int length1 = strlen(c1);
    int length2 = strlen(c2);
    int min = length1 < length2 ? length1 : length2;
    int i;
    for(i=0;i<min;i++){
        if(c1[i]<c2[i]) return 1*asc;
        if(c1[i]>c2[i]) return -1*asc;
    }
    return 0;
}

main()
{
    // char *c1 = "abcd";
    // char *c2 = "abce";
    // if(compare(c1,c2) == 1){
    //     printf("%s is more than %s",c1,c2);
    // }
    // if(compare(c1,c2) == -1){
    //     printf("%s is less than %s",c1,c2);
    // }
    // if(compare(c1,c2) == 0){
    //     printf("%s is equal to %s",c1,c2);
    // }
    int c,d,n = 10;

    char** array = (char**)malloc(sizeof(char*)*n);
    array[0]="asd";
    array[1]="qwdas";
    array[2]="qwdas";
    array[3]="asdcv";
    array[4]="asfg";
    array[5]="m";
    array[6]="kgig";
    array[7]="gitt";
    array[8]="vnvfjf";
    array[9]="ogh";

    char* swap;
    for (c = 0 ; c < ( n - 1 ); c++)
  {
    for (d = 0 ; d < n - c - 1; d++)
    {
      if (compare(array[d],array[d+1]) == 1)
      {
        swap       = array[d];
        array[d]   = array[d+1];
        array[d+1] = swap;
      }
    }
  }
  printf("\n");
  for(c=0;c<n;c++){
      printf("%s ",array[c]);
  }
}

答案 1 :(得分:0)

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

int invstrcmp(char *a, char *b){
    return strcmp(b, a);
}

int main(){
    char names[10][10];
    int i, sort=2;
    int (*cmp)(const void*, const void*);

    for (i=0;i<10;i++){
        printf("Please enter name %i\n", i+1);
        scanf("%9s",names[i]);
    }
    printf("Would you like to print in Ascending (press 1) or Descending (press 2) order?\n\n");
    scanf("%d", &sort);
    switch (sort) {
    case 1:
        printf("\n Ascending order:\n");
        cmp = (int (*)(const void*, const void*))strcmp;
        break;
    case 2:
    default:
        printf("\n Descending order:\n");
        cmp = (int (*)(const void*, const void*))invstrcmp;
        break;
    }

    qsort(names, 10, 10, cmp);
    for(i=0;i<10;++i)
        printf("%s\n", names[i]);

    return 0;
}

答案 2 :(得分:0)

首先有一些错误:

1,你不能在一个单维数组中存储10个人名,你需要2D数组,只需char names[10][10]

2,当你使用函数qsort时,第四个参数是一个函数指针int (*cmp)(const void*, const void*)

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

int cmp1(const void* l, const void* r)
{
    return strcmp((char*)l , (char*) r);
}
int cmp2(const void* l, const void* r)
{
    return -strcmp((char*)l , (char*) r);
}
int main()
{
    char names[10][10] = {0};
    char temp[10];
    int count;
    int sort;
    count=1;
    for (int i=0;i<10;i++)
    {
        printf("Please enter name %i\n",i+1);
        scanf("%s",names[i]);
    }
    printf("Would you like to print in Ascending (press 1) or Descending (press 2) order?\n\n");
    scanf("%d",&sort);
    switch (sort) 
    {
    case 1:
        printf("\n Ascending order:\n ");
        qsort(names, 10, 10, cmp1);
        break;
    case 2:
        printf("\n Descending order:\n ");
        qsort(names, 10, 10, cmp2);
        break;
    }

    for (int i = 0; i < 10; i ++)
    {
        printf("%s\n", names[i]);
    }
    return 0;
}