字符串中包含多个空格,如下:
0.86527033 0.50127762 0.00529092 -0.50130544 0.86521534 0.00976016 0.00031476 -0.01109755 0.99993837 6.81169042 3.27070358 -0.13535404要把每个数分离出来,我现在是想着逐个判断空格的位置,然后分离出每个数字,遇到的小问题是怎么判定每个空格的位置???哪位大侠若有更好的分离方法,还望赐教!!!
0.86527033 0.50127762 0.00529092 -0.50130544 0.86521534 0.00976016 0.00031476 -0.01109755 0.99993837 6.81169042 3.27070358 -0.13535404要把每个数分离出来,我现在是想着逐个判断空格的位置,然后分离出每个数字,遇到的小问题是怎么判定每个空格的位置???哪位大侠若有更好的分离方法,还望赐教!!!
空格读出来就是0 你这句什么意思啊?
比如我想得到 第六个数:0.00976016,怎么判定空格位置啊?
hezhe1008:
空格读出来就是0 你这句什么意思啊?
比如我想得到 第六个数:0.00976016,怎么判定空格位置啊?
最好是没找到一个,就把它从所有字符串中截取出来,这样Find的时候永远查找到第一个,节省时间。
int main()
{
char str[] = "0.86527033 0.50127762 0.00529092 -0.50130544 0.86521534 0.00976016 0.00031476 -0.01109755 0.99993837 6.81169042 3.27070358 -0.13535404";
int i = 0;
while(str[i] != '\0')
{
if(str[i] == ' ')
printf("%d ", i);
++i;
}
printf("\n");
return 0;
}这是用C语言写的
输出结果
10 21 32 44 55 66 77 89 100 111 122
while ( strRecv, a, index++, _T(' '))
{
// strRecv就是每个字符串。
double d = _tcstod(strRecv, &strStop); // strStop recieved the stop scan string in strRecv.
}
tearywang:下面这句有错误吧:
CString strRecv, strStop;
编译时出现:
error C2664: 'strtod' : cannot convert parameter 2 from 'class CString *' to 'char ** '
我改成:
char *strRecv, *strStop;
后,编译调试又会提示下面的信息:
please enter the path for STRTOD.C.
这个是什么问题?
Sorry,这是一个接收停止字符串的缓存,代码没有在机器上测试,上面的代码还有个小错误:用这段:CString a = _T("0.86527033 0.50127762 0.00529092 -0.50130544 0.86521534 0.00976016 0.00031476 -0.01109755 0.99993837 6.81169042 3.27070358 -0.13535404");CString strRecv;// 这是一个缓存,接收函数解析过程中到什么位置停止了,能容纳目标字符串即可。
// 由于对整个字符串的切分,使用了AfxExtractSubString,而且整体字体串中的内容都是有效的数值,所以
// 缓存不必太大。有即可。TCHAR szStop[1024];
int index = 0;
while ( AfxExtractSubString(strRecv, a, index++, _T(' ')) )
{
// strRecv就是每个字符串。
double d = _tcstod(strRecv, &szStop); // szStop recieved the stop scan string in strRecv.
}
编译还是出错误:
error C2664: 'strtod' : cannot convert parameter 2 from 'char (*)[1024]' to 'char ** '
你的方法不错,但是还有个小错误,把你的那句稍微改下就可以了:
double d = _tcstod(strRecv, NULL); // szStop recieved the stop scan string in strRecv.还是谢谢你啊!!!
我晕,重点都写得很明白了,非重点你就不能自己查一下吗?
http://msdn.microsoft.com/en-us/library/kxsfc1ab(v=vs.71).aspx这里有_tcstod函数的用法。我记错了,那个不是缓存,是一个指向strRecv中停止扫描的开始字符。把TCHAR szStop[1024];改成TCHAR* pszStop;就好啦
#include <string.h>void main(void)
{ char cbuf[1024]="0.86527033 0.50127762 0.00529092 -0.50130544 0.86521534 0.00976016 0.00031476 -0.01109755 0.99993837 6.81169042 3.27070358 -0.13535404" ;
char *seps =" ";
char *token; token = strtok( cbuf, seps );
while( token != NULL )
{
/*打出得到的信息*/
printf( " %s\n", token );
/* 获取下一个 */
token = strtok( NULL, seps );
}
}
输出结果
0.86527033
0.50127762
0.00529092
-0.50130544
0.86521534
0.00976016
0.00031476
-0.01109755
0.99993837
6.81169042
3.27070358
-0.13535404
Press any key to continue