如何在迭代后重复从主函数重复执行
How to Repeat Execution from Main function after an Iteration
所以我有一个程序,可以简化布尔表达式。我要实现的是,在简化第一个表达式的结束时,我希望用户选择他是否要简化另一个表达式或仅退出控制台应用程序(程序)。
这是主函数的代码
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
是答案。
相关文章:
- "error: no matching function for call to"构造函数错误
- 什么时候调用组成单元对象的析构函数
- 继承函数的重载解析
- 为什么随机数生成器不在void函数中随机化数字,而在main函数中随机化
- C++模板来检查友元函数的存在
- 递归函数计算序列中的平方和(并输出过程)
- 对RValue对象调用的LValue ref限定成员函数
- C++17复制构造函数,在std::unordereded_map上进行深度复制
- 将数组作为参数传递给函数安全吗?作为第三方职能部门,可以探索他们想要的之外的其他元素
- 在C++STL中是否有Polyval(Matlab函数)等价物?
- 为什么使用 "this" 指针调用派生成员函数?
- 将对象数组的引用传递给函数
- 函数调用中参数的顺序重要吗
- 函数向量_指针有不同的原型,我可以构建一个吗
- 使用不带参数的函数访问结构元素
- 代码在main()中运行,但在函数中出现错误
- 内置函数可查看CPP中的成员变量
- 将 2D 推力::d evice_vector 复矩阵传递给 CUDA 内核函数
- 用于复选框的c++工具提示函数
- 特征:选择复矩阵上的函数