关于返回的有关问题求解
关于返回的问题求解
#include "stdafx.h"
#include <iostream>
#include <iomanip>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
using std::cout;
using std::endl;
using std::cin;
using std::swap;
using std::setw;
int main() {
const int max(10);
int a[max], i, n;
for (i = 0; i<10; i++)
{
cout << "请输入你想比较的10个数" << endl;
cin >> a[i];
}
bool sorted = false;
while (!sorted)
{
sorted = true;
for (i = 1; i<10; i++){
if (a[i - 1] >a[i]){
swap(a[i - 1], a[i]);
sorted = false;
}
}
}
cout << "********************" << endl;
cout << "*请键入相应数操作字*" << endl;
cout << "*1、从小到大排序 *" << endl;
cout << "*2、从大到小排序 *" << endl;
cout << "*3、给出中间值 *" << endl;
cout << "*4、给出最小值 *" << endl;
cout << "*5、给出最大值 *" << endl;
cout << "********************" << endl;
cin >> n;
switch (n)
{
case 1: cout << "10个数从小到大为:" << endl;
for (i = 0; i<10; i++){
cout << setw(6) << a[i];
}break;
case 2: cout << "10个数从大到小为:" << endl;
for (i = 10 - 1; i >= 0; i--){
cout << setw(6) << a[i];
}break;
case 3: cout << a[4] << endl;
break;
case 4: cout << a[9] << endl;
break;
case 5: cout << a[0] << endl;
break;
}
return 0;
}
这是小弟随便做的一个关于10个数字排序的程序代码,我想让这个程序能执行一次其中一个功能后再返回上级菜单继续可以选择执行另一部分功能,求解方法
------解决思路----------------------
可以将各个功能模块写成一个个函数,然后通过对输入的数字判断进而调用相关的函数
------解决思路----------------------
------解决思路----------------------
#include "stdafx.h"
#include <iostream>
#include <iomanip>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
using std::cout;
using std::endl;
using std::cin;
using std::swap;
using std::setw;
int main() {
const int max(10);
int a[max], i, n;
for (i = 0; i<10; i++)
{
cout << "请输入你想比较的10个数" << endl;
cin >> a[i];
}
bool sorted = false;
while (!sorted)
{
sorted = true;
for (i = 1; i<10; i++){
if (a[i - 1] >a[i]){
swap(a[i - 1], a[i]);
sorted = false;
}
}
}
cout << "********************" << endl;
cout << "*请键入相应数操作字*" << endl;
cout << "*1、从小到大排序 *" << endl;
cout << "*2、从大到小排序 *" << endl;
cout << "*3、给出中间值 *" << endl;
cout << "*4、给出最小值 *" << endl;
cout << "*5、给出最大值 *" << endl;
cout << "********************" << endl;
cin >> n;
switch (n)
{
case 1: cout << "10个数从小到大为:" << endl;
for (i = 0; i<10; i++){
cout << setw(6) << a[i];
}break;
case 2: cout << "10个数从大到小为:" << endl;
for (i = 10 - 1; i >= 0; i--){
cout << setw(6) << a[i];
}break;
case 3: cout << a[4] << endl;
break;
case 4: cout << a[9] << endl;
break;
case 5: cout << a[0] << endl;
break;
}
return 0;
}
这是小弟随便做的一个关于10个数字排序的程序代码,我想让这个程序能执行一次其中一个功能后再返回上级菜单继续可以选择执行另一部分功能,求解方法
------解决思路----------------------
可以将各个功能模块写成一个个函数,然后通过对输入的数字判断进而调用相关的函数
------解决思路----------------------
while (true)
{
// 输入选择
// 跟你一样使用 switch,并设置一个数字用于break 出 while 循环
}
------解决思路----------------------
#include <iostream>
#include <iomanip>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
using std::cout;
using std::endl;
using std::cin;
using std::swap;
using std::setw;
int main()
{
const int max(10);
int a[max], i, n;
cout << "请输入你想比较的10个数" << endl;
for (i = 0; i<10; i++)
{
cin >> a[i];
}
bool sorted = false;
while (!sorted)
{
sorted = true;
for (i = 1; i<10; i++){
if (a[i - 1] >a[i]){
swap(a[i - 1], a[i]);
sorted = false;
}
}
}
while(1)
{
cout << "********************" << endl;
cout << "*请键入相应数操作字*" << endl;
cout << "*1、从小到大排序 *" << endl;
cout << "*2、从大到小排序 *" << endl;
cout << "*3、给出中间值 *" << endl;
cout << "*4、给出最小值 *" << endl;
cout << "*5、给出最大值 *" << endl;
cout << "********************" << endl;
cin >> n;
switch (n)
{
case 1: cout << "10个数从小到大为:" << endl;
for (i = 0; i<10; i++){
cout << setw(6) << a[i];
}break;
case 2: cout << "10个数从大到小为:" << endl;
for (i = 10 - 1; i >= 0; i--){
cout << setw(6) << a[i];
}break;
case 3: cout << a[4] << endl;
break;
case 4: cout << a[9] << endl;
break;
case 5: cout << a[0] << endl;
break;
default:
exit(0);
}
}
return 0;
}