c++读入文本文件

来源:百度知道 编辑:UC知道 时间:2024/06/12 12:15:01
1. 设计一个学生类student,包括学生学号、姓名、成绩;设计一个友元函数,比较某两个学生成绩的高低;读入一个文本文件(格式如示例studengt.txt,每行的学号、姓名、成绩之间用四个空格隔开)中所有学生的学号、姓名、成绩,输出最高成绩和最低成绩的学生信息(学号、姓名、成绩)

// trystudent.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include "fstream"
#include "iostream"
#include "iomanip"
#include "string"
using namespace std;

class student
{
public:
string no;
string name;
int score;
friend int greater2(student s1,student s2);
friend ostream & operator<<(ostream & out,student &s);
};

ostream &operator<<(ostream & out,student &s)
{
out<<setw(10)<<s.no<<setw(10)<<s.name<<setw(5)<<s.score<<endl;
return out;
}

int greater2(student s1,student s2)
{
return s1.score>s2.score;
}

void readfile(student * stu,int &count)
{
count=0;
ifstream ifs("student.txt");
while(!ifs.eof())
{
ifs>>stu[count].no;
ifs>>stu[cou