gpt4 book ai didi

c++ - 我怎样才能使这种模式根据需要重复多次而不使其过度嵌套?

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

我有这个功能是为了显示排名系统的层次结构。我希望层次结构能够根据需要嵌套多次。但是嵌套所有这些“for/if”语句似乎是不必要的。有没有人对编写像这样重复自己的函数有一些提示?

void hierarchy(int x)
{
cout << "HIERARCHY OF COMMAND: " << member[x].name << ", " << member[x].rnk << " [# " << member[x].ID << "]\n";
for (int a = 0; a < member_count; a++)
{
if (member[x].name == member[a].CO)
{
cout << "|---" << member[a].name << ", " << member[a].rnk << " [# "
<< member[a].ID << "]\n";
for (int b = 0; b < member_count; b++)
{
if (member[a].name == member[b].CO)
{
cout << "| |---" << member[b].name << ", " << member[b].rnk << " [# "
<< member[b].ID << "]\n";
for (int c = 0; c < member_count; c++)
{
if (member[b].name == member[c].CO)
{
cout << "| | |---" << member[c].name << ", " << member[c].rnk << " [# "
<< member[c].ID << "]\n";
for (int d = 0; d < member_count; d++)
{
if (member[c].name == member[d].CO)
{
cout << "| | | |---" << member[d].name << ", " << member[d].rnk << " [# "
<< member[d].ID << "]\n";
for (int e = 0; e < member_count; e++)
{
if (member[d].name == member[e].CO)
{
cout << "| | | | |---" << member[e].name << ", " << member[e].rnk << " [# "
<< member[e].ID << "]\n";
}
}
}
}
}
}
}
}
}
}
}

最佳答案

用户 recursionstack解决此类问题。尝试实现它。如果您遇到问题,请发帖。

关于c++ - 我怎样才能使这种模式根据需要重复多次而不使其过度嵌套?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22989628/

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