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

本文详细介绍了如何使用C++实现科学计数法到正常数值的转换过程,包括解析字符串中的符号、基数、指数等关键信息,并根据不同情况进行输出。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

【来源】

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;
}


评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值