您的位置:首页 > 编程语言 > C语言/C++

c语言中不同数据类型之间的相互转化

2015-01-19 19:11 162 查看
1)怎样把带空格的字符串分割成没有空格的字符串(c++)

/*****************************************************************

split a string with space into several small strings without space

*****************************************************************/

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#include <iostream>

void split(char* str, double* position)

{

        char* words[3];

        char* temp;

        temp = strtok(str, " ");

        int i = 0;

        while(temp!=NULL)

            {

                words[i] = temp;

                temp = strtok(NULL, " ");

                position[i] = atof(words[i]);

                i++;

            }

}

int main()  

{  

 

    char lineBuf[] = "11.12 11.13 11.14";

    std::cout << "1" << std::endl;  

    double position[3];

    split(lineBuf, position);

    std::cout << "1" << std::endl;

    for(int i=0; i<3; i++)

    {

//        std::cout << words[i] << std::endl;

//        position[i] = atof(words[i]);

        std::cout << position[i] << std::endl;        

    }

    return 0;  

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: