这一题自己花的时间有点多。
要注意几个问题:
1,注意题目的输入格式,小数点就在固定的位数上,
2,输入用整个字符串读取
3,正号不输出
4 ,考虑正指数和负指数两种情况就行了
#include <iostream>
#include <cstring>
using namespace std;
char a[20000];
int E; //大E的位置
int exp(int n) //找大E的位置
{
int temp = 0;
int i;
for(i = 0;i<n;i++)
if(a[i] == 'E'){
E = i;
break;
}
bool flag = false;
for(i++;i<n;i++)
{
if(a[i] == '-')
flag = true;
if(a[i] >= '0' && a[i] <= '9')
temp = temp*10 + a[i] -'0';
}
if(flag)
temp *= -1;
return temp;
}
int main()
{
scanf("%s",a);
int n = strlen(a);
int i = 2;
int ex = exp(n);
bool flag = true;
if(ex < 0)
{
flag = false;
ex *= -1;
}
if(a[0] == '-')
cout << a[0];
if(ex == 0)
{
for(int j = 1;j < E;j++)
cout << a[j];
return 0;
}
if(flag)
{
int j = 1;
for(;j<i;j++)
cout << a[j];
i++;
while(ex--)
{
if(i<E)
cout << a[i++];
else
cout << 0;
}
if( i < E){
cout << ".";
while(i < E)
cout << a[i++];
}
}
else
{
cout << "0.";
ex = ex + 1 - i;
while(ex--)
cout << 0;
cout << a[1];
i++;
while(i < E)
cout << a[i++];
}
cout << endl;
return 0;
}