64,682
社区成员
发帖
与我相关
我的任务
分享
#include <stdio.h>
#include <string.h>
void strwstr2doublefloat(char *str,float **ppf,double **ppd) {
int n;
static double d;
static float f;
char sf[30],sd[30];
int flag;
*ppf=NULL;
*ppd=NULL;
flag=0;
while (1) {
if (1==swscanf((wchar_t *)str,L"%lf%n",&d,&n)) {
if (n==(int)wcslen((wchar_t *)str)) {
flag=1;
break;
}
}
if (1==sscanf(str,"%lf%n",&d,&n)) {
if (n==(int)strlen(str)) {
flag=1;
break;
}
} else {
break;
}
}
if (flag) {
f=(float)d;
sprintf(sd,"%.15lg",d);
sprintf(sf,"%.6g",f);
if (NULL==strchr(sd,'#')) {
if (0==strcmp(sd,sf)) {
*ppf=&f;
} else {
*ppd=&d;
}
} else {
;
//If the argument corresponding to a floating-point specifier is
//infinite, indefinite, or NaN, printf gives the following output.
//Value Output
//------------------------------ -----------------------
//+infinity 1.#INFrandom-digits
//-infinity -1.#INFrandom-digits
//Indefinite (same as quiet NaN) digit.#INDrandom-digits
//NAN digit.#NANrandom-digits
}
}
}
int main() {
char s1[]="2e400";
wchar_t s2[]=L"1e39";
float *pf;
double *pd;
strwstr2doublefloat((char *)s1,&pf,&pd);
if (!pf&&!pd) printf("Format error!\n");
if (pf) printf("float %g\n",*pf);
if (pd) printf("double %lg\n",*pd);
strwstr2doublefloat((char *)s2,&pf,&pd);
if (!pf&&!pd) printf("Format error!\n");
if (pf) printf("float %.6g\n",*pf);
if (pd) printf("double %.15lg\n",*pd);
return 0;
}
#include <stdio.h>
#include <string.h>
void strwstr2doublefloat(char *str,float **ppf,double **ppd) {
int n;
static double d;
static float f;
char sf[30],sd[30];
int flag;
*ppf=NULL;
*ppd=NULL;
flag=0;
while (1) {
if (1==swscanf((wchar_t *)str,L"%lf%n",&d,&n)) {
if (n==(int)wcslen((wchar_t *)str)) {
flag=1;
break;
}
}
if (1==sscanf(str,"%lf%n",&d,&n)) {
if (n==(int)strlen(str)) {
flag=1;
break;
}
} else {
break;
}
}
if (flag) {
f=(float)d;
sprintf(sd,"%.15lg",d);
sprintf(sf,"%.6g",f);
if (0==strcmp(sd,sf)) {
*ppf=&f;
} else {
*ppd=&d;
}
}
}
int main() {
char s1[]="1.2";
wchar_t s2[]=L"1.23456789012";
float *pf;
double *pd;
strwstr2doublefloat((char *)s1,&pf,&pd);
if (!pf&&!pd) printf("Format error!\n");
if (pf) printf("float %g\n",*pf);
if (pd) printf("double %lg\n",*pd);
strwstr2doublefloat((char *)s2,&pf,&pd);
if (!pf&&!pd) printf("Format error!\n");
if (pf) printf("float %.6g\n",*pf);
if (pd) printf("double %.15lg\n",*pd);
return 0;
}
//float 1.2
//double 1.23456789012
//