1073 Scientific Notation (20分)
程序员文章站
2022-07-15 10:30:17
...
#include<string>
#include<math.h>
#include<iostream>
using namespace std;
int main() {
string s;
cin >> s;
int indexOfE = s.find('E');
string baseNumber = s.substr(1, indexOfE - 1);
int exp = stoi(s.substr(indexOfE + 1));
int pointIndex = baseNumber.find('.');
baseNumber.erase(pointIndex, pointIndex);
if (exp < 0) {
for (int i = 0; i < abs(exp); i++) {
baseNumber = '0' + baseNumber;
}
baseNumber.insert(1, ".");
}
else {
int pointIndex = 1 + exp;
if (pointIndex < baseNumber.size()) {
baseNumber.insert(pointIndex, ".");
}
else {
while (pointIndex > baseNumber.size()) {
baseNumber += '0';
}
}
}
if (s[0] == '-') {
baseNumber = '-' + baseNumber;
}
cout << baseNumber << endl;
}