-8

我在 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);
}
4

2 回答 2

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++) {

我再也看不到了,我不会尝试修复另一个,但我想这是同一个问题。

于 2013-07-17T16:37:13.497 回答
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);
    }
}
}
于 2013-07-17T18:57:24.363 回答