究极大草包 2021-06-15 13:47 采纳率: 40%
浏览 23

如题如题(用C++完成)如题如题

  • 写回答

1条回答 默认 最新

  • qfl_sdu 2021-06-15 15:23
    关注

    代码如下:如有帮助,请采纳一下,谢谢。

    #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;
    }
    评论

报告相同问题?

悬赏问题

  • ¥15 is not in the mmseg::model registry。报错,模型注册表找不到自定义模块。
  • ¥15 安装quartus II18.1时弹出此error,怎么解决?
  • ¥15 keil官网下载psn序列号在哪
  • ¥15 想用adb命令做一个通话软件,播放录音
  • ¥30 Pytorch深度学习服务器跑不通问题解决?
  • ¥15 部分客户订单定位有误的问题
  • ¥15 如何在maya程序中利用python编写领子和褶裥的模型的方法
  • ¥15 Bug traq 数据包 大概什么价
  • ¥15 在anaconda上pytorch和paddle paddle下载报错
  • ¥25 自动填写QQ腾讯文档收集表