科学计数法

链接:http://www.nowcoder.com/pat/6/problem/4050

题目描述

科学计数法是科学家用来表示很大或很小的数字的一种方便的方法,其满足正则表达式[+-][1-9]"."[0-9]+E[+-][0-9]+,即数字的整数部分

只有1位,小数部分至少有1位,该数字及其指数部分的正负号即使对正数也必定明确给出。



现以科学计数法的格式给出实数A,请编写程序按普通数字表示法输出A,并保证所有有效位都被保留。

 

输入描述:

每个输入包含1个测试用例,即一个以科学计数法表示的实数A。该数字的存储长度不超过9999字节,且其指数的绝对值不超过9999。



输出描述:

对每个测试用例,在一行中按普通数字表示法输出A,并保证所有有效位都被保留,包括末尾的0。

 

输入例子:

+1.23400E-03

 

输出例子:

0.00123400

 1 #include "iostream"
 2 #include <iomanip>
 3 #include <string.h>
 4 #include <string>
 5 #include <vector>
 6 #include <cmath>
 7 #include <cctype>
 8 #include <algorithm>
 9 using namespace std;
10 
11 int main()
12 {
13     string str1, str2;
14     cin >>str1;
15     int i=0, posDot=0, count=0, flag=1;
16     if(str1[0] == '-')    flag=0;
17     for(i=1; i<str1.length(); ++i)
18     {
19         if(str1[i] == '.')    posDot = i;
20         else    if(str1[i] == 'E')    {++i;    break;}
21                 else    str2 += str1[i];
22     }
23     for(int j=i+1; j<str1.length(); ++j)
24     {
25         count = count*10+str1[j]-'0';
26     }
27     if(str1[i] == '-')
28     {
29         count = -count;
30     }
31     posDot += count;
32     if(posDot < 0)
33     {
34         if(flag == 0) cout <<'-';
35         cout <<"0.";
36         posDot = -posDot;
37         for(int i=0; i<(-count)-1; ++i)    cout <<0;
38         cout <<str2 <<endl;
39     }
40     else
41     {
42         if(flag == 0) cout <<'-';
43         int i;
44         for(i=0; i<str2.length(); ++i)
45         {
46             if(i == posDot-1)    cout <<".";
47             cout <<str2[i];
48             
49         }
50         while(i < posDot-1) 
51         {
52             cout <<0;
53             ++i;
54         }
55         cout <<endl;
56     }
57     return 0;
58 }

注意:当输入 -1.2E+10时,输入为-12000000000

转载于:https://www.cnblogs.com/mtc-dyc/p/4621784.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值