C++ STL 第九次实验

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_39776901/article/details/78716772

【题目】
  处理score.txt中的学生成绩信息
  并且指出及格率,
  得分87~100之间的学生比例

【要求】
  使用multiset存储对象数据
  使用2分查找函数处理检索要求:
  如:lower_bound, up_bound和equal_range

【代码】

#include <iostream>
#include <set>
#include <fstream>
#include <sstream>

using namespace std;

class Score{
public:
    int s_no;
    int s_score;
};

int main()
{
    ifstream in;
    in.open("score.txt");
    char buf[100];
    int no;
    int score;
    multiset<int> ms;
    int i = 0;  //学生总人数
    int j = 0;  //及格人数
    float k = 0; //及格率
    int l = 0;  //87~100人数
    float m = 0; //优秀率

    while(in.getline(buf,100)){
        istringstream data(buf);

        data >> no;
        data >> score;

        ms.insert(score);
    }

    for(multiset<int>::iterator it = ms.begin();it!=ms.end();it++)
    {
        i++;
    }
    cout << "总共有" << i << "个学生" << endl;

    multiset<int>::iterator ite = ms.lower_bound(60);

    while(ite!=ms.end())
    {
        j++;
        ite++;
    }
    k = (float)j/i;
    cout << "总共有" << j << "个学生及格" << endl;
    cout << "及格率为:" << k << endl;

    multiset<int>::iterator iter = ms.lower_bound(87);

    while(iter!=ms.end())
    {
        l++;
        iter++;
    }
    m = (float)l/i;
    cout << "87~100之间的人数为" << l << endl;
    cout << "优秀率为" << m;

    return 0;
}

【结果】
这里写图片描述

猜你喜欢

转载自blog.csdn.net/qq_39776901/article/details/78716772