1073 Scientific Notation (20)(20 分)

注意要比较E前有多少项和exp的大小

#include
#include
#include
using namespace std;
string s;
int main()
{
    getline(cin, s);
    if (s[0] == '-')printf("-");
    int E, exp = 0, point = -1;
    for (int i = 0; i < s.length(); i++)
    {
        if (s[i] == 'E')
        {
            E = i;
        }
        if (s[i] == '.')point = i;
    }
    point == -1 ? E : point;
    int cnt = E - point - 1;
    if (cnt < 0)cnt = 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 = 1; i < exp; i++)printf("0");
        for (int i = 1; i < E; i++)
        {
            if (s[i] != '.')printf("%c", s[i]);
        }
    }
    else
    {
        if (cnt <= exp)
        {
            for (int i = 1; i < E; i++)
            {
                if (s[i] != '.')printf("%c", s[i]);
            }
            for (int i = 0; i < exp - cnt; 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;
}

你可能感兴趣的:(1073 Scientific Notation (20)(20 分))