C++文件输入输出(5)--读的方式打开放回最大和最小值和平均值

#include<iostream>
#include<fstream>
using namespace std;
int  main()
{
	char name[30],s[80];
	int number,score;
	int n=0,max,min,total=0;
	double aver;
	ifstream instuf("F:\\students.txt",ios::in);
	instuf.seekg(0,ios::beg);
	if(!instuf)
	{
		cerr<<"File could not be open."<<endl;
		abort();
	}
	instuf.getline(s,80);
	while(instuf>>number>>name>>score)
	{
		cout<<number<<' '<<name<<' '<<score<<endl;
		if(n==0) max=min=score;
		else{
			if(score>max) max=score;
			if(score<min) min=score;
		}
		total+=score;n++;
	}
	aver=double(total)/n;
	cout<<"maximal:"<<max<<'\n'<<"minimal:"<<min<<"\naverage:"<<aver<<'\n';
	instuf.close();
 } 
发布了122 篇原创文章 · 获赞 14 · 访问量 6164

猜你喜欢

转载自blog.csdn.net/weixin_44001521/article/details/103973015