我可以將我的物件存盤在一個變數的類中,以便我可以列印排名名稱嗎?
問題是撰寫一個程式,詢問五名跑步者的姓名以及他們每人完成一場比賽所用的時間。該程式應顯示誰獲得了第一、第二和第三名。只接受時間的正數。
我應該做任何修改?
#include <iostream>
using namespace std;
class runner{
public:
char name[50];
float time;
void input(){
cout <<"\n Enter the name: ";
cin>> name;
cout <<"\n Enter the time taken to finish the race (mins): ";
cin>> time;
}
};
int main(){
runner runners[5];
for(int i =0;i<5; i ){
runners[i].input();
}
int i,first, second, third, fourth, fifth;
fifth = fourth = third = first = second = INT_MIN;
char firstname, secondname, thirdname, fourthname, fifthname;
for(int i =0;i<5; i ){
if(runners[i].time>first){
fifth = fourth;
fourth = third;
third = second;
second = first;
first = runners[i].time;
}
else if(runners[i].time> second){
fifth = fourth;
fourth = third;
third = second;
second = runners[i].time;
}
else if(runners[i].time>third){
fifth = fourth;
fourth = third;
third = runners[i].time;
}
else if(runners[i].time>fourth){
fifth = fourth;
fourth = runners[i].time;
}
else if(runners[i].time>fifth){
fifth = runners[i].time;
}
}
cout << first <<","<< second <<","<< third <<","<< fourth<< ","<< fifth<<endl;
return 0;
}
uj5u.com熱心網友回復:
您可以使用簡化程式std::multiset如圖所示如下:
#include <iostream>
#include <string>
#include <set>
//using namespace std; //don't use this
class runner{
public:
std::string name; //use string instead of array of char
float time;
void input(){
std::cout <<"\n Enter the name: ";
std::cin>> name;
std::cout <<"\n Enter the time taken to finish the race (mins): ";
std::cin>> time;
}
//overload operator< since we are using std::set
bool operator<( const runner &r ) const
{
return ( time < r.time );
}
};
int main(){
std::multiset<runner> runners; //use multiset instead of array
for(int i =0;i<5; i ){
runner tempRunner;
tempRunner.input();
runners.insert(tempRunner);
}
int i = 0;
//print out the details as asked in the assignment question. You can adjust
//the output according to your needs for example if you want to display only first
//3 position details or what if all the first three persons in the multiset have the same timing
for(const runner &tempRunner: runners)
{
std::cout<<tempRunner.name <<" came: "<<i 1<<std::endl;
i;
}
return 0;
}
使用 Notemultiset而不是set因為多個跑步者可以有相同的時間。
uj5u.com熱心網友回復:
是的你可以。
您可以撰寫自定義比較函式:
class runner
{
public:
std::string name; // why not std::string?
float time;
void input()
{
std::cout <<"\n Enter the name: ";
std::cin >> name;
std::cout <<"\n Enter the time taken to finish the race (mins): ";
std::cin >> time;
}
};
bool cmp (const runner &lhs, const runner &rhs)
{
return lhs.time < rhs.time;
}
std::ostream& operator<< (std::ostream& out, const runner& run)
{
out << run.name << ' ' << run.time;
}
int main()
{
runner runners[5];
for(int i =0;i<5; i )
{
runners[i].input();
}
std::sort(runners, runners 5, cmp);
for(int i {4}; i >= 0; --i)
{
std::cout << runners[i] << ' ' << i << ',';
}
}
或者您可以多載<運算子:
class runner
{
public:
std::string name;
float time;
void input()
{
std::cout <<"\n Enter the name: ";
std::cin >> name;
std::cout <<"\n Enter the time taken to finish the race (mins): ";
std::cin >> time;
}
};
bool operator< (const runner &lhs, const runner &rhs)
{
return lhs.time < rhs.time;
}
int main()
{
runner runners[5];
for(int i =0;i<5; i )
{
runners[i].input();
}
std::sort(runners, runners 5);
for(int i {4}; i >= 0; --i)
{
std::cout << runners[i] << ' ' << i << ',';
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/351237.html
上一篇:添加華為套件時出現“Couldnotfindcom.huawei.hms:location:6.0.0.302”錯誤
