
1073 Scientific Notation (20 分)
Scientific notation is the way that scientists easily handle very large numbers or very small numbers. The notation matches the regular expression [±][1-9].[0-9]+E[±][0-9]+ which means that the integer portion has exactly one digit, there is at least one digit in the fractional portion, and the number and its exponent’s signs are always provided even when they are positive.
Now given a real number A in scientific notation, you are supposed to print A in the conventional notation while keeping all the significant figures.
Input Specification:
Each input contains one test case. For each case, there is one line containing the real number A in scientific notation. The number is no more than 9999 bytes in length and the exponent’s absolute value is no more than 9999.
Output Specification:
For each test case, print in one line the input number A in the conventional notation, with all the significant figures kept, including trailing zeros.
Sample Input 1:
+1.23400E-03
Sample Output 1:
0.00123400
Sample Input 2:
-1.2E+10
Sample Output 2:
-12000000000
#include<iostream>
#include<cstdlib>
#include<string>
using namespace std;
int main() {
string s;
int pos;
int count1=0,count2=0;
string index;
int e;
cin >> s;
for (int i = 0; i < s.length(); i++) {
if (s[i] == '.') pos = i;
else if (s[i] == 'E') index = s.substr(i + 1, s.length() - i);
}
if (s[0] != '+') {
cout << '-';
if (index[0] == '+') {
e = stoi(index);
for (int i = 1; i < pos; i++) {
cout << s[i];
count1++;
}
for (int j = pos + 1; j < s.length(); j++) {
if (s[j] == 'E') break;
cout << s[j];
if (j == pos + e&&s[j+1]!='E') cout << ".";
count2++;
}
for (int k = 0; k < e-count2; k++) cout << 0;
}
else {
e = -stoi(index);
for (int i = 1; i < pos; i++) {
count1++;
}
if (count1 > e) {
for (int j = 0; j < count1 - e; j++)
cout << s[j];
cout << ".";
for (int k = count1-e; k < s.length(); k++) {
if (s[k] == '.') continue;
if (s[k] == 'E') break;
cout << s[k];
}
}
else {
cout << 0 << ".";
for (int j = 1; j < e-count2; j++) cout << 0;
for (int k = 1; k < s.length(); k++) {
if (s[k] == '.') continue;
if (s[k] == 'E') break;
cout << s[k];
}
}
}
}
else{
if (index[0] == '+') {
e = stoi(index);
for (int i = 1; i < pos; i++) {
cout << s[i];
count1++;
}
for (int j = pos + 1; j < s.length(); j++) {
if (s[j] == 'E') break;
cout << s[j];
if (j == pos + e && s[j + 1] != 'E') cout << ".";
count2++;
}
for (int k = 0; k < e - count2; k++) cout << 0;
}
else {
e = -stoi(index);
for (int i = 1; i < pos; i++) {
count1++;
}
if (count1 > e) {
for (int j = 0; j < count1 - e; j++)
cout << s[j];
cout << ".";
for (int k = count1 - e; k < s.length(); k++) {
if (s[k] == '.') continue;
if (s[k] == 'E') break;
cout << s[k];
}
}
else {
cout << 0 << ".";
for (int j = 1; j < e - count2; j++) cout << 0;
for (int k = 1; k < s.length(); k++) {
if (s[k] == '.') continue;
if (s[k] == 'E') break;
cout << s[k];
}
}
}
}
}
本文介绍了一种将科学计数法表示的数字转换为常规表示的方法,并提供了一个C++实现示例。该程序能够处理非常大或非常小的数字,保持所有有效数字,包括尾随的零。
591

被折叠的 条评论
为什么被折叠?



