PAT-A1073. Scientific Notation (20)(模拟)

来源:互联网 发布:对象数组初始化 编辑:程序博客网 时间:2024/05/21 03:27

指数正负分开处理,易错样例:-3.1415926E+4。

#include <iostream>#include <cstdio>#include <cstring>#include <cmath>#include <iomanip>#include <stdlib.h>using namespace std;#define MAX 10000+100int main(){    //freopen("in.txt", "r", stdin);    //freopen("out.txt", "w", stdout);    char str[MAX], zhishu[MAX], xiaoshu[MAX];    memset(xiaoshu, '\0', sizeof(xiaoshu));    memset(zhishu, '\0', sizeof(zhishu));    int i = 0, j = 0, f = 0, k = 0; //k是小数中小数点后个位数    cin >> str;    for (i = 1, j = 0; str[i] != 'E'; i++ )    {        xiaoshu[j++] = str[i];        k++;        if (str[i] == '.')            k = 0;    }    i++;    if (str[i] == '-')        f = 1;    for (i++, j = 0; str[i] != '\0'; i++)        zhishu[j++] = str[i];    //指数为负数    if (f == 1)    {        if ('-' == str[0])            cout << "-";        if (atoi(zhishu) == 0)        {            for (i = 0; xiaoshu[i] != '\0'; i++)                cout << xiaoshu[i];        }        else        {            cout << "0.";            for (i = 0; i < atoi(zhishu)-1; i++)                cout << "0";            for (i = 0; xiaoshu[i] != '\0'; i++)                if (xiaoshu[i] != '.')                    cout << xiaoshu[i];        }        cout << endl;    }    //指数为正    else    {        if ('-' == str[0])            cout << "-";        if (atoi(zhishu) == 0)        {            for (i = 0; xiaoshu[i] != '\0'; i++)                cout << xiaoshu[i];        }        else        {            if ( k <= atoi(zhishu))            {                for (i = 0; xiaoshu[i] != '\0'; i++)                    if (xiaoshu[i] != '.')                        cout << xiaoshu[i];                for(i = 0; i < atoi(zhishu)-k; i++)                    cout << "0";            }            else            {                //yiwei                for (i = 0; xiaoshu[i] != '\0'; i++)                    if (xiaoshu[i] == '.')                        break;                int j = i;                for (; i < j+atoi(zhishu); i++)                    xiaoshu[i] = xiaoshu[i+1];                xiaoshu[j+atoi(zhishu)] = '.';                cout << xiaoshu;            }        }        cout << endl;    }    return 0;}


0 0
原创粉丝点击