题目描述
解析
由于正则表达式的特殊样式,找到其中字符E
的位置,即可从正则表达式中将底数和指数部分分割出来,加上对STL string的熟练使用,就会很简单了。
#include<iostream>
#include<string>
#include<cmath>
using namespace std;
int main() {
string s, ans; cin >> s;
if (s[0] == '-') cout << "-";
int pos = s.find('E');
int ex = stoi(s.substr(pos + 1)); //指数
if (ex >= 0) {
if (pos - 3 <= ex) { //小数部分长度小于等于指数(需要补0)
ans = s[1] + s.substr(3, pos - 3) + string(ex - pos + 3, '0');
}
else {
ans = s[1] + s.substr(3, pos - 3);
ans.insert(ex + 1, ".");
}
}
else {
ans = "0." + string(abs(ex) - 1, '0') + s[1] + s.substr(3, pos - 3);
}
cout << ans;
return 0;
}