PAT甲1073 Scientific Notation(20 分)
阿新 • • 發佈:2019-02-13
#include <string.h>
#include <stdio.h>
#include <algorithm>
#include <vector>
#include <string>
#include <iostream>
#include <map>
using namespace std;
int main()
{
string str;
cin>>str;
string frac;
int exp=0;
string result="";
if (str[0]=='-')
{
result+='-';
}
str.erase(str.begin());
for(int i=0;i<str.length();i++)
{
if(str[i]=='E')
{
frac=str.substr(0,i);
bool enega=str[i+1]=='-';
for(int j=i+2;j<str.length();j++)
{
exp =exp*10+str[j]-'0';
}
if(enega==true)exp=-exp;
break;
}
}
if(exp>=0)
{
result+=frac[0];
frac.erase(frac.begin());
frac.erase(frac.begin());
int index=0;
while(index<frac.length())
{
if (exp==0)result+='.';
result+=frac[index];
index++;
exp--;
}
while(exp>0)
{
result+='0';
exp--;
}
}
else
{
result+="0.";
exp++;
frac.erase(frac.begin()+1);
while(exp<0)
{
result+='0';
exp++;
}
result+=frac;
}
cout<<result<<endl;
return 0;
}