3.6 字符串处理 :B1024/A1073. 科学计数法

#include<iostream>
#include<cstdio>
#include<stdlib.h>
#include<string.h>
#include<string>
using namespace std;
int main(){
    char ch[1000],tmp[10];
    int i=0,exp;
    gets(ch);
    if(ch[0]=='-')
        printf("-");

    while(ch[i]!='E') i++;
    int y=i+2,s=0,len=strlen(ch);
    for(;y<len;y++){
        tmp[s++]=ch[y];
    }
    tmp[s]='\0';
    exp=atoi(tmp);
    if(exp==0){//指数为0的情况
        for(int j=1;j<i;j++)
            printf("%c",ch[j]);
    }

    if(ch[i+1]=='-'){//指数为-
        printf("0.");
        for(int j=1;j<=exp-1;j++)
            printf("0");
        printf("%c",ch[1]);
        for(int j=3;j<i;j++)
            printf("%c",ch[j]);
    }
    else{//指数为+
         for(int j=1;j<i;j++){
            if(ch[j]=='.') continue;
            printf("%c",ch[j]);
            if(j==exp+2 && exp!=i-3)
                printf(".");
         }
         //指数较大,输出多余的0
         for(int j=0;j<exp-(i-3);j++)
            printf("0");
    }
    printf("\n");
    return 0;
}
发布了157 篇原创文章 · 获赞 15 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/nanke_4869/article/details/104485101