您的位置:首页 > 其它

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

2014-06-05 12:34 316 查看

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 numint indexE = wordStr.find('E');string basicNum = wordStr.substr(1,indexE - 1);// get the num bottom number, meaning '-1.2E+10', this step get 1.2string 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 10int 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 !!! NOTICErNum *= (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;}

                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  PAT ZJU