我面试碰到过 str2float 的一个题:
#include <iostream>
(5488)#include <string>
#include <cmath>
(6079)#include <cstdio>
using namespace std;
int str2i(string s){
int n = s.length();
int b;
for(int i = 0; i < n; ++i){
int c = s[i] - '0';
b+=pow(10, i);
}
return b;
}
double stof(string& s){
int pos = s.find('.');
string per = s.substr(0,pos);
string aft = s.substr(pos);
int a = str2i(per);
double b = str2i(aft);
while(b>1) b*=0.1;
return a+b;
}
int main(){
cout.width(7);
cout<<3444.676609986555211;
} 实现比较简单,而且貌似有些问题,不过只是转整数的话足够了