初学者的C++练习题——(四)煎饼哥

Pancake Glutton

Requires:
variables, data types, and numerical operators
basic input/output
logic (if statements, switch statements)
loops (for, while, do-while)
arrays

Write a program that asks the user to enter the number of pancakes eaten for breakfast by 10 different people (Person 1, Person 2, ..., Person 10)
Once the data has been entered the program must analyze the data and output which person ate the most pancakes for breakfast.

#include using namespace std; class Person { private: int id_; int pancakeEaten_; public: void setId(int id) { id_ = id; } int getId() { return id_; } void setPancakeEaten(int pancakeEaten) { pancakeEaten_ = (pancakeEaten > 0) ? pancakeEaten : 0; } int getPancakeEaten() { return pancakeEaten_; } }; int main() { int personCount = 10; Person* person = new Person[personCount]; for(int i=0; i> input; person[i].setPancakeEaten(input); } int idOfPersonEatMost = 1; int maxPancakeEaten = person[0].getPancakeEaten(); for(int i=1; i

★ Modify the program so that it also outputs which person ate the least number of pancakes for breakfast.
#include using namespace std; class Person { private: int id_; int pancakeEaten_; public: void setId(int id) { id_ = id; } int getId() { return id_; } void setPancakeEaten(int pancakeEaten) { pancakeEaten_ = (pancakeEaten > 0) ? pancakeEaten : 0; } int getPancakeEaten() { return pancakeEaten_; } }; int main() { int personCount = 10; Person* person = new Person[personCount]; for(int i=0; i> input; person[i].setPancakeEaten(input); } int idOfPersonEatMost = 1; int idOfPersonEatLeast = idOfPersonEatMost; int maxPancakeEaten = person[0].getPancakeEaten(); int minPancakeEaten = maxPancakeEaten; for(int i=1; i person[i].getPancakeEaten()) { minPancakeEaten = person[i].getPancakeEaten(); idOfPersonEatLeast = person[i].getId(); } } cout << "Person " << idOfPersonEatMost << " ate the most number of pancakes for breakfast." << endl; cout << "Person " << idOfPersonEatLeast << " ate the least number of pancakes for breakfast." << endl; system("pause"); }

★★★★ Modify the program so that it outputs a list in order of number of pancakes eaten of all 10 people.
i.e.
Person 4: ate 10 pancakes
Person 3: ate 7 pancakes
Person 8: ate 4 pancakes
...
Person 5: ate 0 pancakes

#include using namespace std; class Person { private: int id_; int pancakeEaten_; public: void setId(int id) { id_ = id; } int getId() { return id_; } void setPancakeEaten(int pancakeEaten) { pancakeEaten_ = (pancakeEaten > 0) ? pancakeEaten : 0; } int getPancakeEaten() { return pancakeEaten_; } }; void swap(Person* a, Person* b) { int id = a->getId(); a->setId(b->getId()); b->setId(id); int pancakeEaten = a->getPancakeEaten(); a->setPancakeEaten(b->getPancakeEaten()); b->setPancakeEaten(pancakeEaten); } int main() { int personCount = 10; Person* person = new Person[personCount]; for(int i=0; i> input; person[i].setPancakeEaten(input); } for(int i=0; i person[j+1].getPancakeEaten()) { swap(&person[j], &person[j+1]); } } } for(int i=0; i

你可能感兴趣的:(C/C++)