PAT B1024 / A1073 scientific notation

Problem Description:

Scientists scientific notation is used to represent a convenient method for large or very small number, which is the regular expression [±] [1-9]. [0-9] + E [±] [0-9 ] +, i.e., only the integer part of the number one, there is at least one decimal part, the digital sign and exponent parts must explicitly given of even positive.

A real numbers are now given in scientific notation format, write program output as ordinary digital representation of Method A, and to ensure that all the valid bits are reserved.

Input formats:

Each input comprises a test, i.e., a real number in scientific notation represented by A. This number does not store the length exceeds 9999 bytes, and the absolute value is not more than 9999 index.

Output formats:

For each test case, in a row by general method A digital representation of the output, and to ensure that all the valid bits are retained, including 0 at the end.

Sample Input 1:

+1.23400E-03

Output Sample 1:

0.00123400

Sample Input 2:

1.2E+10

Output Sample 2:

12000000000

AC Code

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#define maxn 10010



int main()
{
 //   freopen("input.txt","r",stdin);
    char s[maxn];
    char tail[maxn];
    char index[maxn];
    char sym1,sym2;//记录尾数和指数的符号
    int num;
    scanf("%s",s);//得到s
    int len=strlen(s),i,j;//得到字符串的长度
    sym1=s[0];
    tail[0]=s[1];
    for(i=3;i<len;i++)
    {
        if(s[i]=='E')
        {
            break;
        }
        tail[i-2]=s[i];//
    }
    tail[i]='\0';//得到尾数部分
    i++;//得到E的长度
    sym2=s[i++];//得到指数部分的符号
    for(j=0;(j+i)<len;j++)
    {
        index[j]=s[j+i];
    }
    index[j]='\0';
    num=atoi(index);//指数的数值
    int tlen=strlen(tail);
    if(num==0)//指数为0,尾数原封不动
    {
        if(sym1=='-')printf("%c",sym1);
        for(i=0;i<tlen;i++)
        {
            printf("%c",tail[i]);
            if(i==0)printf(".");
        }
    }
    else if(sym2=='-')//指数为负数
    {
        if(sym1=='-')printf("%c",sym1);
        printf("0.");//得到字符串
        for(int i=1;i<num;i++)//还要打印0,
        {
            printf("0");
        }
        printf("%s",tail);
    }
    else//指数为正数
    {
        if(sym1=='-')printf("%c",sym1);
        if(num<tlen-1)//如果小数部分的长度(tlen-1)大于指数num,那么保留小数点
        {
            for(i=0;i<tlen;i++)
            {
                printf("%c",tail[i]);
                if(num==i)printf(".");//指数为0时,小数点打印i==0时候,平移num个单位后打印小数点的位置为第num个数字后(从0开始)
            }
        }
        else
        {//不需要保留小数点
                printf("%s",tail);//直接打印位数部分
            for(i=0;i<num-(tlen-1);i++)
                printf("0");//补末尾的0;
        }
    }




}

Published 82 original articles · won praise 1 · views 1555

Guess you like

Origin blog.csdn.net/weixin_43370733/article/details/104072356