如何摆脱此计划中的goto
语句并结束flip:>>>
?
int main()
{
int array[10];
int sum = 0;
int desending;
int mul = 1;
float avg = 10;
int option;
do
{
flip1:
cout << "\n\t\t------------------------------------------------" << endl;
cout << "\t\tlllllllll Menue lllllllllll" << endl;
cout << "\t\tlllllllll 1. Array Basics lllllllllll" << endl;
cout << "\t\tlllllllll 2. Searching Array lllllllllll" << endl;
cout << "\t\tlllllllll 3. Sorting Array lllllllllll" << endl;
cout << "\t\tlllllllll 4. Exit lllllllllll" << endl;
cout << "\t\t------------------------------------------------\n" << endl;
cout << "Please select an option" << endl;
cin >> option;
system("cls");
flip2:
if (option == 1)
{
int h = 1;
int i;
for (i = 0; i < 10; i++)
{
cout << h << " Enter the Numbers ";
cin >> array[i];
sum = sum + array[i];
mul = mul*array[i];
h++;
}
system("cls");
for (int j = 0; j < 10; j++)
{
cout << "Given numbers" << j << " = " << array[j] << "\n";
}
avg = sum / avg;
int max = array[0];
int min = array[0];
for (int i = 0; i < 10; i++)
{
if (array[i] > max)
{
max = array[i];
}
else if (array[i] < min)
{
min = array[i];
}
}
cout << "\nSum of the Numbers= 4" << sum << endl;
cout << "\nProduct of the Numbers is = " << mul << endl;
cout << "\nThe Avg of Numbers is = " << avg << endl;
cout << "\nmaximum of the Numbers is = " << max << endl;
cout << "\nminimum of the Numbers is = " << min << endl;
}
else if (option == 2)
{
int array[10], o, index = -1;
cout << "enter the elements of array" << endl;
for (o = 0; o < 10; o++)
{
cin >> array[o];
}
system("cls");
int p;
cout << "enter value to find" << endl;
cin >> p;
for (int c = 0; c < 10; c++)
{
if (array[c] == p)
index = c;
}
system("cls");
if (index == -1)
{
cout << "no value found" << endl;
}
else
cout << "Value found at\t" << index << endl;
}
else if (option == 3)
{
system("cls");
int a[10], r, t, temp;
cout << "Enter the array elements: " << endl;
for (r = 0; r<10; ++r)
cin >> a[r];
system("cls");
for (r = 0; r<10; ++r)
for (t = 0; t<9; t++)
if (a[r]<a[t])
{
temp = a[r];
a[r] = a[t];
a[t] = temp;
}
cout << "Array after sorting: " << endl;
for (r = 0; r<10; r++)
cout << a[r] << "\t ";
}
cout << "\nplease select an option\n" << endl;
cout << "press m for main menue" << endl;
cout << "press r for repeat" << endl;
cout << "press e for exit" << endl;
char q;
cin >> q;
if (q == 'm')
{
system("cls");
goto flip1;
}
if (q == 'r')
{
system("cls");
goto flip2;
}
if (q == 'e')
{
return 0;
}
} while (option != 4);
return 0;
}
答案 0 :(得分:1)
您可以参考以下代码。
进入循环之前。
cout << "\n\t\t------------------------------------------------" << endl;
cout << "\t\tlllllllll Menue lllllllllll" << endl;
cout << "\t\tlllllllll 1. Array Basics lllllllllll" << endl;
cout << "\t\tlllllllll 2. Searching Array lllllllllll" << endl;
cout << "\t\tlllllllll 3. Sorting Array lllllllllll" << endl;
cout << "\t\tlllllllll 4. Exit lllllllllll" << endl;
cout << "\t\t------------------------------------------------\n" << endl;
cout << "Please select an option" << endl;
cin >> option;
system("cls");
do {
if (option == 1)
{
int h = 1;
int i;
for (i = 0; i < 10; i++)
{
/* All text as same */
最后:改变
if (q == 'm')
{
system("cls");
goto flip1;
}
if (q == 'r')
{
system("cls");
goto flip2;
}
if (q == 'e')
{
return 0;
}
要。
if (q == 'e')
{
return 0;
}
system("cls");
if (q == 'm')
{
cout << "\n\t\t------------------------------------------------" << endl;
cout << "\t\tlllllllll Menue lllllllllll" << endl;
cout << "\t\tlllllllll 1. Array Basics lllllllllll" << endl;
cout << "\t\tlllllllll 2. Searching Array lllllllllll" << endl;
cout << "\t\tlllllllll 3. Sorting Array lllllllllll" << endl;
cout << "\t\tlllllllll 4. Exit lllllllllll" << endl;
cout << "\t\t------------------------------------------------\n" << endl;
cout << "Please select an option" << endl;
cin >> option;
system("cls");
}
PS:您还可以创建一个打印菜单的功能,从用户那里获取输入并返回相同的功能。
答案 1 :(得分:1)
如果将代码划分为具有内聚功能的块,则可以将它们放入自己的函数中,main
函数可以更加简单。这是我的建议:
#include <iostream>
#include <cstdlib>
using namespace std;
int getOption1()
{
int option;
cout << "\n\t\t------------------------------------------------" << endl;
cout << "\t\tlllllllll Menue lllllllllll" << endl;
cout << "\t\tlllllllll 1. Array Basics lllllllllll" << endl;
cout << "\t\tlllllllll 2. Searching Array lllllllllll" << endl;
cout << "\t\tlllllllll 3. Sorting Array lllllllllll" << endl;
cout << "\t\tlllllllll 4. Exit lllllllllll" << endl;
cout << "\t\t------------------------------------------------\n" << endl;
cout << "Please select an option" << endl;
cin >> option;
system("cls");
return option;
}
char getOption2()
{
char option;
cout << "\nplease select an option\n" << endl;
cout << "press m for main menue" << endl;
cout << "press r for repeat" << endl;
cout << "press e for exit" << endl;
cin >> option;
return option;
}
void doArrayBasics()
{
int array[10];
int sum = 0;
float avg = 10;
int mul = 1;
int h = 1;
int i;
for (i = 0; i < 10; i++)
{
cout << h << " Enter the Numbers ";
cin >> array[i];
sum = sum + array[i];
mul = mul*array[i];
h++;
}
system("cls");
for (int j = 0; j < 10; j++)
{
cout << "Given numbers" << j << " = " << array[j] << "\n";
}
avg = sum / avg;
int max = array[0];
int min = array[0];
for (int i = 0; i < 10; i++)
{
if (array[i] > max)
{
max = array[i];
}
else if (array[i] < min)
{
min = array[i];
}
}
cout << "\nSum of the Numbers= 4" << sum << endl;
cout << "\nProduct of the Numbers is = " << mul << endl;
cout << "\nThe Avg of Numbers is = " << avg << endl;
cout << "\nmaximum of the Numbers is = " << max << endl;
cout << "\nminimum of the Numbers is = " << min << endl;
}
void doArraySearching()
{
int array[10];
int o;
int index = -1;
cout << "enter the elements of array" << endl;
for (o = 0; o < 10; o++)
{
cin >> array[o];
}
system("cls");
int p;
cout << "enter value to find" << endl;
cin >> p;
for (int c = 0; c < 10; c++)
{
if (array[c] == p)
index = c;
}
system("cls");
if (index == -1)
{
cout << "no value found" << endl;
}
else
cout << "Value found at\t" << index << endl;
}
void doArraySorting()
{
system("cls");
int a[10];
int r;
int t;
int temp;
cout << "Enter the array elements: " << endl;
for (r = 0; r<10; ++r)
cin >> a[r];
system("cls");
for (r = 0; r<10; ++r)
for (t = 0; t<9; t++)
if (a[r]<a[t])
{
temp = a[r];
a[r] = a[t];
a[t] = temp;
}
cout << "Array after sorting: " << endl;
for (r = 0; r<10; r++)
cout << a[r] << "\t ";
}
int main()
{
int option1;
char option2 = 'r';
do
{
if ( option2 == 'r' )
{
option1 = getOption1();
}
switch (option1)
{
case 1:
doArrayBasics();
break;
case 2:
doArraySearching();
break;
case 3:
doArraySorting();
break;
case 4:
break;
default:
cout << "Invalid option " << option1 << endl;
}
if ( option1 != 4 )
{
option2 = getOption2();
}
} while (option1 != 4 && option2 != 'e' );
return 0;
}
答案 2 :(得分:1)
您可以使用内部循环删除所有gotos。
删除flip1:
,将flip2:
更改为do {
。循环应该像这样开始:
do
{
cout << "\n\t\t------------------------------------------------" << endl;
cout << "\t\tlllllllll Menue lllllllllll" << endl;
cout << "\t\tlllllllll 1. Array Basics lllllllllll" << endl;
cout << "\t\tlllllllll 2. Searching Array lllllllllll" << endl;
cout << "\t\tlllllllll 3. Sorting Array lllllllllll" << endl;
cout << "\t\tlllllllll 4. Exit lllllllllll" << endl;
cout << "\t\t------------------------------------------------\n" << endl;
cout << "Please select an option" << endl;
cin >> option;
system("cls");
do
{
if (option == 1)
{
在循环结束时,更改:
if (q == 'm')
{
system("cls");
goto flip1;
}
if (q == 'r')
{
system("cls");
goto flip2;
}
if (q == 'e')
{
return 0;
}
到:
if (q == 'e')
{
return 0;
}
system("cls");
} while (q == 'r');
将代码分解为函数也会使其更具可读性,但不一定要删除gotos。
答案 3 :(得分:0)
caveat: the following code not tested
int main()
{
int array[10];
int sum = 0;
int desending;
int mul = 1;
float avg = 10;
int option;
int done = 0;
int looping = 1;
while( !done )
{
cout << "\n\t\t------------------------------------------------" << endl;
cout << "\t\tlllllllll Menue lllllllllll" << endl;
cout << "\t\tlllllllll 1. Array Basics lllllllllll" << endl;
cout << "\t\tlllllllll 2. Searching Array lllllllllll" << endl;
cout << "\t\tlllllllll 3. Sorting Array lllllllllll" << endl;
cout << "\t\tlllllllll 4. Exit lllllllllll" << endl;
cout << "\t\t------------------------------------------------\n" << endl;
cout << "Please select an option" << endl;
cin >> option;
system("cls");
while( looping )
{
looping = 0;
switch( option )
{
case 1:
int h = 1;
int i;
for (i = 0; i < 10; i++)
{
cout << h << " Enter the Numbers ";
cin >> array[i];
sum = sum + array[i];
mul = mul*array[i];
h++;
}
system("cls");
for (int j = 0; j < 10; j++)
{
cout << "Given numbers" << j << " = " << array[j] << "\n";
}
avg = sum / avg;
int max = array[0];
int min = array[0];
for (int i = 0; i < 10; i++)
{
if (array[i] > max)
{
max = array[i];
}
else if (array[i] < min)
{
min = array[i];
}
}
cout << "\nSum of the Numbers= 4" << sum << endl;
cout << "\nProduct of the Numbers is = " << mul << endl;
cout << "\nThe Avg of Numbers is = " << avg << endl;
cout << "\nmaximum of the Numbers is = " << max << endl;
cout << "\nminimum of the Numbers is = " << min << endl;
break;
case 2:
int array[10], o, index = -1;
cout << "enter the elements of array" << endl;
for (o = 0; o < 10; o++)
{
cin >> array[o];
}
system("cls");
int p;
cout << "enter value to find" << endl;
cin >> p;
for (int c = 0; c < 10; c++)
{
if (array[c] == p)
index = c;
}
system("cls");
if (index == -1)
{
cout << "no value found" << endl;
}
else
cout << "Value found at\t" << index << endl;
break;
case 3:
system("cls");
int a[10], r, t, temp;
cout << "Enter the array elements: " << endl;
for (r = 0; r<10; ++r)
cin >> a[r];
system("cls");
for (r = 0; r<10; ++r)
{
for (t = 0; t<9; t++)
{
if (a[r]<a[t])
{
temp = a[r];
a[r] = a[t];
a[t] = temp;
}
}
}
cout << "Array after sorting: " << endl;
for (r = 0; r<10; r++)
cout << a[r] << "\t ";
break;
case 4:
done = 1;
break;
default:
cout << "\nplease select an option\n" << endl;
cout << "press m for main menue" << endl;
cout << "press r for repeat" << endl;
cout << "press e for exit" << endl;
char q;
cin >> q;
switch( q )
{
case 'm':
system("cls");
// looping already set = 0
break;
case 'r':
system("cls");
looping = 1;
break;
case 'e':
done = 1;
break;
default:
break;
} // end switch
break;
} // end switch
} // end while looping
} // end while not done
return 0;
} // end function: main