看了boost庫的正則表達式後寫的。。。
代碼:
#include <string>
#include <boost/regex.hpp>
#include <iostream>
#include <fstream>
#include <windows.h>
class weather
{
public:
weather(std::string sprovince="guangdong",std::string sposition="guangzhou"):_province(sprovince),_position(sposition) {}
void flash();
void clear() { _date.clear();_weather.clear();_tem.clear();_wind.clear();}
std::string get_day(int day);
const std::string& get_date(int day=0) {return _date[day];}
const std::string& get_weather(int day=0) {return _weather[day];}
const std::string& get_temperature(int day=0) {return _tem[day];}
const std::string& get_wind(int day=0) {return _wind[day];}
friend std::ostream& operator<<(std::ostream& out,weather& wea);
void change_city(std::string s) {_position=s;}
void change_province(std::string s) {_province=s;}
void change(std::string sprovince,std::string sposition) {change_city(sprovince);change_province(sposition);}
const std::string& get_city() {return _position;}
const std::string& get_province() {return _province;}
private:
std::vector<std::string>_date;
std::vector<std::string>_weather;
std::vector<std::string>_tem;
std::vector<std::string>_wind;
std::string _province;
std::string _position;
void remove_file();
void _downloadfile();
};
void weather::_downloadfile()
{
char buf[200],storage[230],storage2[100]; //分別儲存目錄路徑,文件路徑,獲取天氣的地址
GetCurrentDirectoryA(sizeof(buf),buf);
sprintf_s(storage,"%s\\%s.htm",buf,_position.c_str());
sprintf_s(storage2,"http://qq.ip138.com/weather/%s/%s.htm",_province.c_str(),_position.c_str());
URLDownloadToFileA(0,storage2,storage,0,0);
}
void weather::remove_file()
{
char buf[200],storage[230];
GetCurrentDirectoryA(sizeof(buf),buf);
sprintf_s(storage,"%s\\%s.htm",buf,_position.c_str());
remove(storage);
}
std::ostream& operator<<(std::ostream& out,weather& wea)
{
for(std::vector<std::string>::size_type i=0;i<wea._date.size();++i)
{
out<<wea._date[i].c_str()<<" "<<wea._weather[i].c_str()<<" "<<wea._tem[i].c_str()<<" "<<wea._wind[i].c_str();
if(i!=wea._date.size()-1) out<<std::endl;
}
return out;
}
std::string weather::get_day(int day=0)
{
return _date[day]+" "+_weather[day]+" "+_tem[day]+" "+_wind[day];
}
void weather::flash()
{
clear();
_downloadfile();
boost::regex regdate(";\">(\\d{4}-\\d{1,2}-\\d{1,2} [^<]*)</td>");
boost::regex regweather(" /><br/>([^<]*)</td>");
boost::regex regtem("<td>(\\d{1,2}℃~\\d{1,2}℃)</td>");
boost::regex regwind("<td>([^<0-9]+\\d?.?\\d?\\w*)</td>");
boost::smatch result;
std::string temp;
std::string::const_iterator csbegin;
std::vector<std::string>vecstr;
std::ifstream ifile(_position+".htm");
while(getline(ifile,temp))
{
vecstr.push_back (temp);
}
ifile.close();
for(std::vector<std::string>::const_iterator it=vecstr.begin();it!=vecstr.end();++it)
{
csbegin=it->begin(); //獲取日期
while(regex_search(csbegin,it->end(),result,regdate))
{
_date.push_back(result[1]);
csbegin=result[0].second;
}
csbegin=it->begin(); //獲取天氣
while(regex_search(csbegin,it->end(),result,regweather))
{
_weather.push_back(result[1]);
csbegin=result[0].second;
}
csbegin=it->begin(); //獲取溫度
while(regex_search(csbegin,it->end(),result,regtem))
{
_tem.push_back(result[1]);
csbegin=result[0].second;
}
csbegin=it->begin(); //獲取風力
while(regex_search(csbegin,it->end(),result,regwind))
{
_wind.push_back(result[1]);
csbegin=result[0].second;
}
}
remove_file();
}
調用:
int main()
{
weather myweather,myweather2("guangdong","chaozhou");
myweather.flash(); //刷新天氣
std::cout<<"廣州:"<<std::endl;
std::cout<<myweather<<std::endl<<std::endl; //輸出7天內的天氣
myweather2.flash();
std::cout<<"潮州:"<<std::endl;
std::cout<<myweather2<<std::endl;
system("pause>nul");
}
運行截圖:
(注:UrlDownloadToFile需要鏈接UrlMon.lib坑死了。。添加windows.h就只爲了用這貨。。。)