C++中将科学计数转换为其他类型
2012-03-09 23:40
225 查看
#include <iostream>
#include <sstream>
#include <string>
using namespace std;
double sciToDub(const string & str)
{
stringstream ss(str);
double d = 0;
ss >> d;
if (ss.fail())
{
string s = "Unable to format";
s += str;
s += "as s number!";
throw(s);
}
return(d);
}
int main(int argc, _TCHAR* argv[])
{
int i;
try
{
cout << sciToDub("1.234e-02") << endl;
cout << sciToDub("-1.234e-02") << endl;
}
catch(string & e)
{
cout << e << endl;
}
cin >> i;
return 0;
}
改为模版:
#include <iostream>
#include <sstream>
#include <string>
using namespace std;
template<typename T>
T sciToDub(const string & str)
{
stringstream ss(str);
T d = 0;
ss >> d;
if(ss.fail())
{
string s = "Unable to format";
s += str;
s += "as s number!";
throw(s);
}
return(d);
}
int main(int argc, _TCHAR* argv[])
{
int i;
try
{
cout << sciToDub<double>("1.234e-02") << endl;
cout << sciToDub<double>("-1.234e-02") << endl;
}
catch(string & e)
{
cout << e << endl;
}
cin >> i;
return 0;
}
#include <sstream>
#include <string>
using namespace std;
double sciToDub(const string & str)
{
stringstream ss(str);
double d = 0;
ss >> d;
if (ss.fail())
{
string s = "Unable to format";
s += str;
s += "as s number!";
throw(s);
}
return(d);
}
int main(int argc, _TCHAR* argv[])
{
int i;
try
{
cout << sciToDub("1.234e-02") << endl;
cout << sciToDub("-1.234e-02") << endl;
}
catch(string & e)
{
cout << e << endl;
}
cin >> i;
return 0;
}
改为模版:
#include <iostream>
#include <sstream>
#include <string>
using namespace std;
template<typename T>
T sciToDub(const string & str)
{
stringstream ss(str);
T d = 0;
ss >> d;
if(ss.fail())
{
string s = "Unable to format";
s += str;
s += "as s number!";
throw(s);
}
return(d);
}
int main(int argc, _TCHAR* argv[])
{
int i;
try
{
cout << sciToDub<double>("1.234e-02") << endl;
cout << sciToDub<double>("-1.234e-02") << endl;
}
catch(string & e)
{
cout << e << endl;
}
cin >> i;
return 0;
}
相关文章推荐
- C++中string 和其他类型之间的转换
- C#中将Object数组转换为String(或者其他类型)的数组(集合)
- 把其他类型的DLL文件转换为C++的DLL文件
- C++中将string类型转换为int类型
- C++中将数字转换成string类型
- C++中将string类型转换为int, float, double类型 主要通过以下几种方式:
- C++中将string类型变量转换成int型变量
- c++中将十进制数转换为其他进制的简便方法
- (转)C++ 字符串类型转换为其他字符串
- C++中将整型数与字符串型之间的类型转换
- C/C++关于字符串string、char*字符串数组与其他类型转换
- C++中将string类型转换为int, float, double类型 主要通过以下几种方式:
- C++中将string类型转换为int, float, double类型 主要通过以下几种方式:
- C++中将string类型转换为int, float, double类型
- C++中将string类型转换为int, float, double类型
- c++其他类型转换成string类型
- C++中将string类型转换为int, float, double类型
- C++中将string类型转换为int, float, double类型
- C++中将string类型转换为int, float, double类型 主要通过以下几种方式:
- C++中将string类型转换为int, float, double类型 主要通过以下几种方式: