我对C ++的经验非常有限,我想从我的代码中替换“goto”构造。任何重构的建议
int main()
{
int count;
int countSub = 0;
int userCount = 0;
int roleCount = 0;
int parentGroup;
cout<<"enter a number of parentGroup"<< endl;
cin>> parentGroup;
int subGroup;
cout<<"enter a number sub Group"<< endl;
cin>> subGroup;
int rolePerGroup;
cout<<"enter a number role per Sub Group"<< endl;
cin>> rolePerGroup;
int userPerGroup;
cout<<"enter a number user per Role"<< endl;
cin>> userPerGroup;
do
{
if (parentGroup == 0)
{
cout<<"Error"<<endl;
exit(EXIT_FAILURE);
}
else
{
for(count=1;count <= parentGroup; count ++)
{
do
{
if(subGroup == 0) goto hello;
else
{
for(countSub = 1;countSub<=subGroup; countSub ++)
{
do
{
hello:
if (rolePerGroup == 0)
{
cout<<"Error"<<endl;
exit(EXIT_FAILURE);
}
else
{
for(roleCount = 1; roleCount<=rolePerGroup; roleCount ++)
{
do
{
if(userPerGroup == 0) goto print;
else
{
for(userCount = 1; userCount<=userPerGroup; userCount ++)
{
print:
cout<<"Parent groups are: "<< count <<" | "<<"Sub group are : "<<countSub<<" | "<<"Role per Sub group are : "<< roleCount <<" | "<<"User per role are : "<< userCount <<endl;
}}
userCount --;
}while(userCount < 0);
}}
roleCount --;
}while(roleCount < 0);
}}
countSub --;
}while(countSub < 0);
}}
count --;
}while(count < 0);
}
答案 0 :(得分:0)
您想删除的一个goto:
if(subGroup == 0) goto hello;
else
{
for(countSub = 1;countSub<=subGroup; countSub ++) {
do {
hello:
...
因此,如果我理解正确,您希望如果subGroup
等于0或1,那么您需要进行1次迭代。 goto将输入for-loop
一次。
我建议您使用变量我将调用N
(不一个好名字)并执行:
int N = subGroup == 0 ? 1 : subGroup;
for(countSub = 1;countSub <= N; countSub++) {
我再也看不到了,我不会尝试修复另一个,但我想这是同一个问题。
答案 1 :(得分:0)
如果我错过了什么,请告诉我
#include "stdafx.h"
#include <iostream>
using namespace std;
void Print(int count, int countSub, int rolePerGroup, int userCount, int userPerGroup)
{
for(int roleCount = 1; roleCount<=rolePerGroup; roleCount ++)
{
if(userPerGroup == 0)
{
cout<<"Parent groups are: "<< count <<" | "<<"Sub group are : "<<countSub<<" | "<<"Role per Sub group are : "<< roleCount <<" | "<<"User per role are : "<< userCount <<endl;
continue;
}
for(userCount = 1; userCount<=userPerGroup; userCount ++)
cout<<"Parent groups are: "<< count <<" | "<<"Sub group are : "<<countSub<<" | "<<"Role per Sub group are : "<< roleCount <<" | "<<"User per role are : "<< userCount <<endl;
}
}
int main()
{
int userCount = 0;
int roleCount = 0;
int parentGroup;
cout<<"enter a number of parentGroup"<< endl;
cin>> parentGroup;
if (parentGroup == 0)
{
cout<<"Parent Group should not be zero"<<endl;
exit(EXIT_FAILURE);
}
int subGroup;
cout<<"enter a number sub Group"<< endl;
cin>> subGroup;
int rolePerGroup;
cout<<"enter a number role per Sub Group"<< endl;
cin>> rolePerGroup;
if (rolePerGroup == 0)
{
cout<<"Role per Group should not be zero"<<endl;
exit(EXIT_FAILURE);
}
int userPerGroup;
cout<<"enter a number user per Role"<< endl;
cin>> userPerGroup;
for(int count=1;count <= parentGroup; count ++)
{
if(subGroup == 0)
{
Print( count, 0, rolePerGroup, userCount, userPerGroup);
continue;
}
for(int countSub = 1;countSub<=subGroup; countSub ++)
{
Print( count, countSub, rolePerGroup, userCount, userPerGroup);
}
}
}