PAT Basic level 1024 科学计数法转化为普通数字


1024. 科学计数法 (20)

时间限制
100 ms
内存限制
32000 kB
代码长度限制
8000 B
判题程序
Standard
作者
HOU, Qiming

科学计数法是科学家用来表示很大或很小的数字的一种方便的方法,其满足正则表达式[+-][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
/******************************************************
 *@ author: Gaominquan
 *@ mail  : mqgao@outlook.com
 *@ data  : 2014-6-4
 *@ funciton: change the scientific num to normal number
 * *****************************************************/
#include<iostream>
#include<string>
#include<cmath>
using namespace std;

int main(){
	
	string wordStr = "-1.2E+10";
	
	cin>>wordStr;

	bool nagetive = wordStr[0] == '-'? true:false;
	//note if this num is an nagetive num

	int indexE = wordStr.find('E');
	string basicNum = wordStr.substr(1,indexE - 1);
	// get the num bottom number, meaning '-1.2E+10', this step get 1.2
	string clearNum = "";

	for(int i = 0; i<basicNum.size();i++){
		if(i == 1) continue;
		clearNum += basicNum[i];
		// delete the basicNum's dot, means change '1.2' to '12'
	}
	
	string rStr = wordStr.substr(indexE+2,wordStr.size()-indexE-2);
	//like get the botton num, this step get the pow number, means '-1.2E+10', we will get 10
	
	int rNum = 0;
	int strLength = rStr.size();

	for(int wI = 1; wI <= strLength; wI++){
		rNum += pow(10,wI-1) * (rStr[ strLength - wI] - '0');
		//change "10" that get in line 32 to (int)10
	}

		
	//int leftZero = wordStr[indexE+1] == '-' ? rNum:0;
	//a typical ERROR !!! NOTICE	
	rNum *= (wordStr[indexE+1] == '-')? -1:1;

	int leftZero = 0;
	if(rNum<0){
		leftZero = -1*rNum;
	}
	// leftZero means if need print 0 before print the botton number. 
	// If power number less than 0, we need print 0 before botton number;

	if(nagetive){
		cout<<"-";
	}

	int index = 0;	
	int numLength = clearNum.size();
	bool printDot = false;
	bool printNumFull = false;


	for(int zI = 0; zI < leftZero; zI++){
		cout<<"0";
		if(zI == 0){
			cout<<".";
		}
		printDot = true;
	}

	while(!printDot||!printNumFull){
		if(index < numLength){
			cout<<clearNum[index];
		}else{
			if(!printDot) cout<<"0";
		}

		if(index+1>=numLength){
			printNumFull = true;
		}	

		if(index == rNum){
			// 这里有个BUG一定要注意,在50行里边,可以一定要记得,如果指数是小于零的,那么rNum不能取它的绝对值
			// 就是说,比方E-4,那么简单看来可以先打印4个零在前边就可以了,但是如果rNum是4而不是-4,在这个地方由可能点号就要
			// 多打一次。
			if(!printNumFull) cout<<".";
			printDot = true;
		}
		index++;
	}cout<<endl;

	return 0;
}


 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值