- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
当无符号长整型是结构的一部分时,C 的 Qsort 算法不会对它们进行排序,这应该不重要。代码如下:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
typedef struct TextData {
char catalog, // The catalog code.
txt[137]; // The text for this object.
short mv; // mv * 100.
unsigned long long indx; // Hash of 2D position.
} txtD; // 152 bytes.
// The comparison function required by qsort for txtD items.
int cmpTdata(const void *a,
const void *b) {
txtD *dataA = (txtD*) a;
txtD *dataB = (txtD*) b;
if ((dataB->indx - dataA->indx) < 0) { return 1; }
else if ((dataB->indx - dataA->indx) > 0) { return -1; }
else { return 0; }
}
int main(int argc, char** argv) {
FILE *UNSORTED = fopen("/top/middle/directory/filename", "r");
if (UNSORTED == NULL) {
printf("Filename not opened for reading!\n");
exit(0);
}
int stat = fseek(UNSORTED, 0L, SEEK_END);
if (stat != 0) {
printf("Fseek on failed to find the end! because %s.\n", strerror(errno));
exit(0);
}
unsigned long long size = ftell(UNSORTED);
if ((size == 0) && (errno != 0)) {
printf("Error %d) Ftell is 0! because %s.\n",
errno, strerror(errno));
exit(0);
}
fclose(UNSORTED);
int nrItems = size / sizeof(txtD);
txtD *data = (txtD*) malloc(nrItems * sizeof(txtD));
if (data == NULL) {
printf("txtD structure not allocated because:\n%s.\n",
strerror(errno));
exit(0);
}
// Sort it in order of increasing indx.
qsort(data, nrItems, sizeof(txtD), cmpTdata);
// Save the sorted items using fopen and fwrite. I'll spare you the details.
}
这是一个简单读取结构并记录错误的程序的输出:
Item 2) previous index 466715 greater than t.indx 449261!
Item 4) previous index 464265 greater than t.indx 404184!
Item 5) previous index 404184 greater than t.indx 353788!
Item 7) previous index 446334 greater than t.indx 361489!
Item 8) previous index 361489 greater than t.indx 328323!
Item 10) previous index 487465 greater than t.indx 343185!
Item 12) previous index 494247 greater than t.indx 428224!
Item 14) previous index 478868 greater than t.indx 130860!
Item 16) previous index 444180 greater than t.indx 339954!
Item 18) previous index 342195 greater than t.indx 281552!
Item 20) previous index 394250 greater than t.indx 370791!
Item 22) previous index 458202 greater than t.indx 311406!
Item 23) previous index 311406 greater than t.indx 280793!
Item 25) previous index 466171 greater than t.indx 424598!
Item 27) previous index 467144 greater than t.indx 431265!
Item 29) previous index 472449 greater than t.indx 198109!
Item 31) previous index 469376 greater than t.indx 451215!
Item 33) previous index 453004 greater than t.indx 427448!
Item 34) previous index 427448 greater than t.indx 374260!
Item 37) previous index 447735 greater than t.indx 336330!
检查了41项,有21个错误。
顺便说一句,责备我没有使用 stderr 或其他风格上的违规行为是没有帮助的。为什么 qsort 不排序是。另请注意,当我对 double 进行排序时,函数 cmpTdata 对于其他结构也可以正常工作。
TIA!
最佳答案
function cmpTdata works fine for other structures when I'm sorting doubles.
double
数学与无符号 数学不同。以下内容永远不会成立,因为无符号永远不会小于 0。
if ((dataB->indx - dataA->indx) < 0) { return 1; }
这意味着代码不是用会发出警告的良好编译器编译的。所以最大的问题不是“算错了”,而是没有使用手边的工具。最好启用所有警告。
foo.c:9:35: warning: comparison of unsigned expression < 0 is always false [-Wtype-limits]
<小时/>
推荐 C 比较习惯用法 - 被各种编译器识别以生成高效的代码。
int cmpTdata(const void *a, const void *b) {
const txtD *dataA = (txtD*) a;
const txtD *dataB = (txtD*) b;
return (dataA->indx > dataB->indx) - (dataA->indx < dataB->indx);
}
关于C: qsort 对结构体的 unsigned long long 成员进行排序时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59512346/
我找到了long int long和 int long long可以编译为变量类型。 long int long有什么区别吗, int long long , long long和 long long
我无法找出为什么“加密”函数仍然将“消息”读取为字符串,尽管我已经使用不同的方法将数据类型更改为字节。 错误消息是“Prince 类型中的方法 Encrypt(long, long, long, lo
这个问题在这里已经有了答案: Is "long long" = "long long int" = "long int long" = "int long long"? (4 个答案) 关闭 6 年
我正在从 Java 过渡到 C++,并且对 long 数据类型有一些疑问。在 Java 中,要保存大于 232 的整数,您只需编写 long x;。但是,在 C++ 中,long 似乎既是数据类型又是
clang-tidy 12.0.1 报告了一个相当奇怪的警告。在以下代码中: #include int main() { std::vector v1; const auto a =
我创建了一个 pair 和 long long int 的映射 - map,long long int >; 和一个交互器 - map, long long int >::iterator it1;
我想知道 unsigned long long 和 unsigned long long int 的主要区别。它们可以互换使用吗? 对于像 9223372036854775807 这样的大十进制数的计
我看到的大多数代码都使用缩写类型来声明变量,例如 long long x; // long long int x short y; // short int y 我浏览了 C++11 标准(第 3.9
common_type::type是 unsigned long因为关于积分提升后的操作数,标准说... [...] if the operand that has unsigned integer
long long int A = 3289168178315264; long long int B = 1470960727228416; double D = sqrt(5); long lon
这些新数据类型的目的是什么?我通常只使用“int”或“long”,但为什么会存在这些呢?它们带来了什么新功能或用途? 最佳答案 long int一直是long的全称,只是很少用而已。 long lon
我正在运行以下for循环 for(unsigned long long int i = N-1; i >= 0; i--){ cin>>L[i]; } 当程序到达这个代码段时,它停止响应。但是
最近问了一个关于递归导致这个问题的问题 注意-> count() 函数返回键 K 在 map 容器中出现的次数。如果键存在于容器中,则返回 1,因为映射仅包含唯一键。如果 map 容器中不存在键,则返
好的,所以我正在尝试实现客户端 - 服务器程序(套接字编程)。 我的客户发送一个嵌入字符串中的 long long int,如下所示: char copy[10]; sprintf(send_data
如果我有任务 Long c = a + b; 有没有一种简单的方法来检查 a + b 不大于/小于 Long.MAX_VALUE/Long.MIN_VALUE? 最佳答案 使用 Guava , 就这么
我需要制作一个 Comparator 来根据它的 long 类型的变量之一对我的对象列表进行排序。 public class ParticipantIndexComparator implements
假设我有这两种类型: typedef unsigned long long uint64; typedef signed long long sint64; 我有这些变量: uint64 a = ..
long long 和 long 有什么区别?而且它们都不适用于 12 位数字 (600851475143),我是不是忘记了什么? #include using namespace std; int
当结果将大于 C 中的 long long int 时,是否有可能对两个不同的 long long int 变量求和? 最佳答案 由于 OP 想要“在屏幕上打印结果”,因此将数字分成两部分:Most-
实际上我必须找到从源顶点到所有其他顶点的最短路径。为此,我获得了下面给出的代码模板。我想实现“Bellman–Ford algorithm”。 #include #include #include
我是一名优秀的程序员,十分优秀!