- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
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
到目前为止我所拥有的是:
#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]);
}
在本例中,我选择将 i
与 argv
和 argc
对齐,因为还有更多需要协调的内容。我将偏移量放入一个变量中,以解释它为何存在,并避免更改一个变量而忽略另一个变量。
假设有 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/
我正在尝试对每个条目有多个值的关联数组进行排序。 例如 [0] => stdClass Object ( [type] => node [sid] => 158 [score] => 0.059600
我在 mysql 中有“日期”列以这种格式保存日期 2014 年 9 月 17 日(日-月-年) 我需要对它们进行升序排序,所以我使用了这个命令: SELECT * FROM table ORDER
我目前正在将 MySQL 存储过程重写为 MS SQL 存储过程,但遇到了问题。 在 MySQL 存储过程中,有一个游标,它根据最近的日期 (effdate) 选择一个值并将其放入变量 (thestt
我想要 gwt r.QuestionId- 排序。但是我得到未排序的 QuestionId 尽管我提到了 QuestionId ASC 的顺序。 SELECT r.QuestionId,
我有一个关于在 scandir 函数中排序的基本问题。到目前为止,我阅读了 POSIX readdir 的手册页,但没有找到有关订购保证的具体信息。 但是当我遍历大目录(无法更改,只读)时,我在多个系
基本上我必须从 SQL 数据库中构建项目列表,但是用户可以选择对 7 个过滤器的任意组合进行过滤,也可以选择要排序的列以及按方向排序。 正如您可以想象的那样,这会以大量不同的组合进行编码,并且数据集非
我有两张 table 。想象第一个是一个目录,包含很多文件(第二个表)。 第二个表(文件)包含修改日期。 现在,我想选择所有目录并按修改日期 ASC 对它们进行排序(因此,最新的修改最上面)。我不想显
我想先根据用户的状态然后根据用户名来排序我的 sql 请求。该状态由 user_type 列设置: 1=活跃,2=不活跃,3=创始人。 我会使用此请求来执行此操作,但它不起作用,因为我想在“活跃”成员
在 C++ 中,我必须实现一个“类似 Excel/Access”(引用)的查询生成器,以允许对数据集进行自定义排序。如果您在 Excel 中使用查询构建器或 SQL 中的“ORDER BY a, b,
我面临这样的挑战: 检索按字段 A 排序的文档 如果字段 B 存在/不为空 . 否则 按字段排序 C. 在 SQL 世界中,我会做两个查询并创建一个 UNION SELECT,但我不知道如何从 Mon
我想对源列表执行以下操作: map 列表 排序 折叠 排序 展开 列表 其中一些方法(例如map和toList)是可链接的,因为它们返回非空对象。但是,sort 方法返回 void,因为它对 List
我制作了一个用于分析 Windows 日志消息编号的脚本。 uniq -c 数字的输出很难预测,因为根据数字的大小会有不同的空白。此时,我手动删除了空白。 这是对消息进行排序和计数的命令: cat n
我有以下词典: mydict1 = {1: 11, 2: 4, 5: 1, 6: 1} mydict2 = {1: 1, 5: 1} 对于它们中的每一个,我想首先按值(降序)排序,然后按键(升序)排序
我刚刚开始使用泛型,目前在对多个字段进行排序时遇到问题。 案例: 我有一个 PeopleList 作为 TObjectList我希望能够通过一次选择一个排序字段,但尽可能保留以前的排序来制作类似 Ex
有没有办法在 sql 中组合 ORDER BY 和 IS NULL 以便我可以在列不为空时按列排序,但如果它为null,按另一列排序? 最佳答案 类似于: ORDER BY CASE WHEN
我有一个包含 2 列“id”和“name”的表。 id 是常规的自动增量索引,name 只是 varchar。 id name 1 john 2 mary 3 pop 4 mary 5 j
场景 网站页面有一个带有分页、过滤、排序功能的表格 View 。 表中的数据是从REST API服务器获取的,数据包含数百万条记录。 数据库 REST API 服务器 Web 服务器 浏览器 问
假设我有一本字典,其中的键(单词)和值(分数)如下: GOD 8 DONG 16 DOG 8 XI 21 我想创建一个字典键(单词)的 NSArray,首先按分数排序,然后按字
如何在 sphinx 上通过 sql 命令选择前 20 行按标题 WEIGHT 排序,接下来 20 行按标题 ASC 排序(总共 40 个结果),但不要给出重复的标题输出。 我尝试了这个 sql 命令
我有一个奇怪的问题,当从 SQLite 数据库中选择信息并根据日期排序时,返回的结果无效。 我的SQL语句是这样的: Select pk from usersDates order by dateti
我是一名优秀的程序员,十分优秀!