c++实现查询天气预报

来源:互联网 发布:怎么给淘宝客服发链接? 编辑:程序博客网 时间:2024/05/03 22:31

用到的函数、API等

1、中央气象台API返回的JSON数据(http://m.weather.com.cn/data/101010100.html)

2、外网获取IP(http://ip.dnsexit.com/index.php)

3、Sqlite(1中的城市代号利用sqlite3实现查询)

4、C++中GB2312字符串和UTF-8之间的转换(见这篇文章http://blog.csdn.net/lgh1992314/article/details/8579206)

5、Jsoncpp(主要是处理1中的数据==见这篇文章http://blog.csdn.net/lgh1992314/article/details/8582179)

...

程序依赖于数据库中的城市以及城市代码的丰富度,所以有的城市可能会查询不到哦。

半成品:

[cpp] view plaincopy
  1. #include <iostream>  
  2. #include <ctime>  
  3. #include <string>  
  4. #include <cstring>  
  5. #include <cstdlib>  
  6. #include <sstream>  
  7. #include <fstream>  
  8. #include <sqlite3.h>  
  9. #include <afxinet.h>  
  10. #include <windows.h>  
  11. #include<json.h>  
  12. #pragma comment(lib,"sqlite3.lib")  
  13. using namespace Json;  
  14. using namespace std;  
  15. std::string City_code;  
  16.   
  17. int sqlite3_exec_callback(void *data, int n_columns, char **col_values, char **col_names)  
  18. {  
  19.     //int i;  
  20.     /*for(i=0; i < n_columns; i++) 
  21.     { 
  22.         cout << col_values[i] << "\t"; 
  23.     } 
  24.     cout << endl;*/  
  25.     City_code = col_values[0];  
  26.     return 0;  
  27. }  
  28.   
  29. std::string UtfToGb(std::string utf8)//UTF-8 to GB2312  
  30. {  
  31.     int len = MultiByteToWideChar(CP_UTF8, 0, utf8.c_str(), -1, NULL, 0);  
  32.     wchar_t* wstr = new wchar_t[len+1];  
  33.     memset(wstr, 0, len+1);  
  34.     MultiByteToWideChar(CP_UTF8, 0, utf8.c_str(), -1, wstr, len);  
  35.     len = WideCharToMultiByte(CP_ACP, 0, wstr, -1, NULL, 0, NULL, NULL);  
  36.     char* str = new char[len+1];  
  37.     memset(str, 0, len+1);  
  38.     WideCharToMultiByte(CP_ACP, 0, wstr, -1, str, len, NULL, NULL);  
  39.     if(wstr) delete[] wstr;  
  40.     std::string strs = (str);  
  41.     delete []str;  
  42.     return strs;  
  43. }  
  44.   
  45. void jsonDate(string strValue) //Jsoncpp  
  46. {  
  47.   
  48.     //Json::StyledWriter style_write;  
  49.     Json::Value value;  
  50.     Json::Reader reader;  
  51.     reader.parse(strValue, value);  
  52.     //std::cout << style_write.write(value) << std::endl;  
  53.     std::cout << value["weatherinfo"]["date_y"].asString() << " ";  
  54.     std::cout << value["weatherinfo"]["week"].asString() << std::endl;  
  55.     std::cout << "================================================" << std::endl;  
  56.   
  57.     std::cout << "=   " << value["weatherinfo"]["temp1"].asString() << "  " << "  "  
  58.               << value["weatherinfo"]["weather1"].asString() << value["weatherinfo"]["wind1"].asString() << std::endl;  
  59.   
  60.     std::cout << "=   " << value["weatherinfo"]["temp2"].asString() << "  "   
  61.               << value["weatherinfo"]["weather2"].asString() << "  " << value["weatherinfo"]["wind2"].asString() << std::endl;  
  62.   
  63.     std::cout << "=   " << value["weatherinfo"]["temp3"].asString() << "  "   
  64.               << value["weatherinfo"]["weather3"].asString() << "  " << value["weatherinfo"]["wind3"].asString() << std::endl;  
  65.   
  66.     std::cout << "=   " << value["weatherinfo"]["temp4"].asString() << "  "  
  67.               << value["weatherinfo"]["weather4"].asString() << "  " << value["weatherinfo"]["wind4"].asString() << std::endl;  
  68.   
  69.     std::cout << "=   " << value["weatherinfo"]["temp5"].asString() << "  "  
  70.               << value["weatherinfo"]["weather5"].asString() << "  " << value["weatherinfo"]["wind5"].asString() << std::endl;  
  71.   
  72.     std::cout << "=   " << value["weatherinfo"]["temp6"].asString() << "  "  
  73.               << value["weatherinfo"]["weather6"].asString() << "  " << value["weatherinfo"]["wind6"].asString() << std::endl;  
  74.   
  75.     std::cout << "================================================" << std::endl;  
  76. }  
  77.   
  78. std::string GetIp()  
  79. {  
  80.     CString url = "http://ip.dnsexit.com/";  
  81.     CString content;  
  82.     CString data;  
  83.     DWORD dwStatusCode;  
  84.     CInternetSession session("HttpClient");  
  85.   
  86.     CHttpFile* pfile = (CHttpFile *)session.OpenURL(url);  
  87.     pfile -> QueryInfoStatusCode(dwStatusCode);  
  88.     if(dwStatusCode == HTTP_STATUS_OK)  
  89.     {   
  90.         while (pfile -> ReadString(data))  
  91.         {  
  92.             content  += data;  
  93.         }  
  94.     }  
  95.     pfile -> Close();  
  96.     delete pfile;  
  97.     session.Close();  
  98.     return std::string(content);  
  99. }  
  100.   
  101. std::string GetProvince(std::string ip)  
  102. {  
  103.     std::string url = "http://www.youdao.com/smartresult-xml/search.s?type=ip&q=";  
  104.     url.append(ip);  
  105.     CString content;  
  106.     CString data;  
  107.     DWORD dwStatusCode;  
  108.     CInternetSession session("HttpClient");  
  109.   
  110.     CHttpFile* pfile = (CHttpFile *)session.OpenURL(url.c_str());  
  111.     pfile -> QueryInfoStatusCode(dwStatusCode);  
  112.     if(dwStatusCode == HTTP_STATUS_OK)  
  113.     {   
  114.         while (pfile -> ReadString(data))  
  115.         {  
  116.             content  += data;  
  117.         }  
  118.     }  
  119.     pfile -> Close();  
  120.     delete pfile;  
  121.     session.Close();  
  122.     std::string result = std::string(content);  
  123.     int left = result.find("<location>");  
  124.     int right = result.find("</location>");  
  125.     //cout << left << "\t" << right << endl;  
  126.     std::string province = result.substr(left + 10,right - left - 10 - 5);  
  127.     return province;  
  128. }  
  129.   
  130. std::string GetWeather(std::string city_code)  
  131. {  
  132.     std::string url = "http://m.weather.com.cn/data/";  
  133.     url.append(city_code);  
  134.     url.append(".html");  
  135.     CString content;  
  136.     CString data;  
  137.     DWORD dwStatusCode;  
  138.     CInternetSession session("HttpClient");  
  139.     CHttpFile* pfile = (CHttpFile *)session.OpenURL(url.c_str());  
  140.     pfile -> QueryInfoStatusCode(dwStatusCode);  
  141.     if(dwStatusCode == HTTP_STATUS_OK)  
  142.     {   
  143.         while (pfile -> ReadString(data))  
  144.         {  
  145.             content  += data;  
  146.         }  
  147.     }  
  148.     pfile -> Close();  
  149.     delete pfile;  
  150.     session.Close();  
  151.     return UtfToGb(std::string(content));  
  152. }  
  153.   
  154. void ShowWeather()  
  155. {  
  156.     sqlite3 *db = NULL;  
  157.     char *err_msg = NULL;  
  158.     if(SQLITE_OK != sqlite3_open("weather.db",&db))  
  159.     {  
  160.         std::cout << "can't open the database." << std::endl;  
  161.         exit(-1);  
  162.     }  
  163.   
  164.     std::string province;  
  165.     std::string ip = GetIp();  
  166.     std::cout << "IP:" << ip << std::endl;  
  167.     province = GetProvince(ip);  
  168.     std::cout << "您所在的地方:" << province << std::endl;  
  169.     std::cout << "您所在地天气:" << std::endl;  
  170.     std::string city = province.substr(province.find("省") + 2,4);  
  171.     std::string sql = "select city_code from weather where city = ";  
  172.     sql.append("\"");  
  173.     sql.append(city);  
  174.     sql.append("\"");  
  175.     sqlite3_exec(db, sql.c_str(), &sqlite3_exec_callback, 0, &err_msg);  
  176.     string strValue = GetWeather(City_code);  
  177.     jsonDate(strValue);  
  178.     City_code.clear();  
  179.     sqlite3_close(db);  
  180.       
  181. }  
  182.   
  183. int main()  
  184. {  
  185.     ShowWeather();  
  186.     sqlite3 *db = NULL;  
  187.     char *err_msg = NULL;  
  188.     if(SQLITE_OK != sqlite3_open("weather.db",&db))  
  189.     {  
  190.         std::cout << "can't open the database." << std::endl;  
  191.         exit(-1);  
  192.     }  
  193.   
  194.     std::string province;  
  195.     std::string sql = "select city_code from weather where city = ";  
  196.     std::cout << "please input the province" << std::endl;  
  197.     std::cin >> province;  
  198.     sql.append("\"");  
  199.     sql.append(province);  
  200.     sql.append("\"");  
  201.     std::cout << "天气:" << std::endl;  
  202.     sqlite3_exec(db, sql.c_str(), &sqlite3_exec_callback, 0, &err_msg);  
  203.     string strValue = GetWeather(City_code);  
  204.     jsonDate(strValue);  
  205.     sqlite3_close(db);  
  206.     return 0;  
  207. }  

输出:
[plain] view plaincopy
  1. IP:124.135.247.252  
  2. 您所在的地方:山东省聊城市  
  3. 您所在地天气:  
  4. 2013年2月16日 星期六  
  5. ================================================  
  6. =   7℃~0℃    多云转雨夹雪南风3-4级转小于3级  
  7. =   4℃~-3℃  阴转小雪  南风小于3级转北风4-5级  
  8. =   1℃~-7℃  阴转多云  北风3-4级转小于3级  
  9. =   3℃~-4℃  晴  南风小于3级  
  10. =   5℃~-4℃  晴  北风小于3级  
  11. =   7℃~-2℃  晴  南风小于3级  
  12. ================================================  
  13. please input the province  
  14.   
  15. 北京  
  16. 天气:  
  17. 2013年2月16日 星期六  
  18. ================================================  
  19. =   2℃~-3℃    阴转多云微风  
  20. =   6℃~-4℃  多云转晴  北风3-4级  
  21. =   3℃~-8℃  晴  北风3-4级转微风  
  22. =   3℃~-6℃  晴  微风  
  23. =   4℃~-6℃  晴转多云  微风  
  24. =   4℃~-5℃  多云转晴  微风  
  25. ================================================  
  26. 请按任意键继续. . .  


原创粉丝点击