简体   繁体   English

如何在复杂的算法中处理 goto function?

[英]How can I deal goto function in complicated algorithm?

I remove 4 goto function in my project but I cant deal with that.我在我的项目中删除了 4 goto function 但我无法处理。 I hope you can help me I marked him on code.我希望你能帮助我我在代码上标记了他。 I try bool-break method but doesnt work, also I try the if but I cant handle myself I generated number randomly.我尝试了 bool-break 方法但不起作用,我也尝试了 if 但我无法处理自己随机生成的数字。 Sometimes give true score but mostly it's stuck in some place I found where it is stuck but no solution on my way.有时给出真实分数,但大多数情况下它卡在我发现卡住的地方,但在我的路上没有解决方案。 Other removes work very well but that one not work.其他删除工作非常好,但一个不起作用。

void Solve()
{   
    if (check == 0)
        flag1 = 0;
    else if (flag1 != 0)
    {
        check3 = check;
        while (1)
        {           
            check2 = 0;
            while (1)
            {
                while (check2 < check)
                {
                    {
                        x = check4[check2];
                        z = rowInc[x];
                        for (y = 0; y <= length-1; y++)
                        {
                            if (count1[y])
                            {
                                int delMatrix;
                                delMatrix = mainMatrix[x][y] - z + colEq[y];
                                if (delMatrix < count1[y])
                                {
                                    if (delMatrix == 0)
                                    {
                                        if (rowEq[y] < 0)
                                        {
                                            goto breakthru;  //This section I want to remove
                                            //searching();
                                            //break2 = true;
                                            //break;
                                            //flag4 = 0; break;
                                        }

                                        count1[y] = 0;
                                        rowEq2[y] = x;

                                        check4[check++] = rowEq[y];
                                    }
                                    else
                                    {
                                        count1[y] = delMatrix;
                                        count1Row[y] = x;
                                    }
                                }
                            }
                        }
                    }
                    check2++;
                    flag4 = 1;
                }   
                if (1)
                {
                    z = lengthH;
                    for (y = 0; y < length; y++) 
                    {
                        if (count1[y] && count1[y] < z)
                            z = count1[y];
                    }
                    for (check2 = 0; check2 < check; check2++)
                    {
                        rowInc[check4[check2]] += z;
                    }
                    for (y = 0; y < length; y++)
                    {
                        if (count1[y])
                        {
                            count1[y] -= z;
                            if (count1[y] == 0)
                            {
                                x = count1Row[y];
                                if (rowEq[y] < 0)
                                {
                                    for (j = y + 1; j < length; j++)
                                        if (count1[j] == 0)
                                        {
                                            colEq[j] += z;
                                        }
                                    break1 = true;
                                    break;
                                }
                                else
                                {
                                    rowEq2[y] = x;
                                    check4[check++] = rowEq[y];
                                }
                            }
                        }
                        else
                            colEq[y] += z;
                    }
                    if (break1)break;
                }
            }
            breakthru:

One way is to create sub-function and use return:一种方法是创建子函数并使用 return:

void bar() {
    while (1)
    {
        while (check2 < check)
        {
            {
                x = check4[check2];
                z = rowInc[x];
                for (y = 0; y <= length-1; y++)
                {
                    if (count1[y])
                    {
                        int delMatrix;
                        delMatrix = mainMatrix[x][y] - z + colEq[y];
                        if (delMatrix < count1[y])
                        {
                            if (delMatrix == 0)
                            {
                                if (rowEq[y] < 0)
                                {
                                    return; // Return here.
                                }
                                count1[y] = 0;
                                rowEq2[y] = x;

                                check4[check++] = rowEq[y];
                            }
                            else
                            {
                                count1[y] = delMatrix;
                                count1Row[y] = x;
                            }
                        }
                    }
                }
            }
            check2++;
            flag4 = 1;
        }   
        {
            z = lengthH;
            for (y = 0; y < length; y++) 
            {
                if (count1[y] && count1[y] < z)
                    z = count1[y];
            }
            for (check2 = 0; check2 < check; check2++)
            {
                rowInc[check4[check2]] += z;
            }
            for (y = 0; y < length; y++)
            {
                if (count1[y])
                {
                    count1[y] -= z;
                    if (count1[y] == 0)
                    {
                        x = count1Row[y];
                        if (rowEq[y] < 0)
                        {
                            for (j = y + 1; j < length; j++)
                                if (count1[j] == 0)
                                {
                                    colEq[j] += z;
                                }
                            break1 = true;
                            break;
                        }
                        else
                        {
                            rowEq2[y] = x;
                            check4[check++] = rowEq[y];
                        }
                    }
                }
                else
                    colEq[y] += z;
            }
            if (break1)break;
        }
    }
}

and then接着

void Solve()
{   
    if (check == 0)
        flag1 = 0;
    else if (flag1 != 0)
    {
        check3 = check;
        while (1)
        {           
            check2 = 0;
            bar();

            // breakthru:
            // ...
        }
        // ...
    }
    // ...
}

There isn't any code after breakthru: , so I assume you want the function to stop immediatly when reaching goto breakthru; breakthru:之后没有任何代码,所以我假设您希望 function 在到达goto breakthru; . . In that case, replace goto breakthru;在这种情况下,替换goto breakthru; with return;return; and remove breakthru: .并删除breakthru:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM