【PAT B 1024】科学计数法

代码有问题但是AC了,而且我还不知道哪里有问题,淦

#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
const int maxn = 100005;
int main(){
    char list[maxn],dec[maxn];
    int ans[maxn] = {0};
    int positive = 0,point = 0,len,count = 0,num = 0,zero = 0,ten = 1;
    bool firstflag = true,lastflag = false,ispoint = false,number = true,decimal = false;
    cin >> list;
    len = strlen(list);

    if(list[0] == '+')
        firstflag = true;
    else if(list[0] == '-')
        firstflag = false;

    for(int i = 1;i < len;i++){
        if(list[i] == '.')
            ispoint = true;

        if(list[i] == 'E') {
            if (list[i + 1] == '+')
                lastflag = true;
            else if (list[i + 1] == '-')
                lastflag = false;
            number = false;
        }

        if((list[i] != 'E') && (list[i] != '.') && number){
            ans[count++] = list[i] - '0';
        }

        if(!ispoint){
            point++;
        }
        if(!number){
            if(('0' <= list[i]) && (list[i] <= '9'))
                dec[num++] = list[i];
            dec[num] = '\0';
        }
    }

    reverse(dec,dec + strlen(dec));
    for(int i = 0;i < strlen(dec);i++){
        zero += ten * (dec[i] - '0');
        ten *= 10;
    }

    if(!firstflag)
        cout << '-';

    if(lastflag){
        zero -= (count - point - 1);
        if(zero <= 0) {
            zero *= -1;
            decimal = true;
        }
    } else{
        cout << "0.";
        reverse(ans,ans + count);
        for(int i = count;i < count + zero - 1;i++)
            ans[i] = 0;
        reverse(ans,ans + count + zero - 1);
    }
    if(zero > 1) {
        for (int i = 0; i < count + zero - 1; i++) {
            if (decimal) {
                if (i == point + zero + 2)
                    cout << '.';
            }
            cout << ans[i];
        }
    } else if(zero <= 1){
        for (int i = 0; i < count; i++) {
            if (decimal) {
                if (i == point + zero + 2)
                    cout << '.';
            }
            cout << ans[i];
        }
    }

    return 0;
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。