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 file 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-03Sample Output 1:
0.00123400Sample Input 2:
-1.2E+10Sample Output 2:
-12000000000
分析:字符串的处理。有小坑。代码中使用stringstream来输入会比较方便。
#include <iostream>
#include <string>
#include <vector>
#include <sstream>
using namespace std;
int main(int argc, char** argv) {
string outStr="";
string str;
cin>>str;
if(str[0] == '-')
outStr+="-";
int pos = str.find("E");
string num(str, 1, pos-1);
string t(str,pos+1);
stringstream ss;
ss<<t.substr(1,t.size()-1);
char eFlag = t[0];
int i, val=0, index;
ss>>val;
if(val == 0){
cout<<outStr<<num<<endl;
return 0;
}
if(eFlag== '+'){
outStr += num[0];
if( val<=num.size()-2 ){
//例如这种情况: +1.23E+1
index = 0;
for(i=2; index<val; i++, index++){
outStr += num[i];
}
if(val!=num.size()-2){
outStr += ".";
}
outStr += num.substr(i, num.size()-i);
}else{
outStr += num.substr(2, num.size()-2);
for(i=0; i<val-num.size()+2; i++){
outStr += "0";
}
}
}else if(eFlag == '-'){
outStr += "0.";
for(i=0; i<val-1; i++){
outStr += "0";
}
for(i=0; i<num.size(); i++){
if(num[i] != '.'){
outStr += num[i];
}
}
}
cout<<outStr<<endl;
return 0;
}
本文详细介绍了如何将科学记数法表示的实数转换为常规表示法,并保持所有有效数字,包括尾随零。通过使用字符串处理技巧,如`stringstream`,可以轻松实现这一转换。

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



