对命令行参数进行排序

时间:2016-10-09 17:54:44

标签: c arrays sorting

C新手,我的工作任务有问题。我使用的是C89编译器。

编写一个程序,对10个数字的命令行参数进行排序,这些数字被假定为整数。第一个命令行参数指示排序是降序(-d)还是升序(-a),如果用户输入无效选项,程序应显示错误消息。

程序的示例运行:

./sort –a 5 2 92 424 53 42 8 12 23 41
2 5 8 12 23 41 42 53 92 424

./sort –d 5 2 92 424 53 42 8 12 23 41
424 92 53 42 41 23 12 8 5 2
  1. 使用为项目5提供的selection_sort函数。创建另一个类似但按降序排序的函数。
  2. 使用字符串库函数处理第一个命令行参数。
  3. 使用atoi函数将字符串转换为整数形式。
  4. 编译程序以生成可执行文件作为排序: gcc -Wall -o sort command_sort.c
  5. 到目前为止我所拥有的是:

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    //function prototype
    void swap(int *arr, int i, int j);
    
    //main function
    int main(int argc, char *argv[])
    {
         //declare the required variables
         int array[10];
         int maxIndex = 0, minIndex =0;
         int i = 0, j = 0, n = 2;
    
         //check whether the number of arguments are less than 2 or not
         if (argc < 2)
         {
              //print the error message
              printf("Data is insuffient! Please insert proper input at command line. \n");
         }
         else
         {   
              // read the integers from the command line
              for (i = 0; i < 10; i++)
                  array[i] = atoi(argv[2 + i]);
         }
    
         printf("Selection sort on integer arrays \n\n");
    
         //print the elements that are read from the command line
         printf("Elements before sorting are: \n");
    
         //print the sorted elements
         for(i =0;i<10;i++)
              printf("%d ", array[i]);
    
         printf("\n");
    
         //check whether the first argument is -a, if
         //-a sort the elements in the array in asscending order
         if (strcmp(argv[1], "-a") == 0)
         {
              //logic to sort the elements in asscending order using selection sort
              for (i = 0; i < 10; ++i)
              {        
                  minIndex = i;
                  for (int j = i + 1; j < 10; ++j)
                  {
                       if (array[j] < array[minIndex])
                            minIndex = j;
                  }
                  swap(array, minIndex, i);
              }
         }
    
         //check whether the first argument is -d, if
         //-d sort the elements in the array in descending order
         if (strcmp(argv[1], "-d") == 0)
         {   
              //logic to sort the elements in descending order using selection sort
              for (i = 0; i < 10; ++i)
              {
                  maxIndex = i;
                  for (j = i + 1; j < 10; ++j)
                  {
                       if (array[j] > array[maxIndex])
                            maxIndex = j;
                  }
                  swap(array, maxIndex, i);
              }
         }
    
         //print the elements
         printf("\nElements after sorting are: \n");
    
         //print the sorted elements
         for(i =0;i<10;i++)
              printf("%d ", array[i]);
    
         printf("\n\n");
    
         return 0;
    }
    
    //definition of swap function
    void swap(int *arr, int i, int j)
    {
         int temp = arr[i];
         arr[i] = arr[j];
         arr[j] = temp;
    }
    

1 个答案:

答案 0 :(得分:2)

当前的问题是你如何阅读你的论点。

 if (argc < 2)
 {
      //print the error message
      printf("Data is insuffient! Please insert proper input at command line. \n");
 }

如果没有给出参数,这将打印消息,但随后很高兴继续使用该程序和未初始化的数组。它需要退出。

 if (argc < 2)
 {
      fprintf(stderr, "Please enter some numbers to be sorted.\n");
      exit(1);
 }

这也意味着其余代码不必包含在巨大的else子句中。这被称为"early exit"或“早期返回”,它使代码更简单。

接下来是你如何阅读这些论点。

// read the integers from the command line
for (i = 0; i < 10; i++)
    array[i] = atoi(argv[2 + i]);

该循环假设有10个参数。如果有更少,它会读取乱码。如果还有更多,它将无法读取它们。相反,请使用argc

/* Skip the name of the program and the first option */
int argv_offset = 2;
int num_numbers = argc - argv_offset;
for( i = argv_offset; i < argc; i++ ) {
    array[i-argv_offset] = atoi(argv[i]);
}

在这种情况下,我选择将iargvargc对齐,因为还有更多要协调的内容。我把我的偏移量放到一个变量中来解释它为什么存在并避免用另一个变换它。

假设有10个数字这个问题是通过其余代码的问题。这就是为什么我设置num_numbers来跟踪它。所有硬编码的10都可以用它代替。

现在10不再是硬编码的,你必须处理如果有更多参数而不是你为内存分配的问题?你可能只是拒绝它们太久了。或者你可以增加阵列的大小。我让您阅读C中的realloc和动态内存分配。

两种风格的音符。首先,虽然您可以在没有大括号的情况下编写循环和ifs,但 始终使用大括号 。为什么?因为最终你会写这个。

for( blah blah blah )
    do this thing
    and do this other thing

你会盯着它好几个小时,想知道为什么它不起作用。

接下来,//无效C89。您必须使用/* ... */。大多数C编译器都会允许它,但是如果你将-std=c89添加到编译器中,你会收到警告。

cc -std=c89 -Wall -g test.c -o test
test.c:5:1: warning: // comments are not allowed in this language [-Wcomment]
//function prototype
^

没关系,C99允许//,大多数编译器现在都支持C99的重要部分。

最后,C很高兴让你走出一个阵列直到它崩溃。 使用内存检查器 ,例如valgrind。它会让你看到那些导致奇怪行为的隐藏内存问题。这就是我如此快速地发现你的问题的方法。

相关问题