مرتب سازی اطلاعات دانشجو

سوال

اطلاعات ۱۵ دانشجو را بگیرد(اسم و فامیل و معدل)اسامی به صورت صعودی مرتب شود(به ترتیب فامیل و اسم و معدل در یک خط) و مینگین کل کلاس رو چاپ کند و سپس یک اسم از ورودی دریافت کند و اگر در در لیست بود اطلاعاتش رو چاپ کند و اگر نبود هم چاپ کند نیست

در حال بررسی 0
sms2 10 ماه 1 پاسخ 171 دیده شده 0

پاسخ ( ۱ )

  1. #include
    #include
    #include
    #include

    using namespace std;

    struct Student {
    string first_name;
    string last_name;
    double gpa;
    };

    bool compare_students(const Student& s1, const Student& s2) {
    if (s1.last_name != s2.last_name) {
    return s1.last_name < s2.last_name;
    } else if (s1.first_name != s2.first_name) {
    return s1.first_name < s2.first_name;
    } else {
    return s1.gpa < s2.gpa;
    }
    }

    int main() {
    // Get information for 15 students
    vector students;
    for (int i = 0; i < 15; i++) {
    Student student;
    cout << "Enter first name for student " << i + 1 <> student.first_name;
    cout << "Enter last name for student " << i + 1 <> student.last_name;
    cout << "Enter GPA for student " << i + 1 <> student.gpa;
    students.push_back(student);
    }

    // Sort the students by last name, then first name, then GPA
    sort(students.begin(), students.end(), compare_students);

    // Print the sorted list of students
    cout << "Sorted list of students:n";
    for (const auto& student : students) {
    cout << student.last_name << ", " << student.first_name << " – GPA: " << student.gpa << endl;
    }

    // Calculate the class average GPA
    double total_gpa = 0.0;
    for (const auto& student : students) {
    total_gpa += student.gpa;
    }
    double class_average_gpa = total_gpa / students.size();
    cout << "Class average GPA: " << class_average_gpa << endl;

    // Get a name from the user and search for the student in the list
    string search_name;
    cout <> search_name;
    auto it = find_if(students.begin(), students.end(), [&](const Student& s) {
    return s.first_name == search_name || s.last_name == search_name;
    });
    if (it != students.end()) {
    cout << "Found student: " <last_name << ", " <first_name << " – GPA: " <gpa << endl;
    } else {
    cout << "Student not found.n";
    }

    return 0;
    }

ارسال یک پاسخ