编写一个程序,获取100名学生的数据并显示成绩表
write a program that get data of 100 students and display merit list
最近我在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);
}
你可以在这里试试。
当然,这里的一切都是虚拟的,这段代码的目的是向您展示如何处理对象集合。
相关文章:
- 从数据库实时显示QT c++中的数据
- 当使用比格式支持的精度更高的精度来显示数字时,会写出什么数据
- C++射线示踪剂ppm表示没有足够的数据来显示图像
- QChart在使用QDateTimeAxis时不显示任何系列数据
- 如何存储用户输入的所有数据,然后在他们想要查看所有数据时显示它们
- 访问和打印元组中的数据,并使用 C++14 使用模板函数显示数据
- 运算符重载以显示用户定义的数据类型
- 如何在不使用委托的 QML 中显示模型数据
- 从文本文件中读取并输入到数组结构中,然后显示读取的数据C++
- 如何从amd gpus的显示输出中获取图像数据
- 它不显示数据,只显示永无止境的循环
- C++ 矢量不显示包含对象的数据
- 如何在数据文件中查找和显示内容
- Qt - 将空指针(原始数据)转换为 QImage 并将其显示在标签上
- 在QT C++编辑Qtable模型数据时,我需要得到一个小盒子,我需要显示编辑文本,这怎么可能?
- 通过显示一行星号来创建条形图,该条形图将读取数据的输入文件
- 如果构造函数和析构函数可以获取和显示(打印)数据,为什么我们需要 getter 和 setter?
- 在网格视图中显示数据 - 基于 MFC c++ 对话框的应用程序
- 使 XML 数据显示在 Blackberry 10 Cascades QML 中的 listItemComponents
- 提示用户键入 0 到 99 之间的 20 个整数.从该数据显示质数