代码如下:如有帮助,请采纳一下,谢谢。
#include <iostream>
#include <string>
#include <fstream>
#include <vector>
#include <algorithm>
#include <iomanip>
using namespace std;
struct StDate
{
int year;
int mon;
int day;
int hour;
int mint;
int sec;
friend istream & operator>>( istream & is,StDate & c);
void display()
{
cout << year << " " << mon << " " << day << " " << hour << " " << mint <<" " << sec;
}
};
struct StWaveData
{
struct StDate dt;
double height;
friend istream & operator>>( istream & is,StWaveData & c);
bool operator < (const StWaveData &wv) const
{
return height < wv.height;
}
};
istream & operator>>( istream & is,StDate & c)
{
char buf[50] = {0};
is.getline(buf,50);//读入一行数据
string s = buf;
int startpos = s.find(" ",0);
if(startpos < 0) return is;
string year = s.substr(0,startpos);
int endpos = s.find(" ",startpos+1);
if(endpos < 0) return is;
string mon = s.substr(startpos,endpos - startpos);
startpos = endpos;
endpos = s.find(" ",startpos+1);
if(endpos < 0) return is;
string day = s.substr(startpos,endpos - startpos);
startpos = endpos;
endpos = s.find(" ",startpos+1);
if(endpos < 0) return is;
string hour = s.substr(startpos,endpos - startpos);
startpos = endpos;
endpos = s.find(" ",startpos+1);
if(endpos < 0) return is;
string mint = s.substr(startpos,endpos - startpos);
startpos = endpos;
endpos = s.length();
if(endpos < 0) return is;
string sec = s.substr(startpos,endpos - startpos);
c.year = atoi(year.c_str());
c.mon = atoi(mon.c_str());
c.day = atoi(day.c_str());
c.hour = atoi(hour.c_str());
c.mint = atoi(mint.c_str());
c.sec = atoi(sec.c_str());
return is;
}
istream & operator>>( istream & is,StWaveData & c)
{
char buf[50] = {0};
is.getline(buf,50);//读入一行数据
string s = buf;
int startpos = s.find(" ",0);
if(startpos < 0) return is;
string year = s.substr(0,startpos);
int endpos = s.find(" ",startpos+1);
if(endpos < 0) return is;
string mon = s.substr(startpos,endpos - startpos);
startpos = endpos;
endpos = s.find(" ",startpos+1);
if(endpos < 0) return is;
string day = s.substr(startpos,endpos - startpos);
startpos = endpos;
endpos = s.find(" ",startpos+1);
if(endpos < 0) return is;
string hour = s.substr(startpos,endpos - startpos);
startpos = endpos;
endpos = s.find(" ",startpos+1);
if(endpos < 0) return is;
string mint = s.substr(startpos,endpos - startpos);
startpos = endpos;
endpos = s.find(" ",startpos+1);
if(endpos < 0) return is;
string sec = s.substr(startpos,endpos - startpos);
startpos = endpos;
endpos = s.length();
if(endpos < 0) return is;
string height = s.substr(startpos,endpos - startpos);
c.dt.year = atoi(year.c_str());
c.dt.mon = atoi(mon.c_str());
c.dt.day = atoi(day.c_str());
c.dt.hour = atoi(hour.c_str());
c.dt.mint = atoi(mint.c_str());
c.dt.sec = atoi(sec.c_str());
c.height = atof(height.c_str());
return is;
}
int main()
{
ifstream is("E:\\Code\\CSDN\\tt3\\test.txt");
if (!is.is_open())
{
cout << "文件打开失败" << endl;
return 0;
}
//读取第一行数据
StDate dt;
is >> dt;
//读取后续数据并存放在vector中
vector <StWaveData> vdata;
while(!is.eof())
{
StWaveData wv;
is >> wv;
vdata.push_back(wv);
}
is.close();
StWaveData one = vdata[0];
StWaveData last = vdata[vdata.size() -1];
sort(vdata.begin(),vdata.end());
float sum = 0.0;
int tt = 0;
for (int i = vdata.size()/3*2; i < vdata.size();i++)
{
sum += vdata[i].height;
tt++;
}
float avg = sum / tt;
one.dt.display();cout << endl;
last.dt.display();cout << endl;
cout << fixed << showpoint << setprecision(2) << avg << endl;
return 0;
}