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

 

科学计数改为普通的表述

注意:

1、科学计数值可能是0

2、科学计数的有效位可能不足,需要补0

 

代码:

#include <iostream>
#include <cstdio>
#include <string>
using namespace std;

int main()
{
	int first=0,point=0,e=0,ex_f=0;	//有效数据起始位置,点的位置,E的位置,基数的有效位置(见例1,基数前面也有0……)
	int ex=0;	//基数
	string data;	//输入数据
	cin>>data;
	
	int i;
	for(i=1;i<data.size();i++)	//先扫第一个有效数字(非‘0’,非‘.’),可能不存在(数本身就是0)
	{
		if(data[i]!='0'&&data[i]!='.')
		{
			first=i;
			break;
		}
		if(data[i]=='.')	//可能首先找到的是'.'
			point=i;
		if(data[i]=='E')	//先找到'E',说明是0
		{
			first=0;
			break;
		}
	}
	if(first==0)
	{
		cout<<"0";
		return 0;
	}
	for(i++;i<data.size();i++)	//继续找E
	{
		if(data[i]=='.')	//有可能还没找到'.'
			point=i;
		if(data[i]=='E')
		{
			e=i;
			break;
		}
	}
	for(i+=2;i<data.size();i++)	//寻找基数的有效起始位置
		if(data[i]!='0')
		{
			ex_f=i;
			break;
		}
	if(ex_f!=0)	//计算基数
	{
		for(;i<data.size();i++)
		{
			ex*=10;
			ex+=data[i]-'0';
		}
	}
	if(data[e+1]=='-')	//考虑基数是否为负
		ex=-ex;


	int count=0;	//统计输出有效数字数
	int flag_point=0;	//是否输出过'.'的标志,1表示输出过
	if(data[0]=='-')
		cout<<"-";
	if(first<point&&point-first+ex<=0)	//有效数字在'.'前,且数字小于1(为小数),补0
	{
		cout<<"0.";
		flag_point=1;
		while(count<first-point-ex)
		{
			printf("0");
			count++;
		}
	}
	else if(first>point&&point-first+ex<0)	//有效数字在'.'后,且数字小于1(为小数),补0
	{
		cout<<"0.";
		flag_point=1;
		while(count+1<first-point-ex)
		{
			printf("0");
			count++;
		}
	}
	else if(first>point&&point-first+ex>=0)	//有效数字在'.'后,且数字大于等于1(基数必然大于0),需要调整ex,以使后面正确输出'.'
		ex-=first-point;
	for(i=first;i<e;i++)	//输出有效数据段
	{
		if(i!=point)
		{
			printf("%c",data[i]);
			count++;
		}
		if(flag_point==0&&count==ex+1&&i<e-1)	//没输出过'.',到达个位,且后面还有小数部分,输出小数点
			cout<<".";
	}
	while(flag_point==0&&count<ex+1)	//末尾补'0'
	{
		printf("0");
		count++;
	}


	return 0;
}


 

 

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值