#include <iostream>
#include <cstdio>
#include <queue>
#include <vector>
#include <cstring>
using namespace std;
int tr(char str[]){
int len=strlen(str);
int ans=0;
for(int i=0;i<len;i++){
ans*=10;
ans+=str[i]-'0';
}
return ans;
}
int main()
{
char str[1000];memset(str,0,sizeof(str));
while(cin>>str){
char str10[1000];
char num[1000];
char ans2[1000];int p=-1;char ans[1000];
memset(num,0,sizeof(num));
memset(str10,0,sizeof(str10));
memset(ans,0,sizeof(ans));
memset(ans2,0,sizeof(ans2));
int len=strlen(str);
bool flag=1;int ppp=-1,pppp=-1,pso;
for(int i=0;i<len;i++){
if(str[i]=='e') {flag=0;continue;}
if(str[i]=='.') pso=i;
if(flag)
num[i]=str[i];
else
str10[++ppp]=str[i];
}
int len10=tr(str10);
//cout<<num<<endl;
int pp=-1;
int lennum=strlen(num);
int newpso=pso+len10;
//cout<<newpso<<endl;
//cout<<lennum<<endl;
if(newpso>=lennum){
for(int i=0;i<=newpso;i++){
if(i<lennum)
if(num[i]!='.')
ans[++pp]=num[i];
else
continue;
else
ans[++pp]='0';
}
}else{
for(int i=0;i<lennum;i++)
if(num[i]!='.')
ans[++pp]=num[i];
strcpy(num,ans);
pp=-1;
lennum=strlen(num);
for(int i=0;i<lennum;i++){
++pp;
if(pp==newpso){
ans[pp]='.';
i--;
}
else
ans[pp]=num[i];
}
}
//cout<<ans<<endl;
int lenans=strlen(ans);
flag=0;
for(int i=0;i<lenans;i++){
if(ans[i]!='0'||ans[i]=='.') flag=1;
if(flag){
ans2[++p]=ans[i];
}
}
if(ans2[0]=='.'){
ans[0]='0';
int len=strlen(ans2);
for(int i=0;i<len;i++)
ans[i+1]=ans2[i];
}else{
strcpy(ans,ans2);
}
len=strlen(ans);
flag=0;bool flag2=0;
for(int i=0;i<len;i++)
if(!flag){
if(ans[i]=='.')
flag=1;
}else{
if(ans[i]!='0'){
flag2=1;
break;
}
}
if(flag2)
cout<<ans<<endl;
else{
for(int i=0;i<len;i++){
if(ans[i]=='.') break;
cout<<ans[i];
}
cout<<endl;
}
}
}
也有简单的写法(转)
见http://blog.csdn.net/shyazhut/article/details/52224422
#include<stdio.h>
#include<cstring>
const int MYDD=1103;
int main() {
int a,k,b;
char d[MYDD];
scanf("%d.",&a); //两个scanf不可合为一个
scanf("%[^e]%ne%d",d,&k,&b);
//note: "%[^e]" 读入任意多的字符,直到遇到 "=" 停止
if(k==1&&d[0]==48&&!b) printf("%d\n",a);
//note: 这里要是改为字符型判断d[0]=='0'时间复杂度增加一倍,可达到 30ms
else if(b>=k) printf("%d%s%.*d\n",a,d,b-k,0);
else printf("%d%.*s.%s\n",a,b,d,d+b);
// 参数 b 输出字符串 d 的个数
return 0;
}