【发布时间】:2018-03-04 06:57:04
【问题描述】:
所以我有一个简化布尔表达式的程序。我想要实现的是,在第一个表达式的简化结束时,我希望用户选择是要简化另一个表达式还是退出控制台应用程序(程序)。
这是 Main 函数的代码
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 Minimized\n";
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 X\tminimum 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 argument\n"
<< "-b X\tminimum 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 while 循环,但我在这里面临两个问题,即程序在没有用户输入的情况下终止,如果我使用 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 X\tminimum 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 argument\n"
<< "-b X\tminimum bits should be X.\n"
<< "-m \tshow mid process computation.\n"
<< "-h \tshow this.\n";
}
}
}
do
{
cout << "\nEnter the number of variables to be Minimized\n";
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();
}
【问题讨论】:
-
如果您打算致电
main(),请不要这样做,这是禁止的(未定义行为)。 -
任何键,包括'y'?
-
由于您在 else if 和 else 块中有 return 0...程序终止...删除它们...
-
@VijayKalmath 是的,包括“y”
-
唉,大量不明智的全局变量使这变得相当模糊。我看到的两个
return-s 用于转储使用指南,然后终止程序(完全合理)。当 命令行参数(不是用户提示)不是您所期望的时,它们会触发。m的值在您通过命令行启动参数之前是毫无价值的,您似乎出于某种原因对每次迭代都进行了重新处理。坦率地说,这些全局变量的所有设置可能最好在此循环之外完成。
标签: c++ loops recursion main do-while