要求: 变量,数据类型和数字运算符 基本输入/输出 逻辑(if语句,switch语句) 循环(for,while,do-while) 阵列
写一个程序,要求用户输入由10个不同的人(第1人,第2人,......,第10人)吃早餐的煎饼数量 一旦输入数据,程序必须分析数据并输出哪个人吃早餐最煎饼。
★修改程序,以便输出哪个人吃早餐的煎饼数量最少。
★★★★修改程序,使其按照所有10个人吃的煎饼数量顺序输出一个列表。 即 人4:吃了10个煎饼 人3:吃了7个煎饼 人8:吃了4个煎饼 ... 人5:吃了0个煎饼
我写的当前版本:http://codepad.org/QHnt11CT
#include <iostream>
#include <string>
void bubbleSort(int arr[], int n) {
bool swapped = true;
int j = 0;
int tmp;
while (swapped) {
swapped = false;
j++;
for (int i = 0; i < n - j; i++) {
if (arr[i] > arr[i + 1]) {
tmp = arr[i];
arr[i] = arr[i + 1];
arr[i + 1] = tmp;
swapped = true;
}
}
}
}
int main()
{
int pancakeAmount[10];
std::string consumers[10];
for (int i = 0, j = 1; i < 10; i++, j++) {
std::cout << "Please enter an amount of pancakes eaten by consumer"\
" number " << j << "." << std::endl;
std::cin >> pancakeAmount[i];
std::cout << "Please enter the name of the person who ate that amount"\
" of pancakes." << std::endl;
getline(std::cin, consumers[i]);
}
std::cout << "The results from least amount eaten to the greatest amount"\
" eaten are as follows:" << std::endl;
bubbleSort(pancakeAmount, 10);
for (int k = 0; k < 10; k++) {
std::cout << pancakeAmount[k] << std::endl;
}
return 0;
}
这是我目前正在处理的问题。截至目前,这个问题的前两个目标已经解决,没有任何明显的问题。
然而,第三个目标证明有点困难。我很难设计/实现带有适当标签的排序列表。在第47行,我试图获得一个名称或标签,以给予相应的金额。我遇到的问题是控制台会接受我想要分配的金额,但是会完全忽略对getline()函数的调用并循环回到要求另一个金额。当在调用“std :: cin&gt;&gt; pancakeAmount [i]”之前调用getline()函数时,我可以在第一个循环上给出输入,但是连续循环会产生我在getline()时遇到的错误函数在代码中处于原始位置。
我是否尝试以不正确的方式使用字符串数组,或者getline()函数未正确使用?
答案 0 :(得分:0)
所以今天是你的幸运日,因为通常我不做作业 你的问题是你需要忽略你的回报。
所以这应该可以解决你的问题但是没有经过测试:
struct PanCakeEater
{
int pancakeEaten;
std::string name;
};
bool PanCakeSort(PanCakeEater const& eater1, PanCakeEater const& eater2)
{
return eater1.pancakeEaten < eater2.pancakeEaten;
}
int main()
{
std::vector<PanCakeEater> eaters;
const size_t numberOfEaters = 3;
for(size_t i = 0; i < numberOfEaters; ++i)
{
PanCakeEater p;
std::cout << "Please enter an amount of pancakes eaten by consumer number " << i << "." << std::endl;
std::cin >> p.pancakeEaten;
std::cin.ignore(); // ignore the \n
std::cout << "Please enter the name of the person who ate that amount of pancakes." << std::endl;
getline(std::cin, p.name);
eaters.push_back(p);
}
std::sort(eaters.begin(), eaters.end(), PanCakeSort);
for(auto iter = eaters.begin(); iter != eaters.end(); ++iter)
{
std::cout << iter->name << ": " << iter->pancakeEaten << std::endl;
}
return 0;
}
答案 1 :(得分:0)
#include<iostream>
using namespace std;
int main()
{
int ate[10],eater[10],hold,temp;
cout<<"\t\tEnter How Many Pancakes Did Each Person Eat?\n\n";
for(int h=0;h<10;h++)
{
eater[h]=h;
}
for(int q=0;q<10;q++)
{
cout<<"Person "<<eater[q]+1<<" Ate: ";
cin>>ate[q];
}
cout<<"____________________________________________\n\n"<<"\t\tOrdered List Of Above.\n\n";
for(int a=0;a<10;a++)
{
for(int b=0;b<10;b++)
{
if(ate[b]<ate[b+1])
{
hold=ate[b];
ate[b]=ate[b+1];
ate[b+1]=hold;
temp=eater[b];
eater[b]=eater[b+1];
eater[b+1]=temp;
}
}
}
for(int w=0;w<10;w++)
{
cout<<"Person "<<eater[w]+1<<" Ate "<<ate[w]<<" Pancakes\n";
}
return 0;
}
答案 2 :(得分:0)
这是它的简单版本。必须让某些初学者更容易理解。
def open_instructions_window():
def close():
w.destroy()
w = tkinter.Tk()
w.geometry("550x500")
w.title("Instructions")
w.configure(background = "black")
label_1 = Label(w, text = "Instructions", font = "none 17 bold underline",bg = "black", fg = "brown")
label_1.grid(row = 1, sticky = W)
exit_button = Button(w, text = "CLOSE", bg = "brown" ,fg = "white",relief=SUNKEN, command = close)
exit_button.grid(row = 20, column = 1, sticky = E)
w.mainloop()# Because of this I couldn't get back to the menu
menu()