PAT-1073-Scientific Notation

#include<stdio.h>
#include<math.h>
#include<string.h>

#define MAX 1000
int main()
{
    char str[MAX];
    char exp[MAX];
    char data_sym;
    char comp_sym;
    while(scanf("%s",str)!=EOF)
    {
        char * e =strchr(str,'E');//查找字符串里面字符,返回指向这个字符的指针。
        char * point = strchr(str,'.'); 
        comp_sym=*(e+1);
        data_sym=*str;
        
        //printf("%c %c",data_sym,comp_sym);
        int start = e-str+2;
        int len = strlen(str);
        int comp=0;
        for(int j =start;j<len;j++)
        {
             double num = (str[j]-'0')*pow(10.0,(len-j-1)*1.0);
             //printf("%lf
",num);
             comp+=(int)num;
             //printf("%d
",comp);
        }
        if(comp_sym=='-')
        {
             int move=comp-1;
             if(data_sym=='-')
                printf("-"); 
             printf("0.");
             for(int i=0;i<move;i++)
               printf("0");
             for(char *p=str+1;p!=e;p++)
             {
                if(*p!='.')
                  printf("%c",*p);
             }
             printf("
");
        }
        else
        {
            int digit= e-point-1;
            int j=-1;
            bool b=false;
            //printf("%d %d",digit,comp);
            if(data_sym=='-')
              printf("-");
            if(digit>comp)
            {
               for(char *p = str+1;p!=e;p++)
               {
                  if(*p!='.')
                  {
                     printf("%c",*p);
                  }
                  else
                  {
                      b=true;
                  }
                  if(b)
                  {
                     j++;
                     if(j==comp)
                       printf(".");
                  }
               }
               printf("
");
            }
            else
            {
                int move=comp-digit;
                for(char *p=str+1;p!=e;p++)
                {
                 if(*p!='.')
                   printf("%c",*p);
                }
                for(int i=0;i<move;i++)
                  printf("0");
                printf("
");
            }
        }
    }
    return 0;
}

  

原文地址:https://www.cnblogs.com/championlai/p/3946805.html