[关闭]
@Mahdi 2018-02-03T00:04:41.000000Z 字数 2362 阅读 684

源码备份

C


  1. #include<stdio.h>
  2. #include<windows.h>
  3. void strTonum( );
  4. void Tran(double gps);
  5. int main()
  6. {
  7. while(1)
  8. {
  9. }
  10. return 0;
  11. }
  12. void strTonum()
  13. {
  14. //116.575594,40.052042
  15. //116°34'32.1384"
  16. char str[22];
  17. printf("GPS:\n\n");
  18. scanf("%s",&str);
  19. //经度
  20. char longitude_str[12] = {0};
  21. //纬度
  22. char latitude_str[11] = {0};
  23. double longitude_num = 0;
  24. double latitude_num = 0;
  25. snprintf(longitude_str,11,str);
  26. strncpy(latitude_str,str+11,9);
  27. //字符串转数字
  28. longitude_num = atof(longitude_str);
  29. latitude_num = atof(latitude_str);
  30. }
  31. void Tran(double gps)
  32. {
  33. int du = (int)(gps);
  34. int fen = (int)((gps - du)*60);
  35. float miao = ((gps - du)*60 - fen)*60;
  36. char gsp_str[30] = {0};
  37. int j = 0;
  38. j = sprintf(gsp_str,"%d,",du);
  39. j+= sprintf(gsp_str+j,"%d,",fen);
  40. j+= sprintf(gsp_str+j,"%f",miao);
  41. printf("\n");
  42. printf("%s\n\n",gsp_str);
  43. // printf("\n");
  44. }
  1. C语言中没有从某位置开始指定长度子字符串的函数(还是我没找到?),只有从头开始截取的strncatstrncpy
  2. strncpy为例:
  3. char dest[4] = {""};
  4. char src[] = {"123456789"};
  5. strncpy(dest, src, 3);
  6. puts(dest);
  7. 输出结果为 123
  8. 看到了吗,strcpy函数中的参数是字符串数组的名字,而数组名本质上是指针,那么,src+3 就可以实现将 src中从第4个字符开始复制n个字符给 dest
  9. char dest[4] = {""};
  10. char src[] = {"123456789"};
  11. strncpy(dest, src+3, 3);
  12. puts(dest);
  13. 输出结果为 456
  1. http://blog.csdn.net/u011317840/article/details/47808235
  2. // crt_sprintf.c
  3. // compile with: /W3
  4. // This program uses sprintf to format various
  5. //data and place them in the string named buffer.
  6. // 程序使用sprintf 将各种数据格式化后置于字符数组buffer中
  7. #include <stdio.h>
  8. int main( void )
  9. {
  10. char buffer[200], s[] = "computer", c = 'l';
  11. int i = 35, j;
  12. float fp = 1.7320534f;
  13. // 格式化并打印各种数据到buffer
  14. j = sprintf( buffer, "String: %s\n", s ); // C4996
  15. j += sprintf( buffer + j, " Character: %c\n", c ); // C4996
  16. j += sprintf( buffer + j, " Integer: %d\n", i ); // C4996
  17. j += sprintf( buffer + j, " Real: %f\n", fp );// C4996
  18. printf( "%s",buffer);
  19. }
  1. #include<stdio.h>
  2. #include<windows.h>
  3. void strTonum( );
  4. void Tran(double gps);
  5. double * longitude_num;
  6. double * latitude_num;
  7. int main()
  8. {
  9. while(1)
  10. {
  11. strTonum(str_tmp);
  12. Tran(*longitude_num);
  13. Tran(*latitude_num);
  14. }
  15. return 0;
  16. }
  17. void strTonum(char *str)
  18. {
  19. //116.575594,40.052042
  20. //116°34'32.1384"
  21. char str_tmp[22];
  22. printf("GPS:\n\n");
  23. scanf("%s",&str_tmp);
  24. //经度
  25. char longitude_str[12] = {0};
  26. //纬度
  27. char latitude_str[11] = {0};
  28. snprintf(longitude_str,11,str);
  29. strncpy(latitude_str,str+11,9);
  30. //字符串转数字
  31. double temp_f1 = 0, temp_f2 = 0;
  32. temp_f1 = atof(longitude_str);
  33. longitude_num = &temp_f1;
  34. temp_f2 = atof(latitude_str);
  35. latitude_num = &temp_f2;
  36. }
  37. void Tran(double gps)
  38. {
  39. int du = (int)(gps);
  40. int fen = (int)((gps - du)*60);
  41. float miao = ((gps - du)*60 - fen)*60;
  42. char gsp_str[30] = {0};
  43. int j = 0;
  44. j = sprintf(gsp_str,"%d,",du);
  45. j+= sprintf(gsp_str+j,"%d,",fen);
  46. j+= sprintf(gsp_str+j,"%f",miao);
  47. printf("\n");
  48. printf("%s\n\n",gsp_str);
  49. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注