1024. 科学计数法

科学计数法是科学家用来表示很大或很小的数字的一种方便的方法,其满足正则表达式[+-][1-9]"."[0-9]+E[+-][0-9]+,即数字的整数部分只有1位,小数部分至少有1位,该数字及其指数部分的正负号即使对正数也必定明确给出。

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

输入格式:

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

输出格式:

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

输入样例1:
+1.23400E-03
输出样例1:
0.00123400
输入样例2:
-1.2E+10
输出样例2:
-12000000000

==========================================================================================
感觉自己做麻烦了。
简单来说我先判断了原形式的正负,如果为负则先输出负号。然后用两个变量记录“E”和“.”的位置。接着就是再
根据后面的指数大小一位一位排数字或前面补0,到了需要加点的地方就加上点,如果超过了原数长度再在后面补
0。
#include <iostream>
#include <iomanip>
#include <stdio.h>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <cctype>
#include <sstream>
#include <map>
#include <set>
#include <vector>
#include <queue>
#include <stack>
using namespace std;

int main()
{
    int i,temp,len,tlen,pos_point,pos_E;
    int sign1,sign2;
    string str,ans,tt,tem;
    stringstream ss;
    cin>>str;
    len = str.size();
    pos_point = str.find('.',0);
    ans.clear();
    tt.clear();
    if(str[0] == '-')
    {
        sign1 = -1;
        ans += "-";
    }
    else
        sign1 = 1;
    pos_E = str.find('E',0);
    tlen = pos_E - pos_point;
    if(str[pos_E+1] == '-')
        sign2 = -1;
    else
        sign2 = 1;
    for(i=pos_E+2;i<len;i++)
        tt += str[i];
    ss<<tt;
    ss>>temp;
    if(sign2 == -1)
    {
        ans += "0.";
        for(i=1;i<temp;i++)
            ans += "0";
        ans += str[1];
        for(i=pos_point+1;i<pos_E;i++)
            ans += str[i];
    }
    else
    {
        if(temp >= tlen)
        {
            ans += str[1];
            for(i=pos_point+1;i<pos_E;i++)
                ans += str[i];
            for(i=0;i<=temp-tlen;i++)
                ans += "0";
        }
        else
        {
            ans += str[1];
            for(i=3;i<3+temp;i++)
                ans += str[i];
            if(i < pos_E)
                ans += ".";
            for(i;i<pos_E;i++)
                ans += str[i];
        }
    }
    cout<<ans<<endl;
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值