@Mahdi
2018-02-03T00:04:41.000000Z
字数 2362
阅读 684
C
#include<stdio.h>
#include<windows.h>
void strTonum( );
void Tran(double gps);
int main()
{
while(1)
{
}
return 0;
}
void strTonum()
{
//116.575594,40.052042
//116°34'32.1384"
char str[22];
printf("GPS:\n\n");
scanf("%s",&str);
//经度
char longitude_str[12] = {0};
//纬度
char latitude_str[11] = {0};
double longitude_num = 0;
double latitude_num = 0;
snprintf(longitude_str,11,str);
strncpy(latitude_str,str+11,9);
//字符串转数字
longitude_num = atof(longitude_str);
latitude_num = atof(latitude_str);
}
void Tran(double gps)
{
int du = (int)(gps);
int fen = (int)((gps - du)*60);
float miao = ((gps - du)*60 - fen)*60;
char gsp_str[30] = {0};
int j = 0;
j = sprintf(gsp_str,"%d,",du);
j+= sprintf(gsp_str+j,"%d,",fen);
j+= sprintf(gsp_str+j,"%f",miao);
printf("\n");
printf("%s\n\n",gsp_str);
// printf("\n");
}
C语言中没有从某位置开始指定长度子字符串的函数(还是我没找到?),只有从头开始截取的strncat和strncpy,
以strncpy为例:
char dest[4] = {""};
char src[] = {"123456789"};
strncpy(dest, src, 3);
puts(dest);
输出结果为 123
看到了吗,strcpy函数中的参数是字符串数组的名字,而数组名本质上是指针,那么,src+3 就可以实现将 src中从第4个字符开始复制n个字符给 dest 了
char dest[4] = {""};
char src[] = {"123456789"};
strncpy(dest, src+3, 3);
puts(dest);
输出结果为 456
http://blog.csdn.net/u011317840/article/details/47808235
// crt_sprintf.c
// compile with: /W3
// This program uses sprintf to format various
//data and place them in the string named buffer.
// 程序使用sprintf 将各种数据格式化后置于字符数组buffer中
#include <stdio.h>
int main( void )
{
char buffer[200], s[] = "computer", c = 'l';
int i = 35, j;
float fp = 1.7320534f;
// 格式化并打印各种数据到buffer
j = sprintf( buffer, "String: %s\n", s ); // C4996
j += sprintf( buffer + j, " Character: %c\n", c ); // C4996
j += sprintf( buffer + j, " Integer: %d\n", i ); // C4996
j += sprintf( buffer + j, " Real: %f\n", fp );// C4996
printf( "%s",buffer);
}
#include<stdio.h>
#include<windows.h>
void strTonum( );
void Tran(double gps);
double * longitude_num;
double * latitude_num;
int main()
{
while(1)
{
strTonum(str_tmp);
Tran(*longitude_num);
Tran(*latitude_num);
}
return 0;
}
void strTonum(char *str)
{
//116.575594,40.052042
//116°34'32.1384"
char str_tmp[22];
printf("GPS:\n\n");
scanf("%s",&str_tmp);
//经度
char longitude_str[12] = {0};
//纬度
char latitude_str[11] = {0};
snprintf(longitude_str,11,str);
strncpy(latitude_str,str+11,9);
//字符串转数字
double temp_f1 = 0, temp_f2 = 0;
temp_f1 = atof(longitude_str);
longitude_num = &temp_f1;
temp_f2 = atof(latitude_str);
latitude_num = &temp_f2;
}
void Tran(double gps)
{
int du = (int)(gps);
int fen = (int)((gps - du)*60);
float miao = ((gps - du)*60 - fen)*60;
char gsp_str[30] = {0};
int j = 0;
j = sprintf(gsp_str,"%d,",du);
j+= sprintf(gsp_str+j,"%d,",fen);
j+= sprintf(gsp_str+j,"%f",miao);
printf("\n");
printf("%s\n\n",gsp_str);
}