1073. Scientific Notation (20)

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 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-03

Sample Output 1:

0.00123400

Sample Input 2:

-1.2E+10

Sample Output 2:

-12000000000

#include <iostream>
#include <iomanip>
#include <math.h>
#include <stdio.h>
#include <string>
#include <string.h>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <vector>

using namespace std;

int main()
{

    char s[9999];
    scanf("%s", s);
    int len = strlen(s);

    int idx;//E的下标
    for (int i = 0; i < len; i++)
    {
        if (s[i] == 'E')
        {
            idx = i;
            break;
        }
    }
    int cnt_xs = idx-3;//小数点后面的位数
    string a;
    int j = 0;
    char e[9999];//临时数组储存尾数
    int fuhao_zhishu = (s[idx + 1] == '+') ? 1 : -1;//指数的符号
    for (int i = idx + 2; i < len; i++)
    {
        e[j++] = s[i];
    }
    int zhishu = atoi(e);//字符串转换成整数

    for (int i = 1; i < idx; i++)//提取尾数部分为字符串a
    {
        a = a + s[i];
    }
    
    
    if (s[0] == '-')//只有当尾数符号为负时输出符号位
        cout << '-';

    if (zhishu == 0)//相当于小数点不用移动
    {
        cout << a;
    }

    else if (zhishu != 0)
    {
        a = a.replace(a.find('.'), 1, "");//删去尾数部分的小数点
        if (zhishu*fuhao_zhishu < 0)//小数点左移时
        {
            if (zhishu == 1)//10^-1
            {
                a = "0." + a;
                cout << a;
            }
            else
            {
                for (int j = 0; j < zhishu - 1; j++)
                    a = "0" + a;
                a = "0." + a;
                cout << a;
            }
        }
        else//小数点右移时
        {
            if (zhishu < cnt_xs)//直接后移小数点
            {
                a.insert(zhishu+1, ".");//插入小数点
                cout << a;
            }
            else if (zhishu == cnt_xs)//小数点加到末尾,相当于直接输出
            {
                cout << a;
            }
            else//末尾加0
            {
                for (int j = 0; j < zhishu - cnt_xs; j++)
                    a = a + '0';
                cout << a;
            }

        }
        
    }



    system("pause");
    return 0;
}

 

posted @ 2017-03-24 14:48  NhdVW0V  阅读(141)  评论(0编辑  收藏  举报