C++ 如何把C风格字符串转化为数字
2018-02-12 16:13
316 查看
#include "stdafx.h"
#include<string>
#include "iostream"
using namespace std;
double ToNumber(const char* charArray)
{
double Value = 0;
bool isNegative = false;
bool isFloat = false;
int floatCount = 0;
//处理首字母
if (*charArray == '-')
{
isNegative = true;
charArray++;
}
if (*charArray == '+')
{
charArray++;
}
while (*charArray) //*charArray != '\0'
{
int intChar = *charArray - '0';
if (*charArray == '.') {
isFloat = true;
charArray++;
continue;
}
if (isFloat) {
floatCount++;
Value = Value +intChar * pow(0.1, floatCount);
}
else {
Value = Value * 10+intChar;
}
charArray++;
}
if (isNegative)Value = -Value;
return Value;
}
bool InputIsLegue(char* input) {
if (*input == NULL) {
cout << "Error:输入不能为空。请重新输入!" << endl << endl;
return false;
}
int pointCount = 0;
//判断首字母
if (*input == '-' || *input == '+') {
input++;
}
while (*input != '\0')
{
if (*input > '9' || *input < '0')
{
if (*input != '.')
{
cout << "Error:输入数字字符串不能包含非数字字符。请重新输入!" << endl << endl;
return false;
}
else
{
pointCount++;
if (pointCount >= 2)
{
cout << "Error:输入数字字符串不能包含多个小数点。请重新输入!" << endl << endl;
return false;
}
}
}
input++;
}
cout << "输入合法,可转为数字..." << endl;
return true;
}
int main()
{
////测试用例
//const char* case0= "123095";
//const char* case1 = "56486759815872639127598247598275423894729374";
//const char* case3 = "78.54";
//const char* case33 ="78.54.48";
//const char* case4 = "-5673";
//const char* case5 = "+5673";
//const char* case6 ="56AAAAAA73";
int SIZE = 100;
char* input=new char(SIZE);
while (1)
{
cin.getline(input, SIZE);
if (*input == 'q')break;
bool isLegue= InputIsLegue(input);
if (isLegue == false)continue;
else
{
double result = ToNumber(input);
cout << "转换结果:"<<result << endl<<endl;
}
}
return 0;
}
#include<string>
#include "iostream"
using namespace std;
double ToNumber(const char* charArray)
{
double Value = 0;
bool isNegative = false;
bool isFloat = false;
int floatCount = 0;
//处理首字母
if (*charArray == '-')
{
isNegative = true;
charArray++;
}
if (*charArray == '+')
{
charArray++;
}
while (*charArray) //*charArray != '\0'
{
int intChar = *charArray - '0';
if (*charArray == '.') {
isFloat = true;
charArray++;
continue;
}
if (isFloat) {
floatCount++;
Value = Value +intChar * pow(0.1, floatCount);
}
else {
Value = Value * 10+intChar;
}
charArray++;
}
if (isNegative)Value = -Value;
return Value;
}
bool InputIsLegue(char* input) {
if (*input == NULL) {
cout << "Error:输入不能为空。请重新输入!" << endl << endl;
return false;
}
int pointCount = 0;
//判断首字母
if (*input == '-' || *input == '+') {
input++;
}
while (*input != '\0')
{
if (*input > '9' || *input < '0')
{
if (*input != '.')
{
cout << "Error:输入数字字符串不能包含非数字字符。请重新输入!" << endl << endl;
return false;
}
else
{
pointCount++;
if (pointCount >= 2)
{
cout << "Error:输入数字字符串不能包含多个小数点。请重新输入!" << endl << endl;
return false;
}
}
}
input++;
}
cout << "输入合法,可转为数字..." << endl;
return true;
}
int main()
{
////测试用例
//const char* case0= "123095";
//const char* case1 = "56486759815872639127598247598275423894729374";
//const char* case3 = "78.54";
//const char* case33 ="78.54.48";
//const char* case4 = "-5673";
//const char* case5 = "+5673";
//const char* case6 ="56AAAAAA73";
int SIZE = 100;
char* input=new char(SIZE);
while (1)
{
cin.getline(input, SIZE);
if (*input == 'q')break;
bool isLegue= InputIsLegue(input);
if (isLegue == false)continue;
else
{
double result = ToNumber(input);
cout << "转换结果:"<<result << endl<<endl;
}
}
return 0;
}
相关文章推荐
- VC 中的IP地址控件,关联一个DWORD的变量dwValue,如何将这个dwValue按IP地址显示转化成4个数字或者字符串?
- C++ 字符串, 数字 相互转化
- C++字符串转化为数字的库函数
- C++字符串转化为数字的库函数
- C/C++ 字符串与数字相互转化方法小结
- c++中字符串数字之间的转化
- C++如何判断一个string字符串,是否是数字
- c++中将数字转化为字符串
- C C++ 数字与字符串的转化
- c++中stringstream 类的用法及字符串与数字之间转化
- 如何将数字转化为字符串
- 【短篇】Python下如何把带有千位分隔符的浮点数字符串形式转化为数字
- c++中字符串与数字之间的相互转化
- Java数字字符串如何转化为数字数组
- C++将十进制数字转化为X 进制的字符串
- C++字符串转化为数字的库函数
- C++ 字符串转化为数组 有负数 数字之间为空格间隔
- C++字符串转化为数字的库函数
- c++数字转化为字符串、字符串转换为数字
- C++字符串转化为数字的库函数