PAT (Advanced Level) 1073 Scientific Notation(20 分)科学计数法
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 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-9].[0-9]+E[+-][0-9]+相匹配,就是说整数部分只有一位数字(1~9中任意一个),小数部分至少有一个数字(0~9中任意一个),并且这个数(整数部分+小数部分)和它的指数的正负号均会给出,即使全正。
现在给出一个用科学记数法表示的实数A,你需要保留所有有意义的数字的并输出它的传统计数形式。
正则表达式 [+-][1-9].[0-9]+E[+-][0-9]+ 解析
-方括号中的字符只能取其一
- [+-]:取+或-
- [1-9]:取1,2,3…9中的任意一个
- [0-9]同理
-暴露在外的:. E
- 像. , )等这样的符号和英文字母A,B,C,D,E…..a,b,c,d…..等,写什么就照抄就好
-但是 !!!!
- 遇见+:常以[ ]+的形式出现,表示括号中的任意字符可以取不只一次
以上只是对题中的正则表达式的简析,具体的大家可以自行查阅相关资料。
注意点:
- 如果double,和math.h中的pow(),要输出很长的位数的时候会产生误差,该误差由pow()函数产生;已进过该坑,所以建议用string。
- 原数据中的0,一个不能少,看样例1。所以要注意比较小数的位数与指数的大小,不够的,小数部分补0,整数部分补0和.。
- 如果指数为0,则去除E及后面的即可
- 正数输出是不用带+号的,这个可以最后统一处理
代码块
#include<iostream>
#include<string>
using namespace std;
int main()
{
string str;
cin >> str;
bool Sign1 = true;//the first sign [ true: + false: -]
bool Sigh2 = true;//the second sign [ true: + false: -]
if (str[0] == '-')Sign1 = false;
int idx = 3, NumOf_fraction = 0;//idx=3 because there are the first sign, one digit and a dot before the fractional portion
while (str[idx] != 'E')
{
NumOf_fraction++;
idx++;
}
idx++;
if (str[idx++] == '-')Sigh2 = false;
int exponent = 0;
while (idx < str.length())
{
exponent = str[idx++] - '0' + exponent * 10;
}
str.erase(str.begin() + 3 + NumOf_fraction, str.end()); //remove E and strings after it
if (exponent != 0) // if exponent=1, just jump this part and consider the sign
{
str.erase(str.begin() + 2); //remove the dot
if (Sigh2) //if exponet >0 ,right shift the dot
{
if (exponent > NumOf_fraction) // exponent > the bits of fraction ,then zero-filled
str.append(exponent - NumOf_fraction, '0');
else if (NumOf_fraction == exponent) //test point 4
str = str.substr(0, str.length() - (NumOf_fraction - exponent));
else // exponent < the bits of fraction ,right shift the dot (find it's location and insert it)
str.insert(str.length() - (NumOf_fraction - exponent), ".");
}
else
{
//if exponet<0 ,left shift the dot and zero-filled
while (--exponent)
{
str.insert(1, "0");
}
str.insert(1, "0.");
}
}
if (Sign1) // deal with the first sign ,if it is positive ,remove it.
{
str.erase(str.begin());
}
cout << str << endl;
//system("pause");
return 0;
}
希望对你有帮助哦~~