PAT_A1073 / PAT_B1024 | Scientific Notation

1073 Scientific Notation (20point(s))

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

 

一、对于输入的处理:

符号op1、整数部分(一位数)integer、字符串str 三部分

 

二、处理字符串str:

  1. 找到E的下标位置len,E之前都是小数部分,所以len也表示小数部分的数的个数
  2. 存下指数符号op2(E的后一位)
  3. 处理并存下指数exponent(op2后一直到最终)

 

三、核心的计算部分处理:

先输出答案的符号(-直接输出,+则不输出)

 

🔺 op2为正号

     🌂指数大于等于小数位数

  •          输出 integer 和所有小数部分
  •          在末尾补上剩余的0

     🌂指数小于小数位数

  •          输出integer和部分小数部分
  •          输出小数点
  •          输出剩余小数部分

🔺 op2为负号

  •         输出 0 和小数点
  •         输出 exponent - 1 个0
  •         输出 integer 和所有小数部分

 

完整代码:

//
// Created by LittleCat on 2020/2/9.
//

#include <cstdio>

#define N 10000

/* 将对应字符串转化为数字 */
int getNumber(char s[], int start) {
    int n = 0;
    for (int i = start; s[i] != '\0'; i++) {
        n = n * 10;
        n += s[i] - '0';
    }
    return n;
}

int main() {
    int integer;
    char str[N];
    char op1;

    scanf("%c%d.%s", &op1, &integer, str);

    if (op1 == '-')
        printf("%c", op1);  //先输出正负


    int len = 0;
    while (str[++len] != 'E');// 找到E对应的下标,即小数位数
    char op2 = str[len + 1];   //指数的符号
    int exponent = getNumber(str, len + 2);  //获取指数
    str[len] = '\0';   //将字符串在E处截止,即字符串全在存小数部分

    if (op2 == '+') {
        /* 输出原先的整数部分 */
        printf("%d", integer);

        /* 指数比小数位数多 */
        if (exponent >= len) {
            printf("%s", str);  //输出原先所有小数部分
            for (int i = len; i < exponent; i++)
                printf("0");  //后面补零
        } else { // 指数比小数位数少
            for (int i = 0; i < exponent; i++)
                printf("%d", str[i] - '0');  //输出小数点前的部分
            printf(".");  //输出小数点
            for (int i = exponent; i < len; i++)
                printf("%d", str[i] - '0');  //输出小数点后的部分
        }
    } else {
        printf("0.");
        for (int i = 1; i < exponent; i++)  //输出exponent - 1个0
            printf("0");
        printf("%d%s", integer, str);
    }
}



 

end 

欢迎关注个人公众号 鸡翅编程 ”,这里是认真且乖巧的码农一枚。

---- 做最乖巧的博客er,做最扎实的程序员 ----

旨在用心写好每一篇文章,平常会把笔记汇总成推送更新~

在这里插入图片描述

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值