编写一个程序,获取100名学生的数据并显示成绩表

write a program that get data of 100 students and display merit list

本文关键字:数据 显示 100名 获取 程序 一个      更新时间:2023-10-16

最近我在c++中遇到了一个特殊的问题:

创建一个名为student的类,该类包含以下成员:roll、name&课程编写程序读取n个学生的信息,然后显示在输入滚动范围内的学生的信息。使用构造函数&成员功能。

在没有数据库使用的情况下,如何在输入roll_no范围内显示信息?这听起来很奇怪。

我有一个简单的解决方案,如下所示:但是,我想要一个更好的算法,在内存和处理器速度方面更经济。

 #include<iostream.h>
     #include<conio.h>
     class student
     {
       int roll;
       char name[100];
       char course[100];
       public:void getData();
          void disp(int r1,int r2)
          {
            if(roll>=r1 && roll<=r2)
            cout<<"nroll_no:"<<roll<<"tname:"<<name<<"tcourse:"<<course;
          }
     };
     void student::getData()
     {
        cout<<"nEnter roll_no,name and coursen";
        cin>>roll>>name>>course;
     }
     void main()
     {
       student s[100];
       int n,r1,r2;
       clrscr();
       cout<<"nEnter no of studentsn";
       cin>>n;
       for(int i=0;i<n;i++)
        s[i].getData();
       cout<<"nEnter the rangen";
       cin>>r1>>r2;
       for(int j=0;j<n;j++)
        s[j].disp(r1,r2);
       getch();
     }

如果有讲师,请询问讲师roll_no是自动递增的,还是用户输入的一部分。澄清要求是工作1。

您需要一个容器,例如向量或贴图来容纳所有对象。

  • 输入一个对象并存储到容器中
  • 重复,直到用户表示不再输入为止
  • 如果使用向量,请按roll_no排序
  • 提示用户打印的开始范围和结束范围
  • 在容器中搜索大于或的第一个roll_no等于起始范围
  • 从容器中的该位置打印,直到roll_no大于或等于结束范围

实现留给读者练习。

以下是您练习的解决方案:

#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;
struct Student{
    Student(unsigned int r) : roll(r), name(), course() {}
    unsigned int roll;
    string name;
    string course;
};
int main(){
    vector<Student> students;
    //populate the vector with meaningful data, here only roll is written
    for(unsigned int i = 0; i < 100; i++)
        students.push_back(Student(i));
    unsigned int min = 45;
    unsigned int max = 85;
    auto operation = [=](const Student& e){if( (e.roll >= min) and (e.roll <= max) ) cout << e.roll << endl;};
    for_each(students.begin(), students.end(), operation);
}

你可以在这里试试。

当然,这里的一切都是虚拟的,这段代码的目的是向您展示如何处理对象集合。