70,020
社区成员




sscanf("002","%d",&x);//x==2,前面的两个0被错误地去掉了
#include <stdio.h>
#include <string.h>
void trim0(char *s) {
// 2.000000e-006→2e-6
// 1.234560e+002→1.23456e2
// 1.234560e+005→1.23456e5
// 1.000000e+000→1e0
// -1.000000e+000→-1e0
// -1.002000e+002→-1.002e2
char x[40];
char E[2];
int z,e,i;
if (4==sscanf(s,"%d.%39[0-9]%1[Ee]%4d",&z,x,E,&e)) {
i=strlen(x)-1;
while (1) {
if ('0'==x[i]) {
x[i]=0;
if (i<=0) break;
i--;
} else break;
}
if (i<=0) {
sprintf(s,"%de%d",z,e);
} else {
sprintf(s,"%d.%se%d",z,x,e);
}
}
}
int main() {
char s[40];
double d;
while (1) {
printf("Input a float number(0 to exit):");
fflush(stdout);
rewind(stdin);
if (1==scanf("%lf",&d)) {
if (0.0==d) break;
sprintf(s,"%e",d);
trim0(s);
printf("%s\n",s);
}
}
return 0;
}
//Input a float number(0 to exit):0.000002
//2e-6
//Input a float number(0 to exit):123.456
//1.23456e2
//Input a float number(0 to exit):123456
//1.23456e5
//Input a float number(0 to exit):1
//1e0
//Input a float number(0 to exit):-1
//-1e0
//Input a float number(0 to exit):-100.2
//-1.002e2
//Input a float number(0 to exit):0
//
#include <stdio.h>
#include <string.h>
void trim0(char *s) {
// 2.000000e-006→2e-6
// 1.234560e+002→1.23456e2
// 1.234560e+005→1.23456e5
// 1.000000e+000→1e0
// -1.000000e+000→-1e0
char t[40];
int z,x,e;
sscanf(s,"%d.%6de%4d",&z,&x,&e);
if (0==x) {
sprintf(t,"%de%d",z,e);
} else {
while (1) {
if (x%10==0) x/=10; else break;
}
sprintf(t,"%d.%de%d",z,x,e);
}
strcpy(s,t);
}
int main() {
char s[40];
double d;
while (1) {
printf("Input a float number(0 to exit):");
fflush(stdout);
rewind(stdin);
if (1==scanf("%lf",&d)) {
if (0.0==d) break;
sprintf(s,"%e",d);
trim0(s);
printf("%s\n",s);
}
}
return 0;
}
//Input a float number(0 to exit):0.000002
//2e-6
//Input a float number(0 to exit):123.456
//1.23456e2
//Input a float number(0 to exit):123456
//1.23456e5
//Input a float number(0 to exit):1
//1e0
//Input a float number(0 to exit):-1
//-1e0
//Input a float number(0 to exit):0
//
//输入0.000002,输出2e-6;输入123.456,输出1.23456e2;输入123456,输出1.23456e5
#include <stdio.h>
#include <string.h>
void trim0(char *s) {
// 2.000000e-006→2e-6
// 1.234560e+002→1.23456e2
// 1.234560e+005→1.23456e5
// 1.000000e+000→1e0
char t[40];
int z,x,e;
sscanf(s,"%1d.%6de%4d",&z,&x,&e);
if (0==x) {
sprintf(t,"%1de%d",z,e);
} else {
while (1) {
if (x%10==0) x/=10; else break;
}
sprintf(t,"%1d.%de%d",z,x,e);
}
strcpy(s,t);
}
int main() {
char s[40];
double d;
while (1) {
printf("Input a float number(0 to exit):");
fflush(stdout);
rewind(stdin);
if (1==scanf("%lf",&d)) {
if (0.0==d) break;
sprintf(s,"%e",d);
trim0(s);
printf("%s\n",s);
}
}
return 0;
}
//Input a float number(0 to exit):0.000002
//2e-6
//Input a float number(0 to exit):123.456
//1.23456e2
//Input a float number(0 to exit):123456
//1.23456e5
//Input a float number(0 to exit):1
//1e0
//Input a float number(0 to exit):0
//