您的位置:首页 > 其它

PAT1024科学计数法 (20)

2016-04-07 09:48 232 查看
科学计数法是科学家用来表示很大或很小的数字的一种方便的方法,其满足正则表达式[+-][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

string str = "-1.23400E+14";
string a = "",tmp="";
char b = ' ', c = ' ';
int len = str.length();
int j = 0;
float num = 0.0;
stringstream ss;
int snum = 0;

b = str[j];
for (int i = 1; i < len; ++i){
if (str[i] == 'E'){ j = i; break; }
else{
a.push_back(str[i]);
}
}
cout << a << endl;
tmp = a;
ss << a;
ss >> num;
a.clear(); ss.clear();

++j;
c = str[j];
++j;
for (j; j < len; ++j){
a.push_back(str[j]);
}
ss << a;
ss >> snum;
cout << snum<< endl;
a.clear(); ss.clear();

if (b == '-')cout << '-';

if (snum == 0){
cout << tmp;
}

if (c=='+'&&snum!=0){
int loc = snum + 2;
len = tmp.length();
for (int i = 0; i < len; ++i){
if (i == loc)cout << '.';
if (tmp[i] == '.')continue;
else cout << tmp[i];
}
int loc2 = loc - len;
if (loc2 > 0){
for (int i = 0; i < loc2; ++i){
cout << '0';
}
}
}
else if (c=='-'&&snum!=0){
cout << "0.";
int loc = snum-1;
len = tmp.length();
for (int i = 0; i < loc; ++i){
cout << '0';
}
for (int i = 0; i < len; ++i){
if (tmp[i] == '.')continue;
else cout << tmp[i];
}
}
cout << endl;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: