如何在迭代后重复从主函数重复执行

How to Repeat Execution from Main function after an Iteration

本文关键字:函数 复执 执行 迭代      更新时间:2023-10-16

所以我有一个程序,可以简化布尔表达式。我要实现的是,在简化第一个表达式的结束时,我希望用户选择他是否要简化另一个表达式或仅退出控制台应用程序(程序)。

这是主函数的代码

int main(int argc, char *argv[]) {
/* allow command line calling with arguments -m -b X
where X is a number. order or -m and -b X does not
matter*/
cout << "Designed By a Student For the Students :)n";
char choice;
do
{
    cout << "nEnter the number of variables to be Minimizedn";
    cin >> m;
    if (argc >= 2)
    {
        string arg = argv[1];
        if (arg.find("-m") != -1) {
            show_mid = true;
            if (argc >= 3) {
                arg = argv[2];
                if (arg.find("-b") != -1)
                    MIN_BIT = atoi(argv[3]);
            }
        }
        else if (arg.find("-h") != -1) {
            cout << "-b Xtminimum bits should be X.n"
                << "-m  tshow mid process computation.n"
                << "-h  tshow this.n";
                                     return 0;

        }
        else {
            if (arg.find("-b") != -1 && argc >= 3)
                MIN_BIT = atoi(argv[2]);
            if (argc >= 4) {
                arg = argv[3];
                if (arg.find("-m") != -1)
                    show_mid = true;
            }
            else
            {
                cout << "Invalid argumentn"
                    << "-b Xtminimum bits should be X.n"
                    << "-m  tshow mid process computation.n"
                    << "-h  tshow this.n";
                                            return 0;
            }
        }
    }
    getinput();
    init();
    cout << "Press 'y' to Reduce Another Expression or 'n' to Close this Application";
    cin >> choice;
} while (choice == 'y');
WINPAUSE;
return 0;

}

如上所以看到,我在循环中使用了do do n do to to to the,我在这里遇到了两个问题,即程序终止而无需接受用户输入,如果我使用winpause,当我按任何键时,程序将退出。是递归的答案,请建议解决方法。

注意:我使用VS2017 IDE ..:)

编辑:新代码

int main(int argc, char *argv[]) {
/* allow command line calling with arguments -m -b X
where X is a number. order or -m and -b X does not
matter*/
cout << "Designed By a Student For the Students :)n";
char choice;
    if (argc >= 2)
    {
        string arg = argv[1];
        if (arg.find("-m") != -1) {
            show_mid = true;
            if (argc >= 3) {
                arg = argv[2];
                if (arg.find("-b") != -1)
                    MIN_BIT = atoi(argv[3]);
            }
        }
        else if (arg.find("-h") != -1) {
            cout << "-b Xtminimum bits should be X.n"
                << "-m  tshow mid process computation.n"
                << "-h  tshow this.n";

        }
        else {
            if (arg.find("-b") != -1 && argc >= 3)
                MIN_BIT = atoi(argv[2]);
            if (argc >= 4) {
                arg = argv[3];
                if (arg.find("-m") != -1)
                    show_mid = true;
            }
            else
            {
                cout << "Invalid argumentn"
                    << "-b Xtminimum bits should be X.n"
                    << "-m  tshow mid process computation.n"
                    << "-h  tshow this.n";
            }
        }
    }
    do
    {
        cout << "nEnter the number of variables to be Minimizedn";
        cin >> m;
        getinput();
        init();
        cout << "Press 'y' to Reduce Another Expression or 'n' to Close this Application";
        cin >> choice;
    } while (choice == 'y');
WINPAUSE;
return 0;

}

编辑:这是getInput()和init()

的代码
void getinput() {
unsigned in;
int num_bits = 0;
cout << "nInput value followed by ENTER[^D ends input]n> ";
while (cin >> in) {
    input_values.push_back(in);
    num_bits = count_bits(in);
    if (num_bits>MIN_BIT)
        MIN_BIT = num_bits;
    cout << "> ";
}
}
/*return min number of bits a number is represented by. used for best output*/
unsigned count_bits(unsigned n) {
    short bit = 0;
    int count = 0;
    while (n>0) {
        bit = n % 2;
        n >>= 1;
        count++;
    }
    return count;
}

void init() {
table.resize(1);
p_group.resize(1);
final_group.resize(1);
create_table();
print_table();
create_p_group();
if (show_mid)
    print_p_group();
create_final_group();
print_final_group();

}

您的逻辑在许多地方存在缺陷,尽管一堆全球群使它有些混淆,但有些事情很清楚:

  • 停止在每次迭代上的重新处理命令行参数。
  • 意识到char的格式提取不会跳过空格

如果您正确解析命令行参数,则可能是正确的。简而言之,如果您在到达循环之前终止程序,则意味着您的命令行参数不合适,或者您的解析逻辑被损坏

后者是使您的循环适当终止的原因。您先前格式化的输入,除非消耗全线数据(包括尾随新线),否则将在输入流上至少留下新线。如果不清除这一点, choice的格式读取的字符将简单地使用它,这显然不是值'y'。为此,在阅读choice之前,请通过任何NEWLINE在输入流中丢弃任何数据。注意:如果您的输入处理使用诸如std::getline之类的东西,则不需要此步骤。

您的循环应该看起来像这样:

int main(int argc, char *argv[])
{
    /* configure command line argument settings ONCE */
    if (argc >= 2)
    {
        string arg = argv[1];
        if (arg.find("-m") != -1) {
            show_mid = true;
            if (argc >= 3) {
                arg = argv[2];
                if (arg.find("-b") != -1)
                    MIN_BIT = atoi(argv[3]);
            }
        }
        else if (arg.find("-h") != -1) {
            cout << "-b Xtminimum bits should be X.n"
            << "-m  tshow mid process computation.n"
            << "-h  tshow this.n";
            return 0;
        }
        else {
            if (arg.find("-b") != -1 && argc >= 3)
                MIN_BIT = atoi(argv[2]);
            if (argc >= 4) {
                arg = argv[3];
                if (arg.find("-m") != -1)
                    show_mid = true;
            }
            else
            {
                cout << "Invalid argumentn"
                << "-b Xtminimum bits should be X.n"
                << "-m  tshow mid process computation.n"
                << "-h  tshow this.n";
                return 0;
            }
        }
    }
    cout << "Designed By a Student For the Students :)n";
    char choice = 'y';
    do
    {
        cout << "nEnter the number of variables to be Minimizedn";
        if (cin >> m)
        {
            getinput();
            init();
            cout << "Press 'y' to Reduce Another Expression or 'n' to Close this Application";
            // flush through eoln, read prompt
            std::cin.ignore(std::numeric_limits<std::streamsize>::max(), 'n');
            if (!(std::cin >> choice))
                break;
        }
        else
        {   // could not extract a valid value for m. not much more we can do.
            break;
        }
    } while (choice == 'y');
    return 0;
}

移动在循环之前检查命令行参数,如这样:

cout << "nEnter the number of variables to be Minimizedn";
cin >> m;
if (argc >= 2)
{
        string arg = argv[1];
        ...
        else
        {
            cout << "Invalid argumentn"
                << "-b Xtminimum bits should be X.n"
                << "-m  tshow mid process computation.n"
                << "-h  tshow this.n";
            return 0;
        }
    }
}
do
{
    // your logic, init(), etc.
    cout << "Press 'y' to Reduce Another Expression or 'n' to Close this Applicationn";
    cin >> choice;
} while (choice == 'y');

然后您的

您有很多这样的陈述:

if (arg.find("-m") != -1)

您应该使用string::npos来测试未找到的匹配项,如std::string::find中所建议。因此,将它们更改为这样:

if (arg.find("-m") != string::npos)

我必须使主函数void

no。

不要那样做。c和c 中的main()应该返回什么?int是答案。