PAT Class B 1024 scientific notation

Scientific notation is a convenient method used by scientists to represent very large or very small numbers. It satisfies the regular expression [+-][1-9] .[0-9]+E[+-][0- 9]+, that is, the integer part of the number has only 1 digit, and the decimal part has at least 1 digit. The sign of the number and its exponent part must be clearly given even for positive numbers.

Now the real number A is given in the format of scientific notation. Please write a program to output A according to the ordinary number representation, and ensure that all valid digits are reserved.

Input format:

Each input contains 1 test case, which is a real number A expressed in scientific notation. The storage length of this number does not exceed 9999 bytes, and the absolute value of its exponent does not exceed 9999.

Output format:

For each test case, output A in a line according to ordinary number notation, and ensure that all valid bits are reserved, including the 0 at the end.

Input example 1:

+1.23400E-03

Output sample 1:

0.00123400

Input example 2:

-1.2E+10

Output sample 2:

-12000000000


 

#include<stdio.h>
#include<iostream>
using namespace std;
int main() {
    string s;
    int k,kk;
    while(getline(cin,s)) {
        int e=0,t;
        string shu="";
        if(s[0]=='+')k=1;
        else k=-1;

        for(int i=1; i<s.size(); i++) {
            if(s[i]=='E') {
                t=i;
                break;
            } else if(s[i]=='.'){
                continue;
            }else {
                shu+=s[i];
            }
        }
        if(s[t+1]=='+')kk=1;
        else kk=-1;
        for(int i=t+2; i<s.size(); i++) {
            e=e*10+s[i]-48;
        }
        //t-3就是点到e中有几个
        if(k==-1){
            cout<<"-";
        }
        if(kk==1){
            if(t-3<e){
                cout<<shu;
                e=e-t+3;
                while(e--){
                cout<<"0";    
                }
            }else if(t-3==e){
                cout<<shu;
            }
            else{
                for(int i=0;i<=e;i++){cout<<shu[i];}
                cout<<".";
                for(int i=e+1;i<shu.size();i++){cout<<shu[i];}
            }
            cout<<endl;
        }
        else if(kk==-1){
            cout<<"0"<<".";
            e=e-1;
            while(e--){
            cout<<"0";    
            }
            for(int i=0;i<shu.size();i++){cout<<shu[i];}
            cout<<endl;
        }
    }



}

 

Guess you like

Origin blog.csdn.net/paycho/article/details/84555990