C ++如何获取class中的名称,使其成为基于我输入时间的排名系统
C++ how can i get the name in class to make it as a ranking system based on the time I input
我可以将我的对象存储在一个 class 变量中以便打印排名名称吗?
问题是编写一个程序,询问五名跑步者的姓名以及他们每人完成一场比赛所花费的时间。该程序应显示谁排在第一、第二和第三位。次数只接受正数。
我应该做哪些修改?
#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;
}
是的,你可以。
您可以编写自定义比较函数:
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 << ',';
}
}
您可以使用 std::multiset
简化您的程序,如图 below:
#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;
}
注意使用multiset
而不是set
,因为不止一个跑步者可以有相同的时间。
我可以将我的对象存储在一个 class 变量中以便打印排名名称吗?
问题是编写一个程序,询问五名跑步者的姓名以及他们每人完成一场比赛所花费的时间。该程序应显示谁排在第一、第二和第三位。次数只接受正数。
我应该做哪些修改?
#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;
}
是的,你可以。
您可以编写自定义比较函数:
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 << ',';
}
}
您可以使用 std::multiset
简化您的程序,如图 below:
#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;
}
注意使用multiset
而不是set
,因为不止一个跑步者可以有相同的时间。