Detailed explanation: PAT (Basic Level) Practice (Chinese) 1024 scientific counting method (20) (20 points)

Scientific counting is a convenient method for scientists to express large or small numbers. It satisfies the regular expression [+ -] [1-9] ". [0-9] + E [+ -] [0-9] +, that is, the integer part of a number is only one digit, and the decimal part is at least one digit. Even if the sign of the number and its exponential part is positive, it must be clearly given.

Now the real number A is given in the format of scientific counting method. Please write A program to output A according to the common number representation and ensure that all valid bits are reserved.

Input format:

Each input contains A test case, that is, A real number A in scientific notation. The storage length of this number shall not exceed 9999 bytes, and the absolute value of its index shall not exceed 9999.

Output format:

For each test case, output A in one line according to the common digital representation, and ensure that all significant bits are reserved, including the 0 at the end.

Enter example 1:

+1.23400E-03

Output example 1:

0.00123400

Enter example 2:

-1.2E+10

Output example 2:

-12000000000

It's a simple topic, just a few points to pay attention to.

1: How to output the 0 at the end? I save it with string directly;

2: - 1.20E+1 out: - 1.20, that is, the index is smaller than the number of decimal places

3: In: - 1.2E+1 out: - 1.2 means that the index is equal to the number of digits after the decimal point, and the decimal point is not output

#include<iostream>
#include<cstdlib>
#include <stdio.h>
#include <cstring>
using namespace std ;
int main()
{
    char sym ;
    int zhs,power;
    int i;
    char num[10006] ;
    sym = getchar();
    scanf("%d.",&zhs);
    char tmp ;
    tmp = getchar();
    for ( i = 0; tmp!='E' ; ++i)
    {
        num[i]=tmp;
        tmp = getchar();
    }
    num[i]=0;
    if(sym=='-')
        cout<<"-";
    sym = getchar();
    cin>>power;
    if(sym=='-')
    {
        cout<<"0.";
        for(i = 1;i<power;i++)
            cout<<"0";
        cout<<zhs<<num ;
    }
    else
    {
        cout<<zhs;
        if(strlen(num)<power)
        {
            cout<<num;
            for (int j =strlen(num) ; j <power ; ++j)
                cout<<"0";
        }
        else
        {
            for (i = 0 ; i <power ; ++i)
                cout<<num[i];
            if(power <strlen(num))//The index is smaller than the number of decimal places, so no decimal point is output
            cout<<".";
            for (i = power ; i <strlen(num) ; ++i)
                cout<<num[i];
        }
    }
}

 

Posted by jasraj on Fri, 31 Jan 2020 03:50:22 -0800