gpt4 book ai didi

c - 对命令行参数进行排序

转载 作者:行者123 更新时间:2023-11-30 21:20:20 25 4
gpt4 key购买 nike

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 排序 command_sort.c

到目前为止我所拥有的是:

#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;
}

最佳答案

眼前的问题是你如何解读你的论点。

 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 不再是硬编码的,您必须处理这样的问题:如果参数多于您分配的内存怎么办?您可以拒绝它们,因为时间太长了。或者您可以增加数组的大小。我留给你阅读 realloc以及 C 中的动态内存分配。

<小时/>

两个样式注释。首先,虽然您可以编写不带大括号的循环和 if,但始终使用大括号。为什么?因为最终你会写这个。

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 。它会让你看到那些导致奇怪行为的隐藏内存问题。这就是我这么快发现你的问题的原因。

关于c - 对命令行参数进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39946709/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com