您的位置:首页 > 其它

PAT Basic Level 1024. 科学计数法(20)

2014-03-11 22:49 453 查看
【来源】

1024. 科学计数法

【分析】

此题给出用科学技术法表示的数,要求输出正常表示的数。为字符串处理题。

大致解题思路为从字符串中分别解析出基数的符号、基数的大小、指数的大小、指数的符号,然后以此得到输出。此类题目需要耐心和细心,最好先自己设计一系列的测试样例,然后分情况解析字符串即可。需注意整数输出的时候末尾没有小数点。

类似的题目可参考 Advanced level 1060.
Are They Equal (25),比此题难度略大,需要更加细心。

【代码】

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

int main()
{
string num;
cin >> num;
int pos;
if(num[0]== '+'){
pos = 1;
}
else{
pos = 0;
}
int digit = num[1]-'0';
int i;
for(i = 3; i < num.size(); ++i){
if(num[i] == 'E'){
break;
}
}
int Epos = i;
int expopos;
if(num[i+1] == '+'){
expopos = 1;
}
else{
expopos = 0;
}
stringstream ss;
for(i += 2;i<num.size(); ++i){
ss << num[i]-'0';
}
int expo;
ss >> expo;

if(pos == 0){
cout << "-";
}
if(expopos == 0){
int shift = expo;
cout << "0.";
for(int i = 0; i < shift-1; ++i){
cout << "0";
}
cout << digit;
for(int i = 3; i < Epos; ++i){
cout << num[i];
}
cout << endl;
}
else{
cout << digit;
int overflow = 0;

int i;
for(i = 0; i < expo; ++i){
if(3 + i < Epos){
cout << num[3+i];
}
else{
overflow = 1;
cout << 0;
}
}
if(overflow == 0 && num[i+3] != 'E'){
cout << ".";
}
for(i = i+3; i < Epos; ++i){
cout << num[i];
}

cout << endl;
}
system("pause");
return 0;
}


【点评】

此题为2014.3.1春季PAT第四题,考察稍复杂的字符串处理。本题给出的代码是考试时仓促写的,虽然AC了,但是结构有些混乱,使用了大量的条件语句,有空再优化吧。

【优化版代码】

/*
PAT Basic 1024

2014.9.3
*/

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

int main()
{
string sci_num;
cin >> sci_num;

// Parse the number into parts
// Decimal point position
size_t ppos = sci_num.find('.');
// 'E' position
size_t epos = sci_num.find('E');

// sign
int neg = sci_num[0] == '-' ? 1 : 0;
// base number(integer part)
string part1 = sci_num.substr(1, ppos-1);
// base number(float part)
string part2 = sci_num.substr(ppos+1, epos-ppos-1);
// exponent
string part3 = sci_num.substr(epos + 2);
int neg_exp = sci_num[epos + 1] == '-' ? 1 : 0;
stringstream ss(part3);
int exp;
ss >> exp;

string result = "";

if (!neg_exp){
// move decimal point to right
result += part1;
int p;
for (p = 0; p < exp && p < part2.size(); ++p){
result += part2[p];
}
if (p < exp){
// appnending 0s in the end
while (p < exp){
result += '0';
++p;
}
}
else{
//
result += '.';
for (; p < part2.size(); ++p){
result += part2[p];
}
}
}
else{
// move decimal point to left
result += part2;
int p;
for (p = 0; p < exp && part1.size() - p > 0; ++p){
result = part1[part1.size()-p-1] + result;
}
if (p < exp){
// add 0s in the front
while (p < exp){
result = '0' + result;
++p;
}
result = "0." + result;
}
else{
//
result = '.' + result;
for (; part1.size() - p > 0; ++p){
result = part1[part1.size() - p - 1] + result;
}
}
}
if (result[0] == '.'){
// avoid .123
result = "0" + result;
}
if (result[result.size() - 1] == '.'){
// avoid 123.
result = result.substr(0, result.size() - 1);
}
if (neg){
// sign
result = "-" + result;
}

cout << result << endl;

system("pause");
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  PAT 字符串