我必须为我的算法类编写一个红黑树,然后编写一个菜单用于插入、删除、搜索等……元素。所以我认为 Switch 语句将是要走的路,但是当它打破一个案例时,它总是直接进入 return 语句并结束程序,即使我没有输入“0”。
我已经连续工作了大约 24 小时,并在其中的最后 16 小时进行了编程,所以如果它是愚蠢的或者我没有很好地解释它,我深表歉意。
while ( true )
{
int userinput = NULL;
PrintMenu();
cin >> userinput;
cin.clear();
cin.ignore( 10000 , '\n' );
switch ( userinput )
{
case 0:
{
return 0;
}
case 1:
{
while (true )
{
cout << "Enter an integer to be entered into the Red Black Tree or any letter to exit:\n";
if ( !(cin >> userinput) )
break;
cin.clear();
cin.ignore( 10000, '\n' );
data.Insert( userinput );
data.PrintInOrder();
}
}
case 2:
{
while (true )
{
cout << "WARNING : This mode allows entering of duplicate numbers.\n";
cout << "Enter an integer to be entered into the Red Black Tree or any letter to exit:\n";
if ( !(cin >> userinput) )
break;
cin.clear();
cin.ignore( 10000, '\n' );
data.Insert( userinput );
data.PrintInOrder();
}
}
case 3:
{
while (true )
{
cout << "Enter an integer to search the Red Black Tree for or any letter to exit:\n";
if ( !(cin >> userinput) )
break;
cin.clear();
cin.ignore( 10000, '\n' );
data.Search( userinput );
}
}
case 4:
{
while (true )
{
cout << "Enter an integer to be deleted from the Red Black Tree or any letter to exit:\n";
if ( !(cin >> userinput) )
break;
cin.clear();
cin.ignore( 10000, '\n' );
data.Delete( userinput );
data.PrintInOrder();
}
}
case 5:
{
while (true )
{
cout << "WARNING : This mode deletes all copies of an integer.\n";
cout << "Enter an integer to delete from the Red Black Tree or any letter to exit:\n";
if ( !(cin >> userinput) )
break;
cin.clear();
cin.ignore( 10000, '\n' );
data.DeleteAll( userinput );
data.PrintInOrder();
}
}
case 6:
data.PrintInOrder();
break;
case 7:
{
cout << "This Function tests if the tree passes all 5 Criteria of a Red Black Tree.\n";
data.IsRBT();
cout << "Test Finished, if you see no Violations then it passed. Press any letter to exit.\n";
if ( !(cin >> userinput) )
break;
}
}
system("CLS");
data.PrintInOrder();
}