I have to make a function that converts a char* string into a float. float sexag
ID: 3865681 • Letter: I
Question
I have to make a function that converts a char* string into a float.
float sexag2decimal(char *degreeString);//prototype
for example i have to pass in "apd->latitude" which equals "28-26-08.0210N" and have it come out as a float in this format "DD-MM-SS.MASD"
full instructions below:
1.3.1 Latitude/Longitude Input The latitude and longitude are both degrees, expressed as shown in the tables below Table 2: Degrees Value 180 0-59 Placeholder Name Decimal 0-180 Degrees Minutes Seconds.MilliArcSeconds 0-59.0-9999 Direction SS.MAS N.S.E,W See Table Table 3: Direction Name Decimal Sign Unit Latitude Longitude The conversion of the DDD-MM-SS.MASD string is shown in Table 2 The for- mula to convert a sexagesimal degree measurement to a digital degree measurement is shown below decimal Note that the is derived from the information in Table B above 1.4 Functions 1.4.1 float sexag2decimal(char *degreeString):; Description: Convert the sexagesimal input string of chars to a decimal degree based on the formula in Tables 2and Special Cases: If a NULL pointer is passed to this function, simply return 0.0. Sim ilarly, if the DD-MM-SS.MASD fields have invalid or out-of-range data, return 0.0 Caveat: Even though the valid range of Degrees is from 0 to 180, the data files for the Continental US and Florida are from 0 to 99. Make sure that the conversion can handle all valid cases correctlyExplanation / Answer
Below is your function.. Let me know if you have any issue in comments.
float sexag2decimal(char *degreeString)
{
int Degrees, Mins, Secs, MAS;
char D;
float decDegree;
//convert the sexagesimal input string of chars to a decimal degree based on the formula.
if(degreeString == NULL)
{
return 0.0;
}
else
{
if(sscanf(degreeString, "%d-%d-%d.%d%c", &Degrees, &Mins, &Secs, &MAS, &D) != 5)
{
return 0.0;
}
if(Degrees < 0 || Degrees > 180 || Mins < 0 || Mins > 59 || Secs < 0|| Secs > 59|| MAS < 0 || MAS > 9999 || (D != 'S' && D != 'N' && D != 'E' && D != 'W'))
{
return 0.0;
}
decDegree = (float)Degrees +(float)Mins + (float)(Mins/(float)60.0) + ((float)55/(float)60.0 * 60.0);
if( D == 'W' || D == 'S')
{
decDegree= -decDegree;
}
return decDegree;
}
}