字符串转化为数字 aotf
2015-07-24 15:15
267 查看
版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/lingzhu111/article/details/47041657
#include <iostream>
using namespace std;
float str2num(char *p){
float tmp = 0.0;
int dot = 0;
float n = 1.0;
const char *str = p;
if (*str == '-' || *str == '+'){
str++;
}
while(*str != 0){
if(dot == 0){
if(*str <= '9' && *str >= '0'){
tmp = tmp*10 + ((*str) - '0');
} else if (*str == '.') {
dot = 1;
} else {
break;
}
} else {
if(*str <= '9' && *str >= '0'){
n = n*10;
tmp += ((*str) - '0')/n;
} else {
break;
}
}
str++;
}
if (*p == '-'){
tmp = -tmp;
}
return tmp;
}
int main(){
float n=0.0;
char p[50]="";
cin.getline(p,20);
n = str2num(p);
cout << n << endl;
}
#include <iostream>
using namespace std;
float str2num(char *p){
float tmp = 0.0;
int dot = 0;
float n = 1.0;
const char *str = p;
if (*str == '-' || *str == '+'){
str++;
}
while(*str != 0){
if(dot == 0){
if(*str <= '9' && *str >= '0'){
tmp = tmp*10 + ((*str) - '0');
} else if (*str == '.') {
dot = 1;
} else {
break;
}
} else {
if(*str <= '9' && *str >= '0'){
n = n*10;
tmp += ((*str) - '0')/n;
} else {
break;
}
}
str++;
}
if (*p == '-'){
tmp = -tmp;
}
return tmp;
}
int main(){
float n=0.0;
char p[50]="";
cin.getline(p,20);
n = str2num(p);
cout << n << endl;
}
相关文章推荐
- c#如实现将一个数字转化为其他进制字符串输出
- C++字符串转化为数字的库函数
- js字符串转化为数字 详解
- c的数字与字符串的转化
- Java数字字符串如何转化为数字数组
- QLineEdit将数字转化为字符串,对单个字符的修改
- 将一个数字字符串转换为数字(支持数字中间夹杂字母,但是受int型的限制,转化的长度很有限),如:"1234" -->1234
- Long.parseLong(String s) 其中s必须是数字形式的字符串,才能运用该函数转化为长整型。
- 字符串转化为数字
- 关于字符串转化为数字的深度优化两种算法
- 字符串转化为数字,并拆分为几个数字
- 将数字转化为大写汉字的字符串
- 16进制字符串转化为数字
- 面试题:字符串转化为数字
- C51 数字和字符串互相转化
- 二进制字符串转化为十进制数字
- JavaScript中数字与字符串相互转化
- 字符串和数字间的转化
- C语言字符串与数字转化(T函数系列)
- 14.求字符串中的数字字符转化为数字并求和