所以我有一个简化布尔表达式的程序。我想要实现的是,在第一个表达式的简化结束时,我希望用户选择是否要简化另一个表达式或者只是退出控制台应用程序(程序)。
这是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();
}
答案 0 :(得分:1)
你的逻辑在许多地方存在缺陷,虽然一堆全局变量使它有些混淆,但有些事情很清楚:
char
的格式化提取不会跳过空白如果您正确解析命令行参数,那么前者可能是正确的。简而言之,如果您发现程序在到达循环之前终止,则意味着您的命令行参数不正确,或者您的解析逻辑已损坏
后者是阻止你的循环正常终止的原因。除非使用完整行数据(包括尾部换行符),否则之前的格式化输入将在输入流上留下至少一个换行符。如果不清除,choice
的格式化字符只会使用它,显然不是值'y'
。为了适应这一点,在阅读choice
之前,通过任何换行丢弃输入流中的任何数据。注意:如果您的输入处理使用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 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;
}
}
}
cout << "Designed By a Student For the Students :)\n";
char choice = 'y';
do
{
cout << "\nEnter the number of variables to be Minimized\n";
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;
}
答案 1 :(得分:0)
在for循环之前移动检查命令行参数的部分,如下所示:
cout << "\nEnter the number of variables to be Minimized\n";
cin >> m;
if (argc >= 2)
{
string arg = argv[1];
...
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;
}
}
}
do
{
// your logic, init(), etc.
cout << "Press 'y' to Reduce Another Expression or 'n' to Close this Application\n";
cin >> choice;
} while (choice == 'y');
然后你的
你有很多这样的陈述:
if (arg.find("-m") != -1)
您应该使用string::npos
来测试未找到的匹配项,如std::string::find
中所述。因此,以这种方式改变它们:
if (arg.find("-m") != string::npos)
我必须使主要功能无效
NO。
不要那样做。答案是What should main() return in C and C++? int
。