【发布时间】:2017-05-20 14:37:06
【问题描述】:
所以基本上我想对文件中的数字进行排序。所以我先读了
for (int i = 0; i < count; i++) {
fscanf(read_file, "%d %s %d %s\n", &custid_temp[i].id, custid_temp[i].name, &custid_temp[i].reward_point, custid_temp[i].promo);
printf("%d %s %d %s\n", custid_temp[i].id, custid_temp[i].name, custid_temp[i].reward_point, custid_temp[i].promo);
}
文件由以下内容组成:
1 Test 200 WELCOME2017
2 James 700 WELCOME2017
看是文件中的点,200和700,我想按升序(从大到小)排序变成这样:
2 James 700 WELCOME2017
1 Test 200 WELCOME2017
所以我将使用这个函数对文件进行排序:
for (int i = 0; i<count+1; i++)
{
for (int j = 0; j<count - i; j++)
{
if (custid[j].reward_point <custid[j + 1].reward_point)
{
struct customer temp_sort = custid[j];
custid[j] = custid[j + 1];
custid[j + 1] = temp_sort;
}
}
}
最后打印更新的结构:
for (int i = 0; i < count; i++) {
printf("%d %s %d %s\n", custid[i].id, custid[i].name, custid[i].reward_point, custid[i].promo);
}
结果是这样的(VS2017):
1 Test 200 WELCOME2017
2 James 700 WELCOME2017
0 0
0 0
结果是这个(代码块):
1 Test 200 WELCOME2017
2 James 700 WELCOME2017
2 James 700 WELCOME2017
1 Test 200 WELCOME2017
Process returned 2 (0x2) execution time : 0.015 s
Press any key to continue.
即使使用相同的代码,为什么两个编译器会产生 2 个不同的结果,是否有任何解决方案可以在 VS2017 上运行?
无论如何,感谢您的时间。
【问题讨论】:
-
调试器是你的朋友。
-
custid[j + 1]出现越界。 -
Visual Studio 和 CodeBlock 是 IDE,而不是编译器。 Visual Studio 使用 Microsoft 的
cl.exe或 Clang 的修改版本。 CodeBlocks 可以支持广泛的编译器 AFAIK。 -
@BLUEPIXY ,但如果我将
custid[j + 1]更改为custid[j]或custid[j - 1],它会产生这个。1 Test 200 WELCOME2017 2 James 700 WELCOME2017 1 Test 200 WELCOME2017 2 James 700 WELCOME2017 Process returned 2 (0x2) execution time : 0.010 s Press any key to continue.这意味着它不再排序了 -
你使用
custid_temp作为阅读。但是您使用custid作为排序。