c++的程序设计——类型转换的(在线等!)

来源:百度知道 编辑:UC知道 时间:2024/07/02 06:57:35
编一函数集,可分别将整数,实数,布尔值转换成相应的字符串类型,及将以字符串表示的整数,实数,布尔值转换成相应类型的值!

楼上的很可爱啊~ 以为有atof就一定会有ftoa吧

用string流,直接转换

#include <iostream>
#include <string>
#include <sstream>
using namespace std;

void ValToArray()
{
double x= 43.435;
int y = 3434;
bool z = true;
stringstream ss;
char str[100];
ss << x;
ss >> str;
cout << str << endl;
ss.sync();
ss.clear();
ss << y;
ss >> str;
cout << str << endl;
ss.sync();
ss.clear();
ss << z;
ss >> str;
cout << str << endl;
}

void ArrayToVal()
{
string s;
bool flag = 0;
cout << "input a number string: ";
cin >> s;
for(string::iterator it = s.begin(); it != s.end(); it++)
{
if((*it < '0' || * it > '9') && *it != '.')
{
cout << "invalid in