我正在关注我在 C++ 论坛上找到的一组初学者作业,但我现在完全陷入了一项任务。任务如下:
编写一个程序,要求用户输入 10 个不同的人(第 1 个人、第 2 个人、...、第 10 个人)早餐吃的煎饼数量。一旦输入数据,程序必须分析数据并输出哪个人早餐吃的煎饼最多。
★ 修改程序,让它也输出哪个人早餐吃的煎饼最少。
★★★★ 修改程序,使其按照10个人吃的煎饼数量的顺序输出一个列表。
现在我已经整理出了原始位和一星位,但是我选择让自己更困难一点,而不是仅仅使用“人 1、2、3、4 等等,而是我已经分配给字符命名,然后用 . 打印出来switch
。这是我当前的代码,我希望得到一些关于如何在不弄乱数组中数字顺序的情况下对数组进行排序的建议,因为这会弄乱我的命名我猜的代码。
这是我的代码,我知道它不是最漂亮的代码,但它现在可以使用。
#include "stdafx.h"
#include <iostream>
using namespace std;
int main()
{
int nMostPancakesName;
int nLeastPancakesName;
enum BreakfastNames
{
NED, // 0
ARYA, // 1
JON, // 2
ROBB, // 3
SANSA, // 4
CATELYN, // 5
BRAN, // 6
THEON, // 7
HODOR, // 8
GHOST // 9
};
int anArray[10];
cout << "Enter the number of pancakes Ned ate for breakfast: " << endl;
cin >> anArray[NED];
cout << "How many did Arya eat?" << endl;
cin >> anArray[ARYA];
cout << "And Jon?" << endl;
cin >> anArray[JON];
cout << "What about Robb?" << endl;
cin >> anArray[ROBB];
cout << "Did Sansa have any?" << endl;
cin >> anArray[SANSA];
cout << "Catelyn?" << endl;
cin >> anArray[CATELYN];
cout << "Crippleboy aka Bran?" << endl;
cin >> anArray[BRAN];
cout << "The traitor didn't get any, did he?" << endl;
cin >> anArray[THEON];
cout << "Hodor?" << endl;
cin >> anArray[HODOR];
cout << "No pets at the dining table, Ghost." << endl;
cin >> anArray[GHOST];
int nMaxPancakes = 0;
for (int nPancakes = 0; nPancakes < 10; nPancakes++)
if (anArray[nPancakes] > nMaxPancakes)
{
nMostPancakesName = nPancakes;
nMaxPancakes = anArray[nPancakes];
}
int nLeastPancakes = 100;
for (int nPancakes2 = 0; nPancakes2 < 10; nPancakes2++)
if (anArray[nPancakes2] < nLeastPancakes)
{
nLeastPancakesName = nPancakes2;
nLeastPancakes = anArray[nPancakes2];
}
for (int nStartIndex = 0; nStartIndex < 10; nStartIndex++)
{
int nSmallestIndex = nStartIndex;
for (int nCurrentIndex = nStartIndex + 1; nCurrentIndex < 10; nCurrentIndex++)
{
if (anArray[nCurrentIndex] < anArray[nSmallestIndex])
nSmallestIndex = nCurrentIndex;
}
}
switch(nMostPancakesName)
{
case 0:
cout << "Ned had " << nMaxPancakes << endl;
break;
case 1:
cout << "Arya had " << nMaxPancakes << endl;
break;
case 2:
cout << "Jon had " << nMaxPancakes << endl;
break;
case 3:
cout << "Robb had " << nMaxPancakes << endl;
break;
case 4:
cout << "Sansa had " << nMaxPancakes << endl;
break;
case 5:
cout << "Catelyn had " << nMaxPancakes << endl;
break;
case 6:
cout << "Bran had " << nMaxPancakes << endl;
break;
case 7:
cout << "Theon had " << nMaxPancakes << endl;
break;
case 8:
cout << "Hodor had " << nMaxPancakes << endl;
break;
case 9:
cout << "Ghost had " << nMaxPancakes << endl;
break;
}
switch(nLeastPancakesName)
{
case 0:
cout << "Ned had " << nLeastPancakes << endl;
break;
case 1:
cout << "Arya had " << nLeastPancakes << endl;
break;
case 2:
cout << "Jon had " << nLeastPancakes << endl;
break;
case 3:
cout << "Robb had " << nLeastPancakes << endl;
break;
case 4:
cout << "Sansa had " << nLeastPancakes << endl;
break;
case 5:
cout << "Catelyn had " << nLeastPancakes << endl;
break;
case 6:
cout << "Bran had " << nLeastPancakes << endl;
break;
case 7:
cout << "Theon had " << nLeastPancakes << endl;
break;
case 8:
cout << "Hodor had " << nLeastPancakes << endl;
break;
case 9:
cout << "Ghost had " << nLeastPancakes << endl;
break;
}
return 0;
}