64,687
社区成员
发帖
与我相关
我的任务
分享
#include<iostream>
using namespace std;
double my_atof(const char* s)
{
bool dot = true;
double result = 0;
int sign = 1;
int i = 0;
double temp = 1;
for( ; isspace(s[i]) ;++i);
sign = (s[i] == '-') ? -1 : 1;
if(s[i] == '+' || s[i] == '-')
++i;
for( ; isdigit(s[i]) || s[i] == '.'; ++i){
if(s[i] == '.'){
dot = false;
++i;
}
if(dot == true){
result = result * 10 + (s[i] - '0');
}
else{
temp = temp * 10;
result = result + (s[i] - '0')/temp;
}
}
return sign * result;
}
int main()
{
cout<<my_atof(" 33990.09822")<<endl;
return 0;
}
int main()
{
cout<<setiosflags(ios::fixed)<<setiosflags(ios::showpoint)<<setprecision(5);//小数点后面的个数
cout<< setw(8)<<my_atof("33990.09822")<<endl;
return 0;
//cout<<setiosflags()
}