排序时无法执行交换操作.我做的时候它会崩溃.为什么

I can not do a swap operation while sorting. It crashes when I do it. Why?

本文关键字:候它会 崩溃 为什么 操作 执行 交换 排序      更新时间:2023-10-16
struct Dates
{
int day;
int month;
int year;
}accountinfo[3];
struct Accounts
{
string name,lastname;
int number;
float balance;
}account[3];
void sortduetoaccnumbers()
{
for (i=0;i<3;i++)
{
for (j=0;j<3;j++)
{
if (account[j].number>account[j+1].number)
{
//swap
}
}
}
}
void sortduetodates()
{
for (i=0;i<3;i++)
{
for (j=0;j<3;j++)
{
if (accountinfo[j].year>accountinfo[j+1].year)
{
//swap
}
else if (accountinfo[j].year==accountinfo[j+1].year)
{
if (accountinfo[j].month>accountinfo[j+1].month)
{
//swap
}
else if (accountinfo[j].month==accountinfo[j+1].month)
{
if (accountinfo[j].day>accountinfo[j+1].day)
{
//swap
}
}
}
}
}
}

我无法使用排序算法对这些帐户进行排序。如果我进入它们,它就会崩溃。cmd突然停止并完成程序。

我输入了一个评论行,其中必须交换函数。这样你就可以分析代码了。

除此之外,其他所有功能都在工作。我陷入了困境。

此代码错误:if (accountinfo[j].year>accountinfo[j+1].year)因为如果j==2,那么j+1=3-数组大小上的索引=>未定义的行为(在您的情况下崩溃(

您需要将循环条件更改为j<2或重写您的支票

相关文章: