【PAT】A1073. Scientific Notation (20)

来源:互联网 发布:文档加密软件下载 编辑:程序博客网 时间:2024/06/03 09:26

A1073. Scientific Notation (20)

主要易错点在于对于零指数的处理,和正指数时小数点和‘E’之间的数字个数和指数相等的情况的处理

#include <stdio.h>#include <string.h>int main(int argc, const char * argv[]) {    char str[10000];    gets(str);    int len = (int)strlen(str);    // 判断正负    if(str[0] == '-')        printf("%c", str[0]);    int epos = 0; // 'E'的位置    while(str[epos] != 'E')        epos++;    int exp = 0; // 指数    for(int i = epos + 2; i < len; i++)        exp = exp * 10 + (int)(str[i] - '0');    // 指数为‘0’    if(exp == 0){        for(int i = 1; i < epos; i++)            printf("%c", str[i]);        return 0;    }    if(str[epos + 1] == '-'){ // 负指数        printf("0.");        for(int i = 0; i < exp - 1; i++)            printf("0");        for(int i = 1; i < epos; i++){            if(str[i] == '.')                continue;            else                printf("%c", str[i]);        }    } else { // 正指数        for(int i = 1; i < epos; i++){            if(str[i] == '.'){                continue;            } else {                printf("%c", str[i]);                if(exp == i - 2 && epos - 3 != exp) // 小数点和'E'之间的数的个数(epos - 3)不能等于指数,如果等于指数,就不应该打印小数点                    printf(".");            }        }        // exp较大,打印出多余的“0”        for(int i = 0; i < exp - (epos - 3); i++)            printf("0");    }    return 0;}
0 0