c++ extracting double from stream

前端 未结 3 1254
一整个雨季
一整个雨季 2021-01-14 05:35

I\'ve a funny problem with school exercise. I am given latitude and longitude and I have to ensure that it is in right format: \\(\\d+\\.\\d+[NS], \\d\\+.\\d+[EW]\\)

3条回答
  •  感情败类
    2021-01-14 06:11

    You can replace E with X (for example) in the input string, after running your code you can replace back X with E in EW variable.

    char    lBracket, rBracket, comma, NS, EW;
    int     nLat, nLon;
    double  lat, lon;
    
    std::string inputString = "(51.5N, 0.0E)";
    size_t e_pos = inputString.find( 'E' );
    if ( e_pos != std::string::npos ) {
        inputString.replace( e_pos, e_pos + 1, 'X' );
    }
    istringstream iss ( inputString );
    
    iss >> fixed >> lBracket >> nLat >> lat >> NS >> comma >> 
                       nLon >> lon >> EW >> rBracket;
    
    if ( EW == 'X' ) {
        EW = 'E';
    }
    
    lat += nLat;
    lon += nLon;
    

    Update: Sorry, didn't see your remark

    Only solution I came up with is replace the E with some other letter, which would probably work, but isn't really nice piece of code.

    If you want - I'll delete an answer

提交回复
热议问题