/* 功能 C++解析GPS数据并显示 时间 2016-04-22*/
#include<fstream>
#include<string>
#include<iostream>
#include<vector>
#include<sstream>
using namespace std;
//数据类型转换模板函数
template <class Type>
Type stringToNum(const string str)
{
istringstream iss(str);
Type num;
iss >> num;
return num;
}
int main()
{
ifstream file("实验一GNSS语句.TXT");
string line;
getline(file, line);
//读取第一行 GGA格式 $GPGGA,025620.00,2602.33721,N,11911.49176,E,2,04,1.63,13.5,M,9.9,M,,0000*5D
//GGA字段含义 语句ID,UTC时间,纬度,N/S,经度,E/W,GPS状态,卫星数量,精度因子,海拔 。。。
vector<string> arr1; //定义一个字符串容器
int position = 0;
do
{
string tmp_s; position = line.find(","); //找到逗号的位置
tmp_s = line.substr(0,position); //截取需要的字符串
line.erase(0,position+1); //将已读取的数据删去
arr1.push_back(tmp_s); //将字符串压入容器中
}while(position != -1);
position = 0;
getline(file,line);
//读取第二行 RMC格式 $GPRMC,025620.20,A,2602.33722,N,11911.49176,E,0.100,,281211,,,D*79
//RMC字段含义 语句ID,UTC时间,GPS状态,纬度,N/S,经度,E/W,速度(节),,UTC日期,,,校验值
vector<string> arr2;
do
{
string tmp_s;
position = line.find(",");
tmp_s = line.substr(0,position);
line.erase(0,position+1);
arr2.push_back(tmp_s);
}while(position != -1);
file.close();
//构造日期时间字符串 YYYY-MM-DD HH:MM:SS
string datetime = "20"+arr2[9].substr(4,2)+"-"+arr2[9].substr(2,2)+"-"+arr2[9].substr(0,2)+" "+arr1[1].substr(0,2)+
":"+arr1[1].substr(2,2)+":"+arr1[1].substr(4,2);
cout<<"UTC时间:"<<datetime<<endl;
double d0 = stringToNum<double>(arr1[2]);
cout<<"经度:";
printf("%lf\n",d0/100);
//此处不能使用cout,否则输出精度将受到影响
double d1 = stringToNum<double>(arr1[4]);
cout<<"纬度:";
printf("%lf\n",d1/100);
cout<<"海拔:"<<arr1[9]<<arr1[10]<<endl;
return 0;
}