c++程序来比较年龄,如果5个人,并打印最年长的人的名字

C++ program to compare ages if 5 persons and print the name of the eldest person

本文关键字:最年长 打印 如果 程序 比较 c++ 5个人      更新时间:2023-10-16

我想写一个c++程序,我需要接受5个人的名字,年龄和地址,并显示最年长的人的名字。我能找到最年长的人的年龄,但不知道如何打印与年龄相对应的名字。请看看我的代码,并帮助我如何做到这一点。

我的c++代码是:
#include <iostream>
using namespace std;
struct student{
   char name[20];
   int age;
   char add[40];
};
int main()
{
   student stu[5];
   int i,  max;
for (i = 0; i <=4; i++)
{
    cout << "n Enter name";
    cin >>stu[i].name;
    cout << "n Enter age";
    cin >>stu[i].age;
    cout << "n Enter address";
    cin >>stu[i].add;
}
max = stu[0].age;
for ( i = 0; i <=4; i++)
{
    if (stu[i].age > max)
        max = stu[i].age;
}
    cout << "Max age is: " <<max;
    return 0;
}

我能找到最大年龄。请帮我如何显示年龄最大的人的名字

不存储遇到的最大年龄,而是尝试存储一个指向最大年龄记录的指针。像这样:

student* max = &stu[0];
for ( i = 0; i <=4; i++)
{
    if (stu[i].age > max->age)
        max = &stu[i];
}
std::cout << "Student with max age is " << max->name << std::endl;

您可以使用<algorithm>中的std::max_element来检索指向max元素的迭代器:

auto it = std::max_element(std::begin(stu), std::end(stu),
    [](const student& lhs, const student& rhs) {
        return lhs.age < rhs.age;
});
std::cout << "Oldest is " << it->name << " with " << it->age << std::endl;

生活例子

您可以保留达到最大值的索引,这样您就可以访问该条目中的所有信息。

int maxi = 0;
for ( i = 1; i <=4; i++)
{
    if (stu[i].age > stu[maxi].age)
    {
        maxi = i;
    }
}
cout << "max age is " << stu[maxi].age << " and name is " << stu[maxi].name;

你就快成功了。当你找到年龄的时候,就保留最大的:

student eldest;
for ( i = 0; i <=4; i++)
{
    if (stu[i].age > max)
    {
        max = stu[i].age;
        eldest = stu[i];
    }
}
    cout << "Max age is: " <<max << " named " << eldest.name;
    return 0;
}

添加另一个类型为student的变量maxStudent,当if语句为真时设置该变量。最后,您应该有正确的学生,您可以打印出每个字段。

将max的类型从int改为Student-

student max;
max.age = stu[0].age;
strcpy( stu[0].name, max.name );
strcpy( stu[0].add, max.add );
for ( i = 0; i <=4; i++)
{
    if (stu[i].age > max)
        max.age = stu[i].age;
        strcpy( stu[i].name, max.name );
        strcpy( stu[i].add, max.add );
}

您的代码通常缺乏std::vectorstd::string。除非有充分的理由,否则不应该使用数组。

结构体student应该是:

struct student{
   std::string name;
   int age;
   std::string add;
};

同样,main中的数组应该变成:

std::vector<student> stu;

请注意,您不再需要考虑大小。

接下来的事情是找到最年长的人。c++已经具备了在中找到最伟大的东西的功能。在<algorithm>头文件中,有一个名为std::max_element的函数正是这样做的。它适用于任何具有"小于"特定含义的东西。例如,两个int变量具有"小于"关系,例如在"4小于5"中。

您的student结构体没有这样的自然关系,但是您可以通过提供operator<:

来添加它:
struct student{
   std::string name;
   int age;
   std::string add;
   bool operator<(student const &other) const
   {
      return age < other.age;
   }
};

这将工作:

std::vector<student>::const_iterator eldest_student_iter = std::max_element(stu.begin(), stu.end());
int max_age = eldest_student_iter->age;

现在有人可能会说,这给student结构体本身增加了太多的功能,并且使age比其他两个成员更重要,这在逻辑上可能是不合理的。

幸运的是,std::max_element有第二个版本,它接受第三个形参;所谓的函子,它告诉函数如何比较两个元素。下面是一个例子:

struct StudentComparison
{
   bool operator()(student const &lhs, student const &rhs) const
   {
      return lhs.age < rhs.age;
   }
};
std::vector<student>::const_iterator eldest_student_iter = std::max_element(stu.begin(), stu.end(), StudentComparison());
int max_age = eldest_student_iter->age;

这应该很好地包装在自己的函数中:

int GetHighestAge(std::vector<student> const &stu)
{
   std::vector<student>::const_iterator eldest_student_iter = std::max_element(stu.begin(), stu.end(), StudentComparison());
   int max_age = eldest_student_iter->age;
   return max_age;
}

在实际代码中,由于student的向量为空,所以没有最大元素时,您还必须确定应该发生什么。

int GetHighestAge(std::vector<student> const &stu)
{
   if (stu.empty())
   {
      // error...
   }
   std::vector<student>::const_iterator eldest_student_iter = std::max_element(stu.begin(), stu.end(), StudentComparison());
   int max_age = eldest_student_iter->age;
   return max_age;
}

如果使用c++ 11,代码可以写得更简洁。您不再需要在其他地方定义StudentComparison函子,而是可以使用所谓的lambda直接在调用位置定义比较。您可以使用auto来避免过于冗长的std::vector<student>::const_iterator

   auto eldest_student_iter = std::max_element(stu.begin(), stu.end(), [](student const &lhs, student const &rhs) { return lhs.age < rhs.age; });

最后,这里是一个完整的玩具示例:

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
struct student{
   std::string name;
   int age;
   std::string add;
};
int GetHighestAge(std::vector<student> const &stu)
{
   if (stu.empty())
   {
       return -1;
   }
   auto eldest_student_iter = std::max_element(stu.begin(), stu.end(), [](student const &lhs, student const &rhs) { return lhs.age < rhs.age; });
   int max_age = eldest_student_iter->age;
   return max_age;
}
int main()
{
   std::vector<student> stu;
   // some test data:
   student s1;
   s1.name = "One";
   s1.age = 18;
   s1.add = "Somewhere";
   student s2;
   s2.name = "Two";
   s2.age = 21;
   s2.add = "Somewhere Else";
   student s3;
   s3.name = "Three";
   s3.age = 20;
   s3.add = "Yet Somewhere Else";
   stu.push_back(s1);
   stu.push_back(s2);
   stu.push_back(s3);
   std::cout << "Max age: " << GetHighestAge(stu) << "n";
}

最后一个提示:存储出生日期而不是年龄。出生日期永远不变,年龄过时。

这是您的程序编辑后的版本,希望您喜欢。它显示了最年长的人的所有细节

//从学生的所有详细信息中找到最高年龄

#include < iostream >
using namespace std;
struct student
{
  char name[20];
  int age;
  char add[40];
};
int main()
{
  student stu[5];
  int i, n, max;
  cout << "nenter the no. of record you want to enter : ";
  cin >> n;
  for (i = 0; i < n; i++)
  {
    cout << "n Enter name : ";
    cin >> stu[i].name;
    cout << "n Enter age : ";
    cin >> stu[i].age;
    cout << "n Enter roll : ";
    cin >> stu[i].add;
  }
  max = stu[0].age;
  for (i = 0; i < n; i++)
  {
    if (stu[i].age > max)
      max = stu[i].age;
  }
  cout << "nHighest age report detail -----";
  for (i = 0; i < n; i++)
  {
    if (stu[i].age == max)
    {
      cout << "n your name : " << stu[i].name;
      cout << "n your age : " << stu[i].age;
      cout << "n your roll : " << stu[i].add;
    }
  }
  return 0;
}

这个程序是完全编译的