我试着做一个解析gps值($GPRMC)的程序。
首先,成功地从整行解析字符串。
$GPRMC,062513.000,A,3645.9487,N,12716.8382,E,1.76,295.08,160116,,,A*6E
做完之后,我使用函数stod()
表示字符串为double。
但如果我调试它就会崩溃。
这里有密码。
#include<iostream>
#include<string>
using namespace std;
//"$GPRMC,062513.000,A,3645.9487,N,12716.8382,E,1.76,295.08,160116,,,A*6E";
int main()
{
string gps="$GPRMC,062516.000,A,3645.9494,N,12716.8365,E,1.82,302.69,160116,,,A*63";
int com_1;
int com_2;
int com_3;
int com_4;
int com_5;
int com_6;
int com_7;
int com_8;
int com_9;
string kind;
string time;
string state;
string latitude;
string n_s;
string longitude;
string e_w;
string knot;
string degree;
double Kind;
double Time;
double Latitude;
double Longitude;
double Knot;
double Degree;
com_1=gps.find(",");
com_2=gps.find(",",com_1+1);
com_3=gps.find(",",com_2+1);
com_4=gps.find(",",com_3+1);
com_5=gps.find(",",com_4+1);
com_6=gps.find(",",com_5+1);
com_7=gps.find(",",com_6+1);
com_8=gps.find(",",com_7+1);
com_9=gps.find(",",com_8+1);
kind=gps.substr(0,com_1);
time=gps.substr(com_1+1,com_2-com_1-1);
//state=gps.substr(com_2+1,com_3-com_2-1);
latitude=gps.substr(com_3+1,com_4-com_3-1);
//n_s=gps.substr(com_4+1,com_5-com_4-1);
longitude=gps.substr(com_5+1,com_6-com_5-1);
//e_w=gps.substr(com_6+1,com_7-com_6-1);
knot=gps.substr(com_7+1,com_8-com_7-1);
degree=gps.substr(com_8+1,com_9-com_8-1);
Kind=stod(kind);
Time=stod(time);
//State=stod(state);
Latitude=stod(latitude);
//N_s=stod(n_s);
Longitude=stod(longitude);
//E_w=stod(e_w);
Knot=stod(knot);
Degree=stod(degree);
发布于 2016-01-20 16:17:16
在回顾中,让我们考虑如何解析第一个数据。
com_1=gps.find(",");
上面一行找到第一个逗号的位置,很好。
接下来,提取第一个项的子字符串:
kind=gps.substr(0,com_1);
根据您的输入,变量kind
应该是"$GPRMC“。
最后,将此文本转换为双文本:
Kind=stod(kind);
// In other words, this is equivalent to
// Kind = stod("$GPRMC");
stod
函数失败,因为字符串中没有数字。
顺便说一句,按情况不同的变量名,比如kind
和Kind
,被认为是糟糕的编码实践。大多数编码准则都禁止这样做,并要求变量名的不同程度超过了大小写。
发布于 2016-01-20 16:14:44
Kind=stod(kind);
似乎不对。kind
的值将是"$GPRMC"
。您不能从其中提取double
。
PS修复它可能不会修复任何其他问题。
https://stackoverflow.com/questions/34904811
复制相似问题