1024 科学计数法(20 分)

#include
#include
#include
using namespace std;
string s;
int main()
{
    getline(cin, s);
    if (s[0] == '-')printf("-");
    int point=-1, e;
    for (int i = 1; i < s.length(); i++)
    {
        if (s[i] == '.')point = i;
        if (s[i] == 'E')e = i;
    }
    if (point == -1)point = e;
    int exp = 0;
    for (int i = e + 2; i < s.length(); i++)exp = exp * 10 + s[i] - '0';
    if (s[e + 1] == '-')
    {
        printf("0.");
        for (int i = 0; i < exp - 1; i++)printf("0");
        for (int i = 1; i < e; i++)
        {
            if (s[i] != '.')printf("%c", s[i]);
        }
    }
    else
    {
        int eleft = e - point - 1;
        if (exp >= eleft)
        {
            for (int i = 1; i < e; i++)
            {
                if (s[i] != '.')printf("%c", s[i]);
            }
            for (int i = 0; i < exp - eleft; i++)printf("0");
        }
        else
        {
            for (int i = 1; i <= point + exp; i++)
            {
                if (s[i] != '.')printf("%c", s[i]);
            }
            printf(".");
            for (int i = point + exp+1; i < e; i++)printf("%c", s[i]);
        }
    }
    return 0;
}

你可能感兴趣的:(1024 科学计数法(20 分))